Class: Ransack::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/ransack/context.rb

Direct Known Subclasses

Adapters::ActiveRecord::Context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ Context

Returns a new instance of Context.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ransack/context.rb', line 30

def initialize(object, options = {})
  @object = object.scoped
  @klass = @object.klass
  @join_dependency = join_dependency(@object)
  @join_type = options[:join_type] || Arel::OuterJoin
  @base = @join_dependency.join_base
  @engine = @base.arel_engine
  @arel_visitor = Arel::Visitors.visitor_for @engine
  @default_table = Arel::Table.new(@base.table_name, :as => @base.aliased_table_name, :engine => @engine)
  @bind_pairs = Hash.new do |hash, key|
    parent, attr_name = get_parent_and_attribute_name(key.to_s)
    if parent && attr_name
      hash[key] = [parent, attr_name]
    end
  end
end

Instance Attribute Details

#arel_visitorObject (readonly)

Returns the value of attribute arel_visitor.



5
6
7
# File 'lib/ransack/context.rb', line 5

def arel_visitor
  @arel_visitor
end

#auth_objectObject

Returns the value of attribute auth_object.



6
7
8
# File 'lib/ransack/context.rb', line 6

def auth_object
  @auth_object
end

#baseObject (readonly)

Returns the value of attribute base.



5
6
7
# File 'lib/ransack/context.rb', line 5

def base
  @base
end

#engineObject (readonly)

Returns the value of attribute engine.



5
6
7
# File 'lib/ransack/context.rb', line 5

def engine
  @engine
end

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/ransack/context.rb', line 5

def klass
  @klass
end

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/ransack/context.rb', line 5

def object
  @object
end

#searchObject (readonly)

Returns the value of attribute search.



5
6
7
# File 'lib/ransack/context.rb', line 5

def search
  @search
end

Class Method Details

.for(object, options = {}) ⇒ Object



10
11
12
13
# File 'lib/ransack/context.rb', line 10

def for(object, options = {})
  context = Class === object ? for_class(object, options) : for_object(object, options)
  context or raise ArgumentError, "Don't know what context to use for #{object}"
end

.for_class(klass, options = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ransack/context.rb', line 15

def for_class(klass, options = {})
  if klass < ActiveRecord::Base
    Adapters::ActiveRecord::Context.new(klass, options)
  end
end

.for_object(object, options = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/ransack/context.rb', line 21

def for_object(object, options = {})
  case object
  when ActiveRecord::Relation
    Adapters::ActiveRecord::Context.new(object.klass, options)
  end
end

Instance Method Details

#association_path(str, base = @base) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ransack/context.rb', line 78

def association_path(str, base = @base)
  base = klassify(base)
  str ||= ''
  path = []
  segments = str.split(/_/)
  association_parts = []
  if (segments = str.split(/_/)).size > 0
    while segments.size > 0 && !base.columns_hash[segments.join('_')] && association_parts << segments.shift do
      assoc, klass = unpolymorphize_association(association_parts.join('_'))
      if found_assoc = get_association(assoc, base)
        path += association_parts
        association_parts = []
        base = klassify(klass || found_assoc)
      end
    end
  end

  path.join('_')
end

#bind(object, str) ⇒ Object



54
55
56
# File 'lib/ransack/context.rb', line 54

def bind(object, str)
  object.parent, object.attr_name = @bind_pairs[str]
end

#contextualize(str) ⇒ Object

Convert a string representing a chain of associations and an attribute into the attribute itself



49
50
51
52
# File 'lib/ransack/context.rb', line 49

def contextualize(str)
  parent, attr_name = @bind_pairs[str]
  table_for(parent)[attr_name]
end

#ransackable_association?(str, klass) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/ransack/context.rb', line 110

def ransackable_association?(str, klass)
  klass.ransackable_associations(auth_object).include? str
end

#ransackable_attribute?(str, klass) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/ransack/context.rb', line 106

def ransackable_attribute?(str, klass)
  klass.ransackable_attributes(auth_object).include? str
end

#searchable_associations(str = '') ⇒ Object



118
119
120
# File 'lib/ransack/context.rb', line 118

def searchable_associations(str = '')
  traverse(str).ransackable_associations(auth_object)
end

#searchable_attributes(str = '') ⇒ Object



114
115
116
# File 'lib/ransack/context.rb', line 114

def searchable_attributes(str = '')
  traverse(str).ransackable_attributes(auth_object)
end

#traverse(str, base = @base) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ransack/context.rb', line 58

def traverse(str, base = @base)
  str ||= ''

  if (segments = str.split(/_/)).size > 0
    association_parts = []
    found_assoc = nil
    while !found_assoc && segments.size > 0 && association_parts << segments.shift do
      # Strip the _of_Model_type text from the association name, but hold
      # onto it in klass, for use as the next base
      assoc, klass = unpolymorphize_association(association_parts.join('_'))
      if found_assoc = get_association(assoc, base)
        base = traverse(segments.join('_'), klass || found_assoc.klass)
      end
    end
    raise UntraversableAssociationError, "No association matches #{str}" unless found_assoc
  end

  klassify(base)
end

#unpolymorphize_association(str) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/ransack/context.rb', line 98

def unpolymorphize_association(str)
  if (match = str.match(/_of_([^_]+?)_type$/))
    [match.pre_match, Kernel.const_get(match.captures.first)]
  else
    [str, nil]
  end
end