bit_settings

BitSettings is a plugin for ActiveRecord that transform a column of your model in a set of boolean settings.


Keywords
activerecord, boolean, rails
License
MIT
Install
gem install bit_settings -v 1.0.2

Documentation

BitSettings

BitSettings is a plugin for ActiveRecord that transform a column of your model in a set of boolean settings. You can have up to 32 boolean values stored in a single database column.

Installation

Add this line to your application's Gemfile:

gem 'bit_settings'

And then execute:

$ bundle

Or install it yourself as:

$ gem install bit_settings

Usage

First of all add a unsigned int column for your boolean settings with rails g migration add_settings_to_users settings:integer and then edit like follow:

class AddSettingsToUsers < ActiveRecord::Migration
  def change
    add_column :users, :settings, 'INT UNSIGNED', null: false, default: 0
  end
end

The max unsigned integer in 4 bytes is 2^32-1 = 4294967295 so with a column you can have max 32 settings. If you want that the default value of your settings is 1 (true) set the default value of the column to 2^32-1.

Then in your model:

class User extends ActiveRecord::Base
  include BitSettings
  add_settings :disable_notifications, :help_tour_shown
end

Now you can do:

user.disable_notifications? # => false
user.help_tour_shown? # => false
user.disable_notifications = true # => true
user.save

You can also use the scope with_settings which takes a hash as parameter like the example below:

# Get all users with the value of setting disable_notifications equal to true
# and the settings help_tour_shown equal to false.
users = User.with_settings(disable_notifications: true, help_tour_shown: false)

Available options are :column and :prefix:

class User extends ActiveRecord::Base
  add_settings :disable_notifications, :help_tour_shown, column: :my_settings, prefix: :setting
end
  • :column specify the name of the column to use (default is settings)
  • :prefix add a prefix to dynamic methods
user.setting_disable_notifications? # => false
users = User.with_settings(setting_disable_notifications: false)

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/pioz/bit_settings.

License

The gem is available as open source under the terms of the MIT License.