Class: Deli::Adapters::ActiveRecord::Param
- Inherits:
-
Param
- Object
- Param
- Deli::Adapters::ActiveRecord::Param
show all
- Defined in:
- lib/deli/adapters/active_record.rb
Instance Attribute Summary collapse
Attributes inherited from Param
#controller, #default, #exact, #key, #model_name, #namespace
Instance Method Summary
collapse
Methods inherited from Param
#inspect, #parse_value
Constructor Details
#initialize(key, options = {}) ⇒ Param
Returns a new instance of Param.
101
102
103
104
|
# File 'lib/deli/adapters/active_record.rb', line 101
def initialize(key, options = {})
super(key, options)
configure_clauses(options)
end
|
Instance Attribute Details
#table_key ⇒ Object
Returns the value of attribute table_key.
99
100
101
|
# File 'lib/deli/adapters/active_record.rb', line 99
def table_key
@table_key
end
|
Instance Method Details
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/deli/adapters/active_record.rb', line 110
def configure_clauses(options)
case options[:to]
when ::String
self.table_key = options[:to]
when ::Hash
join_key = options[:as] || options[:to].keys.first
join_class = join_key.to_s.singularize.camelize.constantize
attribute = options[:to].values.first.to_sym
self.table_key = "#{join_class.table_name}.#{attribute}"
else
if model_class.respond_to?(:table_name)
if association = find_association
self.table_key = "#{model_class.table_name}.#{key}_id"
else
self.table_key = "#{model_class.table_name}.#{key}"
end
else
self.table_key = key
end
end
end
|
#find_association ⇒ Object
137
138
139
140
141
142
|
# File 'lib/deli/adapters/active_record.rb', line 137
def find_association
key = self.key.to_sym
model_class.reflect_on_all_associations.detect do |association|
association.name == key
end
end
|
#match_operator ⇒ Object
186
187
188
|
# File 'lib/deli/adapters/active_record.rb', line 186
def match_operator
postgres? ? "ILIKE" : "LIKE"
end
|
#operators ⇒ Object
133
134
135
|
# File 'lib/deli/adapters/active_record.rb', line 133
def operators
[:equals, :contains, :start, :end]
end
|
#parenthesize!(items) ⇒ Object
190
191
192
193
|
# File 'lib/deli/adapters/active_record.rb', line 190
def parenthesize!(items)
items.map! { |i| "(#{i})" } if items.length > 1
items
end
|
#parse(value) ⇒ Object
106
107
108
|
# File 'lib/deli/adapters/active_record.rb', line 106
def parse(value)
[value]
end
|
#postgres? ⇒ Boolean
195
196
197
198
199
200
|
# File 'lib/deli/adapters/active_record.rb', line 195
def postgres?
if @postgres.nil?
@postgres = defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ::ActiveRecord::Base.connection.is_a?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
end
@postgres
end
|
#render(value) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/deli/adapters/active_record.rb', line 144
def render(value)
keys, values = [], []
parse(value).each do |and_collection|
and_keys = []
and_collection.each do |item|
and_keys << "#{table_key} #{render_operator(item[:operators][0])} ?"
values << render_value(item[:value], item[:operators][1] || item[:operators][0])
end
keys << and_keys
end
keys.map! { |and_keys| and_keys.join(" AND ") }
if keys.length > 1
[parenthesize!(keys).join(" OR "), *values]
else
[keys.join(" OR "), *values]
end
end
|
#render_name(*names) ⇒ Object
167
168
169
|
# File 'lib/deli/adapters/active_record.rb', line 167
def render_name(*names)
names.flatten.compact.join(".")
end
|
#render_operator(value) ⇒ Object
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/deli/adapters/active_record.rb', line 175
def render_operator(value)
case value
when "=~"
match_operator
when "!~"
"NOT #{match_operator}"
else
value
end
end
|
#render_value(value, operator = nil) ⇒ Object
171
172
173
|
# File 'lib/deli/adapters/active_record.rb', line 171
def render_value(value, operator = nil)
value
end
|