Module: MetaWhere::JoinDependency
- Defined in:
- lib/meta_where/join_dependency.rb
Defined Under Namespace
Classes: AssociationNotFoundError, BaseMismatchError, ConfigurationError
Class Method Summary collapse
Instance Method Summary collapse
- #build_with_metawhere(associations, parent = nil, join_type = Arel::Nodes::InnerJoin) ⇒ Object
- #find_join_association_with_metawhere(name_or_reflection, parent) ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/meta_where/join_dependency.rb', line 4 def self.included(base) base.class_eval do alias_method_chain :build, :metawhere alias_method_chain :find_join_association, :metawhere end end |
Instance Method Details
#build_with_metawhere(associations, parent = nil, join_type = Arel::Nodes::InnerJoin) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/meta_where/join_dependency.rb', line 15 def (associations, parent = nil, join_type = Arel::Nodes::InnerJoin) parent ||= @joins.last if MetaWhere::JoinType === associations klass = associations.klass join_type = associations.join_type associations = associations.name end case associations when Symbol, String reflection = parent.reflections[associations.to_s.intern] or raise ConfigurationError, "Association named '#{ associations }' was not found; perhaps you misspelled it?" unless (association = find_join_association(reflection, parent)) && (!klass || association.active_record == klass) @reflections << reflection if reflection.[:polymorphic] raise ArgumentError, "You can't create a polymorphic belongs_to join without specifying the polymorphic class!" unless klass association = PolymorphicJoinAssociation.new(reflection, self, klass, parent) else association = build_join_association(reflection, parent) end association.join_type = join_type @joins << association cache_joined_association(association) end association else (associations, parent, join_type) end end |
#find_join_association_with_metawhere(name_or_reflection, parent) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/meta_where/join_dependency.rb', line 45 def (name_or_reflection, parent) case name_or_reflection when MetaWhere::JoinType join_associations.detect do |j| (j.reflection.name == name_or_reflection.name) && (j.reflection.klass == name_or_reflection.klass) && (j.parent == parent) end else (name_or_reflection, parent) end end |