Module: Ransack::Adapters::ActiveRecord::JoinDependency

Defined in:
lib/ransack/adapters/active_record/join_dependency.rb,
lib/ransack/adapters/active_record/3.0/join_dependency.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
# File 'lib/ransack/adapters/active_record/join_dependency.rb', line 8

def self.included(base)
  base.class_eval do
    # Squeel's graft is more capable than Ransack's.
    alias_method_chain :graft, :ransack unless defined?(Squeel)
  end
end

Instance Method Details

#build_join_association_respecting_polymorphism(reflection, parent, klass = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/ransack/adapters/active_record/join_dependency.rb', line 49

def build_join_association_respecting_polymorphism(reflection, parent, klass = nil)
  if reflection.options[:polymorphic] && klass
    JoinAssociation.new(reflection, self, parent, klass)
  else
    JoinAssociation.new(reflection, self, parent)
  end
end

#build_polymorphic(association, parent = nil, join_type = Arel::OuterJoin, klass = nil) ⇒ Object

Should only be called by Ransack, and only with a single association name



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ransack/adapters/active_record/join_dependency.rb', line 24

def build_polymorphic(association, parent = nil, join_type = Arel::OuterJoin, klass = nil)
  parent ||= join_parts.last
  reflection = parent.reflections[association] or
    raise ::ActiveRecord::ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?"
  unless join_association = find_join_association_respecting_polymorphism(reflection, parent, klass)
    @reflections << reflection
    join_association = build_join_association_respecting_polymorphism(reflection, parent, klass)
    join_association.join_type = join_type
    @join_parts << join_association
    cache_joined_association(join_association)
  end

  join_association
end

#find_join_association_respecting_polymorphism(reflection, parent, klass) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/ransack/adapters/active_record/join_dependency.rb', line 39

def find_join_association_respecting_polymorphism(reflection, parent, klass)
  if association = find_join_association(reflection, parent)
    unless reflection.options[:polymorphic]
      association
    else
      association if association.active_record == klass
    end
  end
end

#graft_with_ransack(*associations) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/ransack/adapters/active_record/join_dependency.rb', line 15

def graft_with_ransack(*associations)
  associations.each do |association|
    join_associations.detect {|a| association == a} ||
      build_polymorphic(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type, association.reflection.klass)
  end
  self
end