94 lines
2.7 KiB
Text
94 lines
2.7 KiB
Text
= acts_as_configurable
|
|
|
|
This mixin adds a number of methods to an ActiveRecord module which
|
|
enable saving any settings you want (see examples below).
|
|
ActiveRecord is required.
|
|
|
|
== Install
|
|
|
|
./script/plugin install http://svn.nkryptic.com/plugins/acts_as_configurable
|
|
|
|
== Usage
|
|
|
|
This mixin will provide your model with a large variety of configuration options.
|
|
class User < ActiveRecord::Base
|
|
acts_as_configurable
|
|
end
|
|
|
|
Example:
|
|
|
|
user = User.create(:name => 'joe')
|
|
user.settings # => []
|
|
|
|
user.settings[:friends] = ['jane','sam','karl']
|
|
user.settings[:friends] # => ['jane','sam','karl']
|
|
user.settings[:age] = 25
|
|
user.settings[:age] # => 25
|
|
|
|
OR
|
|
|
|
user = User.create(:name => 'joe')
|
|
post = Post.find(:first)
|
|
|
|
user.settings_for(post) # => []
|
|
user.settings_for(post)[:show_headlines] = true
|
|
|
|
user.settings_for(post)[:show_headlines] # => true
|
|
user.settings_for(post).size # => 1
|
|
|
|
# but the user's untargeted settings are still empty
|
|
user.settings # => []
|
|
|
|
update: now there is a method each_with_key
|
|
user.settings.each_with_key {|k,s| do something} # k is the key (a string) and s is the setting
|
|
and
|
|
user.settings_for(post).each_with_key {|k,s| do something} # k is the key (a string) and s is the setting
|
|
|
|
|
|
This mixin will provide your model with the ability to see where it is used as a target of acts_as_configurable
|
|
class Post < ActiveRecord::Base
|
|
acts_as_configurable_target
|
|
end
|
|
|
|
Example:
|
|
|
|
user = User.create(:name => 'joe')
|
|
post = Post.find(:first)
|
|
|
|
user.settings_for(post) # => []
|
|
user.settings_for(post)[:num_lines] = 15
|
|
|
|
user.settings_for(post)[:num_lines] # => 15
|
|
post.targeted_settings[:num_lines].size # => 1
|
|
post.targeted_settings[:num_lines].first # => 15
|
|
post.targeted_settings[:num_lines].first.owner # => user
|
|
|
|
OR
|
|
|
|
user = User.create(:name => 'joe')
|
|
post = Post.find(:first)
|
|
|
|
user.settings_for(post) # => []
|
|
user.settings_for(post)[:num_lines] = 15
|
|
|
|
user.settings_for(post)[:num_lines] # => 15
|
|
user.settings_for(post)[:hide_comments] # => true
|
|
post.targeted_settings_for(user)[:num_lines] # => 15
|
|
post.targeted_settings_for(user) # => [15,true]
|
|
post.targeted_settings_for(user).collect {|x| x.name} # => ['num_lines','hide_comments']
|
|
|
|
== Subversion
|
|
|
|
http://svn.nkryptic.com/plugins/acts_as_configurable
|
|
|
|
== Credits
|
|
|
|
I was insprired by the following people/works
|
|
|
|
* Rick Olson - acts_as_versioned plugin (plugin design)
|
|
* Bill Katz - authorization plugin (roles applied to any model)
|
|
* Tobias Luetke - Typo (configuration settings manager with ruby-typing of value)
|
|
* Rails core - AssociationCollection and AssociationProxy classes
|
|
|
|
|
|
|