users: refactor bit_prefs search.
This commit is contained in:
@@ -5,7 +5,7 @@ module ApproverPruner
|
||||
MINIMUM_APPROVALS = 30
|
||||
|
||||
def inactive_approvers
|
||||
approvers = User.where("bit_prefs & ? > 0", User.flag_value_for("can_approve_posts"))
|
||||
approvers = User.bit_prefs_match(:can_approve_posts, true)
|
||||
approvers = approvers.where("level < ?", User::Levels::MODERATOR)
|
||||
|
||||
recently_promoted_approvers = UserFeedback.where("created_at >= ?", APPROVAL_PERIOD.ago).where_like(:body, "*You gained the ability to approve posts*").select(:user_id)
|
||||
|
||||
@@ -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