FlagShihTzu <img src=“https://secure.travis-ci.org/pboling/flag_shih_tzu.png” /> <img src=“http://api.coderwall.com/pboling/endorsecount.png” />¶ ↑
Bit fields for ActiveRecord
An extension for ActiveRecord to store a collection of boolean attributes in a single integer column as a bit field.
This gem lets you use a single integer column in an ActiveRecord model to store a collection of boolean attributes (flags). Each flag can be used almost in the same way you would use any boolean attribute on an ActiveRecord object.
The benefits:
-
No migrations needed for new boolean attributes. This helps a lot if you have very large db-tables, on which you want to avoid ‘ALTER TABLE` whenever possible.
-
Only the one integer column needs to be indexed.
Using FlagShihTzu, you can add new boolean attributes whenever you want, without needing any migration. Just add a new flag to the has_flags
call.
And just in case you are wondering what a “Shih Tzu” is: en.wikipedia.org/wiki/Shih_Tzu
FlagShihTzu was originally a XING AG project. Peter Boling was a long time contributor and watcher of the project. In September 2012 XING transferred ownership of the project to Peter Boling. Peter Boling had been maintaining a fork with extended capabilities. These additional features become a part of the 0.3 line. The 0.2 line of the gem will remain true to XING’s original. The 0.3 line aims to maintain complete parity and compatibility with XING’s original as well. I will continue to monitor other forks for original ideas and improvements. Pull requests are welcome, but please rebase your work onto the current master to make integration easier.
More information on the changes for 0.3.X: pboling/flag_shih_tzu/wiki/Changes-for-0.3.x
Travis CI: <img src=“https://secure.travis-ci.org/pboling/flag_shih_tzu.png” />
CodeClimate: codeclimate.com/github/pboling/flag_shih_tzu
The gem is actively being tested with:
-
ActiveRecord versions 2.3.x, 3.0.x, 3.1.x, 3.2.x
-
MySQL, PostgreSQL and SQLite3 databases
-
Ruby 1.8.7, 1.9.2 and 1.9.3
In environment.rb:
config.gem 'flag_shih_tzu'
Then:
$ rake gems:install # use sudo if necessary
In Gemfile:
gem 'flag_shih_tzu'
Then:
$ bundle install
FlagShihTzu assumes that your ActiveRecord model already has an integer field to store the flags, which should be defined to not allow NULL values and should have a default value of 0 (which means all flags are initially set to false).
class Spaceship < ActiveRecord::Base include FlagShihTzu has_flags 1 => :warpdrive, 2 => :shields, 3 => :electrolytes end
has_flags
takes a hash. The keys must be positive integers and represent the position of the bit being used to enable or disable the flag. The keys must not be changed once in use, or you will get wrong results. That is why the plugin forces you to set them explicitly. The values are symbols for the flags being created.
As said, FlagShihTzu uses a single integer column to store the values for all the defined flags as a bit field.
The bit position of a flag corresponds to the given key.
This way, we can use bit operators on the stored integer value to set, unset and check individual flags.
+---+---+---+ +---+---+---+ | | | | | | | | Bit position | 3 | 2 | 1 | | 3 | 2 | 1 | (flag key) | | | | | | | | +---+---+---+ +---+---+---+ | | | | | | | | Bit value | 4 | 2 | 1 | | 4 | 2 | 1 | | | | | | | | | +---+---+---+ +---+---+---+ | e | s | w | | e | s | w | | l | h | a | | l | h | a | | e | i | r | | e | i | r | | c | e | p | | c | e | p | | t | l | d | | t | l | d | | r | d | r | | r | d | r | | o | s | i | | o | s | i | | l | | v | | l | | v | | y | | e | | y | | e | | t | | | | t | | | | e | | | | e | | | | s | | | | s | | | +---+---+---+ +---+---+---+ | 1 | 1 | 0 | = 4 + 2 = 6 | 1 | 0 | 1 | = 4 + 1 = 5 +---+---+---+ +---+---+---+
Read more about bit fields here: en.wikipedia.org/wiki/Bit_field
The default column name to store the flags is ‘flags’, but you can provide a custom column name using the :column
option. This allows you to use different columns for separate flags:
has_flags 1 => :warpdrive, 2 => :shields, 3 => :electrolytes, :column => 'features' has_flags 1 => :spock, 2 => :scott, 3 => :kirk, :column => 'crew'
Calling has_flags
as shown above creates the following instance methods on Spaceship:
Spaceship#warpdrive Spaceship#warpdrive? Spaceship#warpdrive= Spaceship#warpdrive_changed? Spaceship#all_warpdrives Spaceship#selected_warpdrives Spaceship#select_all_warpdrives Spaceship#unselect_all_warpdrives Spaceship#selected_warpdrives= Spaceship#has_warpdrive? Spaceship#shields Spaceship#shields? Spaceship#shields= Spaceship#shields_changed? Spaceship#all_shields Spaceship#selected_shields Spaceship#select_all_shields Spaceship#unselect_all_shields Spaceship#selected_shields= Spaceship#has_shield? Spaceship#electrolytes Spaceship#electrolytes? Spaceship#electrolytes= Spaceship#electrolytes_changed? Spaceship#all_electrolytes Spaceship#selected_electrolytes Spaceship#select_all_electrolytes Spaceship#unselect_all_electrolytes Spaceship#selected_electrolytes= Spaceship#has_electrolyte?
Calling has_flags
as shown above creates the following class methods on Spaceship:
Spaceship.flag_columns # [:features, :crew]
Optionally, you can set the :bang_methods
option to true to enable the bang methods:
Spaceship#electrolytes! Spaceship#not_electrolytes!
which respectively enables or disables the electrolytes flag.
The following named scopes become available:
Spaceship.warpdrive # :conditions => "(spaceships.flags in (1,3,5,7))" Spaceship.not_warpdrive # :conditions => "(spaceships.flags not in (1,3,5,7))" Spaceship.shields # :conditions => "(spaceships.flags in (2,3,6,7))" Spaceship.not_shields # :conditions => "(spaceships.flags not in (2,3,6,7))" Spaceship.electrolytes # :conditions => "(spaceships.flags in (4,5,6,7))" Spaceship.not_electrolytes # :conditions => "(spaceships.flags not in (4,5,6,7))"
If you do not want the named scopes to be defined, set the :named_scopes
option to false when calling has_flags
:
has_flags 1 => :warpdrive, 2 => :shields, 3 => :electrolytes, :named_scopes => false
In a Rails 3 application, FlagShihTzu will use scope
internally to generate the scopes. The option on has_flags is still named :named_scopes
however.
enterprise = Spaceship.new enterprise.warpdrive = true enterprise.shields = true enterprise.electrolytes = false enterprise.save if enterprise.shields? ... end Spaceship.warpdrive.find(:all) Spaceship.not_electrolytes.count ...
The following class methods may support you when manually building ActiveRecord conditions:
Spaceship.warpdrive_condition # "(spaceships.flags in (1,3,5,7))" Spaceship.not_warpdrive_condition # "(spaceships.flags not in (1,3,5,7))" Spaceship.shields_condition # "(spaceships.flags in (2,3,6,7))" Spaceship.not_shields_condition # "(spaceships.flags not in (2,3,6,7))" Spaceship.electrolytes_condition # "(spaceships.flags in (4,5,6,7))" Spaceship.not_electrolytes_condition # "(spaceships.flags not in (4,5,6,7))"
These methods also accept a :table_alias option that can be used when generating SQL that references the same table more than once:
Spaceship.shields_condition(:table_alias => 'evil_spaceships') # "(evil_spaceships.flags in (2,3,6,7))"
While the default way of building the SQL conditions uses an IN() list (as shown above), this approach will not work well for a high number of flags, as the value list for IN() grows.
For MySQL, depending on your MySQL settings, this can even hit the ‘max_allowed_packet’ limit with the generated query.
In this case, consider changing the flag query mode to :bit_operator
instead of :in_list
, like so:
has_flags 1 => :warpdrive, 2 => :shields, :flag_query_mode => :bit_operator
This will modify the generated condition and named_scope methods to use bit operators in the SQL instead of an IN() list:
Spaceship.warpdrive_condition # "(spaceships.flags & 1 = 1)", Spaceship.not_warpdrive_condition # "(spaceships.flags & 1 = 0)", Spaceship.shields_condition # "(spaceships.flags & 2 = 2)", Spaceship.not_shields_condition # "(spaceships.flags & 2 = 0)", Spaceship.warpdrive # :conditions => "(spaceships.flags & 1 = 1)" Spaceship.not_warpdrive # :conditions => "(spaceships.flags & 1 = 0)" Spaceship.shields # :conditions => "(spaceships.flags & 2 = 2)" Spaceship.not_shields # :conditions => "(spaceships.flags & 2 = 0)"
The drawback is that due to the bit operator, this query can not use an index on the flags column.
If you need to do mass updates without initializing object for each row, you can use #set_flag_sql method on your class. Example:
Spaceship.set_flag_sql(:warpdrive, true) # "flags = flags | 1" Spaceship.set_flag_sql(:shields, false) # "flags = flags & ~2"
And then use it in:
Spaceship.update_all Spaceship.set_flag_sql(:shields, false)
Beware that having multiple flag manipulation sql statements probably will not bring required result (at least on sqlite3, not tested on other databases), so you _should not_ do this:
Spaceship.update_all "#{Spaceship.set_flag_sql(:shields, false)},#{ Spaceship.set_flag_sql(:warpdrive, true)}"
General rule of thumb: issue only one flag update per update statement.
By default when you call has_flags in your code it will automatically check your database to see if you have correct column defined.
Sometimes this may not be a wanted behaviour (e.g. when loading model without database connection established) so you can set :check_for_column option to false to avoid it.
has_flags 1 => :warpdrive, 2 => :shields, :check_for_column => false
First, make sure all required gems are installed:
$ bundle install
The default rake test task will run the tests against the currently locked ActiveRecord version (see Gemfile.lock
):
$ bundle exec rake test
If you want to run the tests against all supported ActiveRecord versions:
$ bundle exec rake test:all
This will internally use bundler to load specific ActiveRecord versions before executing the tests (see gemfiles/
), e.g.:
$ BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-3.1.x' bundle exec rake test
All tests will use an in-memory sqlite database by default. If you want to use a different database, see test/database.yml
, install the required adapter gem and use the DB environment variable to specify which config from test/database.yml
to use, e.g.:
$ DB=mysql bundle exec rake
Peter Boling, Patryk Peszko, Sebastian Roebke, David Anderson, Tim Payton and a helpful group of contributors. Thanks!
Find out more about Peter Boling’s work RailsBling.com.
Find out more about XING Devblog.
-
Fork it
-
Create your feature branch (‘git checkout -b my-new-feature`)
-
Commit your changes (‘git commit -am ’Added some feature’‘)
-
Push to the branch (‘git push origin my-new-feature`)
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Create new Pull Request
This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.
As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.
For example:
spec.add_dependency 'flag_shih_tzu', '~> 4.0'
The MIT License
Copyright © 2012 Peter Boling Copyright © 2011 XING AG
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.