module BitwiseAttribute::ActiveRecordMethods
Public Instance Methods
cleanup_keys(keys, mapping)
click to toggle source
# File lib/bitwise_attribute/active_record_methods.rb, line 71 def cleanup_keys(keys, mapping) return [] unless keys clean = keys.flatten.map(&:to_sym).compact.uniq clean & mapping.keys # only roles that we know about end
define_named_scopes(name, column_name, mapping)
click to toggle source
# 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 [] unless keys&.any? records = where("#{column_name} & #{mapping[keys.first]} = #{mapping[keys.first]}") keys[1..-1].each do |key| records = records.where("#{column_name} & #{mapping[key]} = #{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("#{column_name} & #{mapping[keys.first]} = #{mapping[keys.first]}") keys[1..-1].each do |key| records = records.or(where("#{column_name} & #{mapping[key]} = #{mapping[key]}")) end records end define_singleton_method("with_exact_#{name}") do |*keys| keys = cleanup_keys(keys, mapping) return [] unless keys&.any? records = send("with_#{name}", keys) (mapping.keys - keys).each do |key| records = records.where("#{column_name} & #{mapping[key]} != #{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 = where("#{column_name} & #{mapping[keys.first]} != #{mapping[keys.first]}") keys[1..-1].each do |key| records = records.where("#{column_name} & #{mapping[key]} != #{mapping[key]}") end records end # Defines a class method for each key of the mapping, returning records that have *at least* # the corresponding value. mapping.each_key do |key| define_singleton_method(key) do send("with_#{name}", key) end end end