Module: TheWhere
- Includes:
- Like, Not, Order, Range
- Defined in:
- lib/the_where.rb,
lib/the_where/not.rb,
lib/the_where/like.rb,
lib/the_where/order.rb,
lib/the_where/range.rb,
lib/the_where/version.rb
Defined Under Namespace
Modules: Like, Not, Order, Range
Constant Summary
collapse
- REJECT =
['', ' ', nil]
- STRIP =
true
- VERSION =
"0.1.0"
Constants included
from Range
Range::PATTERN
Instance Method Summary
collapse
Methods included from Like
#filter_like, #like_scope
Methods included from Order
#filter_order, #order_scope
Methods included from Range
#filter_range, #range_scope
Methods included from Not
#filter_not, #not_scope
Instance Method Details
#params_with_table(params = {}, options = {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/the_where.rb', line 34
def params_with_table(params = {}, options = {})
if options[:reject]
default_reject = [options[:reject]].flatten
elsif options[:allow]
default_reject = REJECT - [options[:allow]].flatten
else
default_reject = REJECT
end
unless options.has_key? :strip
options[:strip] = STRIP
end
params = params.to_h
params.stringify_keys!
params.reject! { |_, value| default_reject.include?(value) }
refs = []
tables = []
final_params = {}
params.each do |key, value|
value = value.strip if value.is_a?(String) && options[:strip]
if key =~ /\./
table, col = key.split('.')
as_model = reflections[table]
f_col, _ = col.split('-')
if as_model && as_model.klass.column_names.include?(f_col)
final_params["#{as_model.table_name}.#{col}"] = value
tables << as_model.table_name
refs << table.to_sym
elsif connection.data_sources.include? table
final_params["#{table}.#{col}"] = value
tables << table
keys = reflections.select { |_, v| !v.polymorphic? && v.table_name == table }.keys
if keys && keys.size == 1
refs << keys.first.to_sym
end
end
else
f_key, _ = key.split('-')
if column_names.include?(f_key)
final_params["#{table_name}.#{key}"] = value
end
end
end
[final_params, refs, tables]
end
|
#the_where(params = {}, options = {}) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/the_where.rb', line 15
def the_where(params = {}, options = {})
return all if params.blank?
params, refs, tables = params_with_table(params, options)
range_params = filter_range(params)
order_params = filter_order(params)
not_params = filter_not(params)
like_params = filter_like(params)
equal_params = params.except!(*range_params.keys, *order_params.keys, *not_params.keys, *like_params.keys)
includes(refs).where(equal_params).references(tables)
.not_scope(not_params)
.like_scope(like_params)
.range_scope(range_params)
.order_scope(order_params)
end
|