Module: FlagShihTzu

Defined in:
lib/flag_shih_tzu.rb,
lib/flag_shih_tzu/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: DuplicateFlagColumnException, IncorrectFlagColumnException, NoSuchFlagException, NoSuchFlagQueryModeException

Constant Summary collapse

TRUE_VALUES =

taken from ActiveRecord::ConnectionAdapters::Column

[true, 1, "1", "t", "T", "true", "TRUE"]
DEFAULT_COLUMN_NAME =
"flags"
VERSION =
"0.3.23"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


10
11
12
13
14
15
# 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

Instance Method Details

#all_flags(colmn = DEFAULT_COLUMN_NAME) ⇒ Object


505
506
507
# 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) ⇒ Object

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

603
604
605
606
607
608
# 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) ⇒ Object

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.

578
579
580
581
582
583
584
585
586
587
588
# 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) ⇒ Object

Performs the bitwise operation so the flag will return false.


476
477
478
479
480
481
# 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) ⇒ Object

Performs the bitwise operation so the flag will return true.


468
469
470
471
472
473
# 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) ⇒ Boolean

Returns:

  • (Boolean)

490
491
492
493
494
495
# 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) ⇒ Boolean

Returns:

  • (Boolean)

483
484
485
486
487
488
# 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) ⇒ Object


497
498
499
# File 'lib/flag_shih_tzu.rb', line 497

def flags(colmn = DEFAULT_COLUMN_NAME)
  self[colmn] || 0
end

#has_flag?(colmn = DEFAULT_COLUMN_NAME) ⇒ Boolean

Returns:

  • (Boolean)

539
540
541
# 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) ⇒ Object


527
528
529
530
531
# 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) ⇒ Object


509
510
511
512
513
# 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) ⇒ Object

Useful for a form builder use selected_#column= for custom column names.


517
518
519
520
521
522
523
524
525
# 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) ⇒ Object


501
502
503
# 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) ⇒ Object


533
534
535
536
537
# 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) ⇒ Object

returns true if successful third parameter allows you to specify that `self` should

also have its in-memory flag attribute updated.

546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# 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