Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically create inverse relationship as needed #89

Closed
catmando opened this issue Dec 11, 2018 · 1 comment
Closed

Automatically create inverse relationship as needed #89

catmando opened this issue Dec 11, 2018 · 1 comment
Labels
enhancement New feature or request ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch
Milestone

Comments

@catmando
Copy link
Contributor

catmando commented Dec 11, 2018

currently hyper-model requires that for the both sides of the belongs_to and has_many relationships are explicitly declared.

Seems like the following patch fixes that:

module ActiveRecord
  module Associations
    class AssociationReflection
      def find_inverse
        klass.reflect_on_all_associations.each do |association|
          next if association.association_foreign_key != @association_foreign_key
          next if association.klass != @owner_class
          next if association.attribute == attribute
          return association if klass == association.owner_class
        end
        if macro == :belongs_to
          Hyperstack::Component::IsomorphicHelpers.log "**** warning dynamically adding relationship: #{klass}.has_many :#{@owner_class.name.underscore.pluralize}, foreign_key: #{@association_foreign_key}", :warning
          klass.has_many @owner_class.underscore.pluralize, foreign_key: @association_foreign_key
        else
          Hyperstack::Component::IsomorphicHelpers.log "**** warning dynamically adding relationship: #{klass}.belongs_to :#{@owner_class.name.underscore}, foreign_key: #{@association_foreign_key}", :warning
          klass.belongs_to @owner_class.underscore, foreign_key: @association_foreign_key
        end
        # raise "Association #{@owner_class}.#{attribute} "\
        #       "(foreign_key: #{@association_foreign_key}) "\
        #       "has no inverse in #{@klass_name}"
      end
    end
  end
end
@catmando catmando added the enhancement New feature or request label Dec 11, 2018
@catmando catmando added this to the alpha1.3 milestone Dec 11, 2018
@catmando
Copy link
Contributor Author

closed see batch7/association_naming_spec.rb:84

@catmando catmando added the ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch label Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch
Projects
None yet
Development

No branches or pull requests

1 participant