Class: Ransack::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/ransack/context.rb,
lib/ransack/adapters/mongoid/ransack/context.rb,
lib/ransack/adapters/active_record/ransack/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

<< self



32
33
34
# File 'lib/ransack/context.rb', line 32

def initialize(object, options = {})
  raise "not implemented"
end

Instance Attribute Details

#arel_visitorObject (readonly)

Returns the value of attribute arel_visitor.



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

def arel_visitor
  @arel_visitor
end

#auth_objectObject

Returns the value of attribute auth_object.



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

def auth_object
  @auth_object
end

#baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

#engineObject (readonly)

Returns the value of attribute engine.



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

def engine
  @engine
end

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

#searchObject (readonly)

Returns the value of attribute search.



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

def search
  @search
end

#search_keyObject

Returns the value of attribute search_key.



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

def search_key
  @search_key
end

Class Method Details

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



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

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

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



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

def for_class(klass, options = {})
  raise "not implemented"
end

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



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

def for_object(object, options = {})
  raise "not implemented"
end

Instance Method Details

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ransack/context.rb', line 92

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

  path.join(Constants::UNDERSCORE)
end

#bind(object, str) ⇒ Object



60
61
62
63
# File 'lib/ransack/context.rb', line 60

def bind(object, str)
  return nil unless str
  object.parent, object.attr_name = bind_pair_for(str)
end

#bind_pair_for(key) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/ransack/adapters/mongoid/ransack/context.rb', line 35

def bind_pair_for(key)
  @bind_pairs ||= {}

  @bind_pairs[key] ||= begin
    parent, attr_name = get_parent_and_attribute_name(key.to_s)
    [parent, attr_name] if parent && attr_name
  end
end

#chain_scope(scope, args) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/ransack/context.rb', line 47

def chain_scope(scope, args)
  return unless @klass.method(scope) && args != false
  @object = if scope_arity(scope) < 1 && args == true
              @object.public_send(scope)
            else
              @object.public_send(scope, *args)
            end
end

#contextualize(str) ⇒ Object

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



42
43
44
45
# File 'lib/ransack/context.rb', line 42

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

#klassify(obj) ⇒ Object



36
37
38
# File 'lib/ransack/context.rb', line 36

def klassify(obj)
  raise "not implemented"
end

#ransackable_alias(str) ⇒ Object



124
125
126
# File 'lib/ransack/context.rb', line 124

def ransackable_alias(str)
  klass._ransack_aliases.fetch(str, str)
end

#ransackable_association?(str, klass) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/ransack/context.rb', line 133

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

#ransackable_attribute?(str, klass) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
# File 'lib/ransack/context.rb', line 128

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

#ransackable_scope?(str, klass) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/ransack/context.rb', line 137

def ransackable_scope?(str, klass)
  klass.ransackable_scopes(auth_object).any? { |s| s.to_sym == str.to_sym }
end

#scope_arity(scope) ⇒ Object



56
57
58
# File 'lib/ransack/context.rb', line 56

def scope_arity(scope)
  @klass.method(scope).arity
end

#searchable_associations(str = ''.freeze) ⇒ Object



149
150
151
# File 'lib/ransack/context.rb', line 149

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

#searchable_attributes(str = ''.freeze) ⇒ Object



141
142
143
# File 'lib/ransack/context.rb', line 141

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

#sortable_attributes(str = ''.freeze) ⇒ Object



145
146
147
# File 'lib/ransack/context.rb', line 145

def sortable_attributes(str = ''.freeze)
  traverse(str).ransortable_attributes(auth_object)
end

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ransack/context.rb', line 65

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

  if (segments = str.split(Constants::UNDERSCORE)).size > 0
    remainder = []
    found_assoc = nil
    while !found_assoc && segments.size > 0 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(
        segments.join(Constants::UNDERSCORE)
        )
      if found_assoc = get_association(assoc, base)
        base = traverse(
          remainder.join(Constants::UNDERSCORE), klass || found_assoc.klass
          )
      end

      remainder.unshift segments.pop
    end
    raise UntraversableAssociationError,
      "No association matches #{str}" unless found_assoc
  end

  klassify(base)
end

#unpolymorphize_association(str) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/ransack/context.rb', line 116

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