Skip to content

mpapis/acts_as_configurable

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ActsAsConfigurable

!!! This is an fork of the original, code was completly rewritten, it works differently, read bellow for more informations !!!

Rails plugin that adds dynamic (non-database) configuration settings to any class (also models & controlers).

Installation

In rails application directory call:


script/plugin install git@github.com:mpapis/acts_as_configurable.git

Examples

Setup file:


default:
  :setting1: 1st
Book:
  :setting1: 2nd
settings:
  :setting1: 3rd
allow_comments: false

Example usages:


class User < ActiveRecord::Base
  acts_as_configurable  
end
puts "#{User.configuration[:setting1]}"
puts "#{User.new.configuration[:setting1]}"
class Book < ActiveRecord::Base
  acts_as_configurable
end
puts "#{Book.configuration[:setting1]}"
class Comment < ActiveRecord::Base
  acts_as_configurable :defult => :settings
end
puts "#{Comment.configuration[:setting1]}"
class Address < ActiveRecord::Base
  acts_as_configurable :field=>allow_comments
end
puts "#{Address.allow_comments}"
class Post < ActiveRecord::Base
  acts_as_configurable :conf, :for => :settings
  acts_as_configurable :conf_book, :for => Book
end
puts "#{Post.conf[:setting1]}"
puts "#{Post.new.conf_book[:setting1]}"

Default settings for :default, :name, :for, :default can be overwritten using:

ActsAsConfigurable::Configuration.default_method_name - set new default for name, if it is not set then 'configuration' is used
ActsAsConfigurable::Configuration.default_klass_name - set new default for 'for', if not set It tryes to use class name, but if not available 'default' will be used
ActsAsConfigurable::Configuration.default_key_name - another default definition, normally 'default' is good

All options can be specified as strings or symbols, additionally :for can also work with Class

Dependencies

This plugin mixes directly into Object, so there’s no dependency on another library. Any class can act as configurable.

Actually, ActiveSupport is required (thanks to cattr_accessor), but since this is a
plugin for a Rails application, there shouldn’t be any issues there.

Credits

Chris Scharf is the original author of this “on-the-fly” configuration.
His original implementation was intended for application wide configuration settings. You can find
his post about it here.

Ryan Heath is the author of original plugin.

With my version you can have the convenience of storing configuration in yaml file – you do not have to change code to change configuration.

© 2010 Michal Papis, Ryan Heath and Chris Scharf, released under the MIT license

About

Rails plugin: rewriten version of plugin, settings are not writen into code, You have to write them down into config file - this helps to prevent using of wrong keys.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%