Acts As Configurable plugin

Plugin details

This mixin adds a number of methods to an ActiveRecord module which enable saving any settings you want (see examples below).

Websitehttp://svn.nkryptic.com Repositoryhttp://svn.nkryptic.com/plugins/acts_as_configurable Author Jacob Radford Tags ActiveRecord LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install http://svn.nkryptic.com/plugins/acts_as_configurable

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                    # => []

user.settings.each_with_key {|k,s| do something} # k is the key (a string) and s is the setting
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']


Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: maxim_kulkin, about 1 year ago