module FlagShihTzu
Constants
- DEFAULT_COLUMN_NAME
- TRUE_VALUES
taken from ActiveRecord::ConnectionAdapters::Column
- VERSION
Public Class Methods
included(base)
click to toggle source
# File lib/flag_shih_tzu.rb, line 10 def self.included(base) base.extend(ClassMethods) base.class_attribute :flag_options unless defined?(base.flag_options) base.class_attribute :flag_mapping unless defined?(base.flag_mapping) base.class_attribute :flag_columns unless defined?(base.flag_columns) end
Public Instance Methods
all_flags(colmn = DEFAULT_COLUMN_NAME)
click to toggle source
# File lib/flag_shih_tzu.rb, line 505 def all_flags(colmn = DEFAULT_COLUMN_NAME) flag_mapping[colmn].keys end
as_flag_collection(colmn = DEFAULT_COLUMN_NAME, *args)
click to toggle source
Use with a checkbox form builder, like rails' or simple_form's :selected_flags, used in the example below, is a method defined
by flag_shih_tzu for bulk setting flags like this: form_for @user do |f| f.collection_check_boxes(:selected_flags, f.object.as_flag_collection("flags", :sent_warm_up_email, :not_follow_up_called), :first, :last) end
# File lib/flag_shih_tzu.rb, line 603 def as_flag_collection(colmn = DEFAULT_COLUMN_NAME, *args) flags_to_collect = args.empty? ? all_flags(colmn) : args collect_flags(*flags_to_collect) do |memo, flag| memo << [flag, flag_enabled?(flag, colmn)] end end
chained_flags_with_signature(colmn = DEFAULT_COLUMN_NAME, *args)
click to toggle source
Use with chained_flags_with to find records with specific flags
set to the same values as on this record.
For a record that has sent_warm_up_email = true and the other flags false:
user.chained_flags_with_signature => [:sent_warm_up_email, :not_follow_up_called, :not_sent_final_email, :not_scheduled_appointment] User.chained_flags_with("flags", *user.chained_flags_with_signature) => the set of Users that have the same flags set as user.
# File lib/flag_shih_tzu.rb, line 578 def chained_flags_with_signature(colmn = DEFAULT_COLUMN_NAME, *args) flags_to_collect = args.empty? ? all_flags(colmn) : args truthy_and_chosen = selected_flags(colmn). select { |flag| flags_to_collect.include?(flag) } truthy_and_chosen.concat( collect_flags(*flags_to_collect) do |memo, flag| memo << "not_#{flag}".to_sym unless truthy_and_chosen.include?(flag) end ) end
disable_flag(flag, colmn = nil)
click to toggle source
Performs the bitwise operation so the flag will return false
.
# File lib/flag_shih_tzu.rb, line 476 def disable_flag(flag, colmn = nil) colmn = determine_flag_colmn_for(flag) if colmn.nil? self.class.check_flag(flag, colmn) set_flags(flags(colmn) & ~self.class.flag_mapping[colmn][flag], colmn) end
enable_flag(flag, colmn = nil)
click to toggle source
Performs the bitwise operation so the flag will return true
.
# File lib/flag_shih_tzu.rb, line 468 def enable_flag(flag, colmn = nil) colmn = determine_flag_colmn_for(flag) if colmn.nil? self.class.check_flag(flag, colmn) set_flags(flags(colmn) | self.class.flag_mapping[colmn][flag], colmn) end
flag_disabled?(flag, colmn = nil)
click to toggle source
# File lib/flag_shih_tzu.rb, line 490 def flag_disabled?(flag, colmn = nil) colmn = determine_flag_colmn_for(flag) if colmn.nil? self.class.check_flag(flag, colmn) !flag_enabled?(flag, colmn) end
flag_enabled?(flag, colmn = nil)
click to toggle source
# File lib/flag_shih_tzu.rb, line 483 def flag_enabled?(flag, colmn = nil) colmn = determine_flag_colmn_for(flag) if colmn.nil? self.class.check_flag(flag, colmn) get_bit_for(flag, colmn) == 0 ? false : true end
flags(colmn = DEFAULT_COLUMN_NAME)
click to toggle source
# File lib/flag_shih_tzu.rb, line 497 def flags(colmn = DEFAULT_COLUMN_NAME) self[colmn] || 0 end
has_flag?(colmn = DEFAULT_COLUMN_NAME)
click to toggle source
# File lib/flag_shih_tzu.rb, line 539 def has_flag?(colmn = DEFAULT_COLUMN_NAME) not selected_flags(colmn).empty? end
select_all_flags(colmn = DEFAULT_COLUMN_NAME)
click to toggle source
# File lib/flag_shih_tzu.rb, line 527 def select_all_flags(colmn = DEFAULT_COLUMN_NAME) all_flags(colmn).each do |flag| enable_flag(flag, colmn) end end
selected_flags(colmn = DEFAULT_COLUMN_NAME)
click to toggle source
# File lib/flag_shih_tzu.rb, line 509 def selected_flags(colmn = DEFAULT_COLUMN_NAME) all_flags(colmn). map { |flag_name| self.send(flag_name) ? flag_name : nil }. compact end
selected_flags=(chosen_flags)
click to toggle source
Useful for a form builder use selected_#{column}= for custom column names.
# File lib/flag_shih_tzu.rb, line 517 def selected_flags=(chosen_flags) unselect_all_flags return if chosen_flags.nil? chosen_flags.each do |selected_flag| if selected_flag.present? enable_flag(selected_flag.to_sym, DEFAULT_COLUMN_NAME) end end end
set_flags(value, colmn = DEFAULT_COLUMN_NAME)
click to toggle source
# File lib/flag_shih_tzu.rb, line 501 def set_flags(value, colmn = DEFAULT_COLUMN_NAME) self[colmn] = value end
unselect_all_flags(colmn = DEFAULT_COLUMN_NAME)
click to toggle source
# File lib/flag_shih_tzu.rb, line 533 def unselect_all_flags(colmn = DEFAULT_COLUMN_NAME) all_flags(colmn).each do |flag| disable_flag(flag, colmn) end end
update_flag!(flag, value, update_instance = false)
click to toggle source
returns true if successful third parameter allows you to specify that `self` should
also have its in-memory flag attribute updated.
# File lib/flag_shih_tzu.rb, line 546 def update_flag!(flag, value, update_instance = false) truthy = FlagShihTzu::TRUE_VALUES.include?(value) sql = self.class.set_flag_sql(flag.to_sym, truthy) if update_instance if truthy enable_flag(flag) else disable_flag(flag) end end if (ActiveRecord::VERSION::MAJOR <= 3) self.class. update_all(sql, self.class.primary_key => id) == 1 else self.class. where("#{self.class.primary_key} = ?", id). update_all(sql) == 1 end end
Private Instance Methods
collect_flags(*args) { |memo, flag| ... }
click to toggle source
# File lib/flag_shih_tzu.rb, line 612 def collect_flags(*args) args.inject([]) do |memo, flag| yield memo, flag memo end end
determine_flag_colmn_for(flag)
click to toggle source
# File lib/flag_shih_tzu.rb, line 623 def determine_flag_colmn_for(flag) self.class.determine_flag_colmn_for(flag) end
get_bit_for(flag, colmn)
click to toggle source
# File lib/flag_shih_tzu.rb, line 619 def get_bit_for(flag, colmn) flags(colmn) & self.class.flag_mapping[colmn][flag] end