= Property sets
The property_set gem is an evolution of the has_settings gem which was an evolution of the features gem. This is getting old.
This gem is a way for you to use a basic "key/value" store for storing attributes for a given model in a relational fashion where there's a row per attribute. Alternatively you'd need to add a new column per attribute to your main table, or serialize the attributes and their values.
== Description
You configure the allowed stored properties by specifying these in an initializer:
class Account < ActiveRecord::Base
property_set :settings do
property :version, :default => "v1.0"
property :product
end
property_set :texts do
property :epilogue
end
end
The declared properties can then be accessed runtime via the defined association:
account.settings.ssl # returns the value of the setting
account.settings.ssl=(value) # immediately changes the value of the setting
account.settings.ssl? # coerces the setting to boolean AR style
If the value has never been set, a nil (or default) is returned. And that's pretty much it.
Stored properties can also be updated with the update_attributes and update_attributes! methods.
# Assuming params include { :account => { :settings => { :version => "v2.0", :forums => '0' }}}
# the below would set the respective values on the account
account.update_attributes(params[:account])
We also created a view helper to help you generate the UI for enabling and disabling properties:
<% form_for(:account, :html => { :method => :put }) do |f| %>
<%= f.property_set_check_box(:settings) :forums %> Enable forums on your account
<% end %>
== Security
We support protection from mass updates, the syntax is as follows:
class Account < ActiveRecord::Base
property_set :settings do
property :product, :protected => true
end
end
== Installation
Install the gem in your rails project by putting it in your Gemfile:
gem 'property_set'
Also remember to create the storage table, if for example you are going to be using this with an accounts model and have, you can should the table like:
create_table :account_settings do |t|
t.integer :account_id, :null => false
t.string :name, :null => false
t.string :value
t.timestamps
end
add_index :account_settings, [ :account_id, :name ], :unique => true
== Requirements
* ActiveRecord
* ActionPack
== LICENSE:
(The MIT License)
Copyright (c) 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.