No commit activity in last 3 years
No release in over 3 years
Add binary flag attributes to AR model using one DB field.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.6.4
~> 2.3.0

Runtime

 Project Readme

ar_binary_flag_attributes¶ ↑

Adds many binary/flag attributes to your AR model using only one integer field.

How to use¶ ↑

Simply as hell :) First, add integer column called ‘flags’ (default) or whatever you like.

class AddFlagsToUserMigration < ActiveRecord::Migration
  def self.run
    add_column :users, :flags, :integer
  end
end

Then add one line to your model so it should look like this:

class User < ActiveRecord::Base
  binary_attributes [:a, :b, :c]
end

Custom ‘flags’ field¶ ↑

You can divide all flags into more integer columns, or use non default column name.

class AnotherMigration < ActiveRecord::Migration
  def self.run
    add_column :phones, :features, :integer
    add_column :phones, :minor_features, :integer
  end
end

class Phone < ActiveRecord::Base
  binary_attributes [:camera, :wlan, :touchscreen], :features
  binary_attributes [:sd_card_slot, :qwerty_keyboard], :minor_features
end

And try:

p = Phone.new
p.camera = true
p.qwerty_keyboard = true
p.save

p.features => 1
p.minor_features => 2

Custom masks¶ ↑

You can specify custom masks if you want to:

class Building < ActiveRecord::Base
  flag_attributes(
    "overdriven"                => 0b00000001,
    "with_points"               => 0b00000010
  )
end

Check also¶ ↑

There is a more powerful tool here github.com/pboling/flag_shih_tzu

Author¶ ↑

Core author - Artūras Šlajus (github.com/arturaz/ar_binary_flag_attributes/blob/master/init.rb). I just added spec, and made a gem :]

Copyright © 2011, 2012 Artūras Šlajus, Aleksander Kwiatkowski. See LICENSE.txt for further details.