README.rdoc in property_sets-0.0.11 vs README.rdoc in property_sets-0.0.12
- old
+ new
@@ -1,20 +1,18 @@
= 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:
+You configure the allowed stored properties by specifying these in the model:
class Account < ActiveRecord::Base
property_set :settings do
property :version, :default => "v1.0"
- property :featured
- property :product
+ property :featured, :protected => true
+ property :activated
end
property_set :texts do
property :epilogue
end
@@ -32,29 +30,72 @@
account.settings.version.create(:value => "v1.1")
# Destroy the version record
account.settings.version.destroy
-On top of the basic access paths, there are some short cuts:
+=== Convenience methods
- account.settings.featured=(1) # immediately changes the value of the setting
- account.settings.featured? # coerces the setting to boolean AR style
- account.settings.featured.enable # sets the value of this setting to a true value
- account.settings.featured.disable # sets the value of this setting to a false value
+On top of the basic access paths, there are some short cuts, mainly convenience methods for dealing with booleans:
-If the value has never been set, a nil (or default) is returned. And that's pretty much it.
+ # immediately changes the value of the setting
+ account.settings.version=("v3.0")
+ # coerces the setting to boolean AR style
+ account.settings.featured?
+
+ # sets the value of this setting to a true value
+ account.settings.featured.enable
+
+ # sets the value of this setting to a false value
+ account.settings.featured.disable
+
+=== Bulk operations
+
Stored properties can also be updated with the update_attributes and update_attributes! methods by
-enabling nested attributes. See the test cases for examples.
+enabling nested attributes. Like this (from the test cases):
+ @account.texts_attributes = [
+ { :name => "foo", :value => "1" },
+ { :name => "bar", :value => "0" }
+ ]
+
+And for existing records:
+
+ @account.update_attributes!(:texts_attributes => [
+ { :id => @account.texts.foo.id, :name => "foo", :value => "0" },
+ { :id => @account.texts.bar.id, :name => "bar", :value => "1" }
+ ])
+
+Using nested attributes is subject to implementing your own security measures for mass update assignments.
+Alternatively, it is possible to use a custom hash structure:
+
+ params = { :property_sets => {
+ :settings => { :version => "v4.0", :featured => "1" },
+ :texts => { :epilogue => "Wibble wobble" }
+ }}
+ @account.update_attributes(params)
+
+The above will not update +featured+ as this has the protected flag set and is hence protected from
+mass updates.
+
+=== View helpers
+
+We support a single convenience mechanism for building forms and putting the values into the above hash structure. So far, we only support check boxes:
+
+ <% form_for(:account, :html => { :method => :put }) do |f| %>
+ <h3>
+ <%= f.property_set(:settings).check_box :activated %> Activated?
+ </h3>
+ <% end %>
+
== Installation
Install the gem in your rails project by putting it in your Gemfile:
- gem 'property_set'
+ gem "property_sets"
-Also remember to create the storage table, if for example you are going to be using this with an accounts model, you can define the table like:
+Also remember to create the storage table(s), if for example you are going to be using this with an accounts model and a "settings" property set, you can define the table like:
create_table :account_settings do |t|
t.integer :account_id, :null => false
t.string :name, :null => false
t.string :value
@@ -64,16 +105,16 @@
add_index :account_settings, [ :account_id, :name ], :unique => true
== Requirements
* ActiveRecord
-* ActionPack
+* ActiveSupport
== LICENSE:
(The MIT License)
-Copyright (c) 2010 Zendesk
+Copyright (c) 2011 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,
\ No newline at end of file