module ActiveFlags

require_relative 'flaggable'

Constants

ACTIVE_FLAGS_PREFIX
VERSION

Public Instance Methods

has_flags(*authorized_flags) click to toggle source
# File lib/active_flags.rb, line 12
def has_flags(*authorized_flags)
  has_many :flags_as_collection, class_name: 'ActiveFlags::Flag', as: :subject

  define_method(:flags=) do |flags|
    Handler::FlagMapper.remap(authorized_flags, flags.symbolize_keys).each do |flag_attributes|
      Handler::FlagBuilder.new(self, flag_attributes).save
    end
  end

  define_method(:flags) do
    hash_of_flags = {}
    flags_as_collection.each do |flag|
      hash_of_flags[flag.key.to_sym] = flag.converted_value
    end
    hash_of_flags.with_indifferent_access
  end
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/active_flags.rb, line 34
def method_missing(method_name, *args, &block) 
  return super unless method_name.to_s.include?(prefix = ACTIVE_FLAGS_PREFIX)
  different_from = method_name.to_s.starts_with?('not')
  flag = method_name.to_s.gsub(different_from ? "not_#{prefix}" : prefix, '')
  result = joins(:flags_as_collection)
    .where(active_flags_flags: { key: flag, value: stringify(args[0]) })

  different_from ? where.not(id: result.pluck(:id)) : result
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/active_flags.rb, line 30
def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.include?(ACTIVE_FLAGS_PREFIX) || super
end