6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
85
86
87
88
|
# File 'lib/default_where/params.rb', line 6
def default_where_params(params = {}, options = {})
refs = []
tables = []
final_params = {}
params.each do |key, value|
strip = options.fetch(:strip, {}).fetch(key, STRIP)
value = value.strip if value.is_a?(String) && strip
if options.key?(:reject)
reject = options.fetch(:reject, {}).fetch(key, REJECT)
if reject == nil
reject = [nil]
elsif reject == []
reject = [[]]
else
reject = Array(reject)
end
else
allow = options.fetch(:allow, {}).fetch(key, [])
if allow == nil
allow = [nil]
else
allow = Array(allow)
end
reject = REJECT - allow
end
next if reject.include?(value)
items = key.to_s.split('.')
column = items[-1]
real_column = column.split(/[-\/]/)[0]
if items.size == 1
next unless column_names.include?(real_column)
table = nil
else
prefix = items[0]
ref = reflections[prefix]
if ref && !ref.polymorphic?
table = ref.table_name
elsif connection.data_sources.include?(prefix) && connection.column_exists?(prefix, real_column)
possible_refs = reflections.select { |_, v| v.table_name == prefix }
if possible_refs.size < 1
ref = nil
elsif possible_refs.size == 1
ref = possible_refs[0]
else
raise "#{key} makes confused, please use reflection name!"
end
table = prefix
else
next
end
if ref && !ref.klass.column_names.include?(real_column)
next
end
if ref && !refs.include?(ref.name)
refs << ref.name
end
unless tables.include?(table)
tables << table
end
end
if table
final_params["#{table}.#{column}"] = value
else
final_params[column] = value
end
end
[final_params, refs, tables]
end
|