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

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::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 build_with_metawhere(associations, parent = nil, join_type = Arel::InnerJoin)
  parent ||= join_parts.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.options[: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
      @join_parts << association
      cache_joined_association(association)
    end
    association
  else
    build_without_metawhere(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
57
58
59
60
61
62
# File 'lib/meta_where/join_dependency.rb', line 45

def find_join_association_with_metawhere(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
    case name_or_reflection
    when Symbol, String
      join_associations.detect {|j| (j.reflection.name == name_or_reflection.to_s.intern) && (j.parent == parent)}
    else
      join_associations.detect {|j| (j.reflection == name_or_reflection) && (j.parent == parent)}
    end
    #find_join_association_without_metawhere(name_or_reflection, parent)
  end
end