Class: Ransack::Context
- Inherits:
-
Object
show all
- Defined in:
- lib/ransack/context.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(object, options = {}) ⇒ Context
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/ransack/context.rb', line 37
def initialize(object, options = {})
@object = relation_for(object)
@klass = @object.klass
@join_dependency = join_dependency(@object)
@join_type = options[:join_type] || Polyamorous::OuterJoin
@search_key = options[:search_key] || Ransack.options[:search_key]
@associations_pot = {}
@tables_pot = {}
@lock_associations = []
@base = @join_dependency.instance_variable_get(:@join_root)
end
|
Instance Attribute Details
#arel_visitor ⇒ Object
Returns the value of attribute arel_visitor.
5
6
7
|
# File 'lib/ransack/context.rb', line 5
def arel_visitor
@arel_visitor
end
|
#auth_object ⇒ Object
Returns the value of attribute auth_object.
6
7
8
|
# File 'lib/ransack/context.rb', line 6
def auth_object
@auth_object
end
|
#base ⇒ Object
Returns the value of attribute base.
5
6
7
|
# File 'lib/ransack/context.rb', line 5
def base
@base
end
|
#engine ⇒ Object
Returns the value of attribute engine.
5
6
7
|
# File 'lib/ransack/context.rb', line 5
def engine
@engine
end
|
#klass ⇒ Object
Returns the value of attribute klass.
5
6
7
|
# File 'lib/ransack/context.rb', line 5
def klass
@klass
end
|
#object ⇒ Object
Returns the value of attribute object.
5
6
7
|
# File 'lib/ransack/context.rb', line 5
def object
@object
end
|
#search ⇒ Object
Returns the value of attribute search.
5
6
7
|
# File 'lib/ransack/context.rb', line 5
def search
@search
end
|
#search_key ⇒ Object
Returns the value of attribute search_key.
6
7
8
|
# File 'lib/ransack/context.rb', line 6
def search_key
@search_key
end
|
Class Method Details
.for(object, options = {}) ⇒ Object
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/ransack/context.rb', line 24
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
14
15
|
# File 'lib/ransack/context.rb', line 11
def for_class(klass, options = {})
if klass < ActiveRecord::Base
Adapters::ActiveRecord::Context.new(klass, options)
end
end
|
.for_object(object, options = {}) ⇒ Object
17
18
19
20
21
22
|
# File 'lib/ransack/context.rb', line 17
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/ransack/context.rb', line 123
def association_path(str, base = @base)
base = klassify(base)
str ||= ''.freeze
path = []
segments = str.split(Constants::UNDERSCORE)
association_parts = []
unless segments.empty?
while !segments.empty? &&
!base.columns_hash[segments.join(Constants::UNDERSCORE)] &&
association_parts << segments.shift
assoc, klass = unpolymorphize_association(
association_parts.join(Constants::UNDERSCORE)
)
next unless found_assoc = get_association(assoc, base)
path += association_parts
association_parts = []
base = klassify(klass || found_assoc)
end
end
path.join(Constants::UNDERSCORE)
end
|
#bind(object, str) ⇒ Object
89
90
91
92
|
# File 'lib/ransack/context.rb', line 89
def bind(object, str)
return nil unless str
object.parent, object.attr_name = bind_pair_for(str)
end
|
#bind_pair_for(key) ⇒ Object
50
51
52
53
54
55
56
57
|
# File 'lib/ransack/context.rb', line 50
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
76
77
78
79
80
81
82
83
|
# File 'lib/ransack/context.rb', line 76
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
71
72
73
74
|
# File 'lib/ransack/context.rb', line 71
def contextualize(str)
parent, attr_name = bind_pair_for(str)
table_for(parent)[attr_name]
end
|
#klassify(obj) ⇒ Object
59
60
61
62
63
64
65
66
67
|
# File 'lib/ransack/context.rb', line 59
def klassify(obj)
if Class === obj && ::ActiveRecord::Base > obj
obj
elsif obj.respond_to? :klass
obj.klass
else
raise ArgumentError, "Don't know how to klassify #{obj.inspect}"
end
end
|
#ransackable_alias(str) ⇒ Object
154
155
156
|
# File 'lib/ransack/context.rb', line 154
def ransackable_alias(str)
klass._ransack_aliases.fetch(str, klass._ransack_aliases.fetch(str.to_sym, str))
end
|
#ransackable_association?(str, klass) ⇒ Boolean
163
164
165
|
# File 'lib/ransack/context.rb', line 163
def ransackable_association?(str, klass)
klass.ransackable_associations(auth_object).any? { |s| s.to_sym == str.to_sym }
end
|
#ransackable_attribute?(str, klass) ⇒ Boolean
158
159
160
161
|
# File 'lib/ransack/context.rb', line 158
def ransackable_attribute?(str, klass)
klass.ransackable_attributes(auth_object).any? { |s| s.to_sym == str.to_sym } ||
klass.ransortable_attributes(auth_object).any? { |s| s.to_sym == str.to_sym }
end
|
#ransackable_scope?(str, klass) ⇒ Boolean
167
168
169
|
# File 'lib/ransack/context.rb', line 167
def ransackable_scope?(str, klass)
klass.ransackable_scopes(auth_object).any? { |s| s.to_sym == str.to_sym }
end
|
#ransackable_scope_skip_sanitize_args?(str, klass) ⇒ Boolean
171
172
173
|
# File 'lib/ransack/context.rb', line 171
def ransackable_scope_skip_sanitize_args?(str, klass)
klass.ransackable_scopes_skip_sanitize_args.any? { |s| s.to_sym == str.to_sym }
end
|
#scope_arity(scope) ⇒ Object
85
86
87
|
# File 'lib/ransack/context.rb', line 85
def scope_arity(scope)
@klass.method(scope).arity
end
|
#searchable_associations(str = ''.freeze) ⇒ Object
183
184
185
|
# File 'lib/ransack/context.rb', line 183
def searchable_associations(str = ''.freeze)
traverse(str).ransackable_associations(auth_object)
end
|
#searchable_attributes(str = ''.freeze) ⇒ Object
175
176
177
|
# File 'lib/ransack/context.rb', line 175
def searchable_attributes(str = ''.freeze)
traverse(str).ransackable_attributes(auth_object)
end
|
#sortable_attributes(str = ''.freeze) ⇒ Object
179
180
181
|
# File 'lib/ransack/context.rb', line 179
def sortable_attributes(str = ''.freeze)
traverse(str).ransortable_attributes(auth_object)
end
|
#traverse(str, base = @base) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/ransack/context.rb', line 94
def traverse(str, base = @base)
str ||= ''.freeze
segments = str.split(Constants::UNDERSCORE)
unless segments.empty?
remainder = []
found_assoc = nil
until found_assoc || segments.empty?
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
unless found_assoc
raise(UntraversableAssociationError,
"No association matches #{str}")
end
end
klassify(base)
end
|
#unpolymorphize_association(str) ⇒ Object
146
147
148
149
150
151
152
|
# File 'lib/ransack/context.rb', line 146
def unpolymorphize_association(str)
if (match = str.match(/_of_([^_]+?)_type$/))
[match.pre_match, Kernel.const_get(match.captures.first)]
else
[str, nil]
end
end
|