5
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
|
# File 'lib/bitwise_attribute/active_record_methods.rb', line 5
def define_named_scopes(name, column_name, mapping)
define_singleton_method("with_#{name}") do |*keys|
keys = cleanup_keys(keys, mapping)
return none unless keys&.any?
records = all
keys.each do |key|
records = records.where((arel_table[column_name] & mapping[key]).eq(mapping[key]))
end
records
end
define_singleton_method("with_any_#{name}") do |*keys|
keys = cleanup_keys(keys, mapping)
return where.not(column_name => nil) unless keys&.any?
records = where('1=0')
keys.each do |key|
records = records.or(where((arel_table[column_name] & mapping[key]).eq(mapping[key])))
end
records
end
define_singleton_method("with_exact_#{name}") do |*keys|
keys = cleanup_keys(keys, mapping)
return none unless keys&.any?
records = send("with_#{name}", keys)
(mapping.keys - keys).each do |key|
records = records.where((arel_table[column_name] & mapping[key]).not_eq(mapping[key]))
end
records
end
define_singleton_method("without_#{name}") do |*keys|
keys = cleanup_keys(keys, mapping)
return where(column_name => nil).or(where(column_name => 0)) unless keys&.any?
records = all
keys.each do |key|
records = records.where((arel_table[column_name] & mapping[key]).not_eq(mapping[key]))
end
records
end
mapping.each_key do |key|
define_singleton_method(key) do
send("with_#{name}", key)
end
end
end
|