users: refactor bit_prefs search.
This commit is contained in:
@@ -5,18 +5,10 @@ module Danbooru
|
||||
module ClassMethods
|
||||
# NOTE: the ordering of attributes has to be fixed#
|
||||
# new attributes should be appended to the end.
|
||||
def has_bit_flags(attributes, options = {})
|
||||
field = options[:field] || :bit_flags
|
||||
|
||||
def has_bit_flags(attributes, field: :bit_flags)
|
||||
attributes.each.with_index do |attribute, i|
|
||||
bit_flag = 1 << i
|
||||
|
||||
define_singleton_method("flag_value_for") do |key|
|
||||
index = attributes.index(key)
|
||||
raise IndexError if index.nil?
|
||||
1 << index
|
||||
end
|
||||
|
||||
define_method(attribute) do
|
||||
send(field) & bit_flag > 0
|
||||
end
|
||||
@@ -33,6 +25,15 @@ module Danbooru
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# bit_prefs_match
|
||||
define_singleton_method("#{field}_match") do |flag, value|
|
||||
value = value ? 1 : 0
|
||||
bits = attributes.length
|
||||
bit_index = bits - attributes.index(flag.to_s) - 1
|
||||
|
||||
where(sanitize_sql(["get_bit(bit_prefs::bit(?), ?) = ?", bits, bit_index, value]))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user