Has settings

This is a slightly revised version of the features gem (github.com/zendesk/features/). This gem allows you to work with row oriented settings rather than having an ever increasing number of columns on your model table. You may want to check out alternative approaches for this, like e.g. the bit mask based ones such as github.com/xing/flag_shih_tzu

Description

You configure the allowed settings by specifying these in an initializer:

class Account < ActiveRecord::Base
  has_settings do
    setting :forums
    setting :wiki
  end
end

These are the settings that can be used runtime. This enables code like:

.settings.ssl?         # checks whether an account has the ssl setting.
.settings.ssl.create   # adds the ssl setting to the account
.settings.ssl.destroy  # removes the ssl setting from the account

And that’s pretty much it.

Settings can also be updated with the update_attributes and update_attributes! methods.

# assuming params include { :account => { :settings => { :wiki => '1', :forums => '0' } } }
.update_attributes(params[:account]) # would add the wiki feature and remove the forums setting

We also created a view helper to help you generate the UI for enabling and disabling settings:

<% form_for(:account, :html => { :method => :put }) do |f| %>
  <h3>
    <%= f.settings_check_box :forums %> Enable forums on your account
  </h3>
<% end %>

Security

We support protection from mass updates, the syntax is as follows:

class Account < ActiveRecord::Base
  has_settings do
    setting :forums, :protected => true
    setting :wiki
  end
end

Installation

Install the gem in your rails project by putting it in your Gemfile:

gem 'has_settings'

Also remember to create the settings table, if for example you are going to be using this with an accounts model, you can should the table like:

create_table :account_settings do |t|
  t.integer :account_id, :null => false
  t.string  :name, :null => false
  t.timestamps
end

add_index :account_settings, [ :account_id, :name ], :unique => true

Requirements

  • ActiveRecord

  • ActionPack

LICENSE:

(The MIT License)

Copyright © 2010 Zendesk

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.