Custom defined user preferences for ActiveRecord models
Ruby
Pull request Compare This branch is 16 commits ahead, 3 commits behind omghax:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
spec
test
.autotest
.gitignore
MIT-LICENSE
README
Rakefile
init.rb

README

= ActsAsCustomConfigurable

This plugin works does (nearly) the same as ActsAsConfigurable, with three exceptions:
 * all the settings are accessible through Model#options
 * you can define new settings for each instance of a model (using the serialized #defined_options)
 * delegating the value lookup to a belongs_to/has_one association

Please see spec/acts_as_custom_configurable_spec.rb

The following is maybe outdated through the fork. Maybe not.

= ActsAsConfigurable

This plugin provides the ability to specify settings on a model class
similarly to the way columns are defined in migrations, including
default values. It does this by saving the settings to a TEXT column
as a Hash object, serialized by YAML.

While you could get the same effect by creating a column for each
individual setting, this becomes cumbersome when there are more than a
handful of settings, or when the settings need to be changed
frequently.

== Getting Started

First, install the plugin:

  $ ./script/plugin install git://github.com/omghax/acts_as_configurable.git

Next, declare acts_as_configurable in your model(s):

  class Blog < ActiveRecord::Base
    acts_as_configurable do |c|
      c.string :title, :default => "My Blog"
      c.integer :number_of_frontpage_articles, :default => 10
      c.boolean :enable_akismet, :default => false
    end
  end

This will define instance methods on Blog for each setting, with the
specified default values. These attributes can be used in
mass-assignment, validations, and pretty much anywhere else you'd use
a normal database column.

The available types of settings are :string, :integer, :boolean, and
:object.

By default, these settings will be stored inside a database column
named "settings". If you'd prefer to store them elsewhere, use the
:using option.

  class Blog < ActiveRecord::Base
    acts_as_configurable :using => "some_other_column" do |c|
      ...
    end
  end

This column should be a TEXT column.

=== Strings

First let's take a look at string settings. These behave as you would
expect them to.

Example:

  >> b = Blog.new
  >> b.title
  # => "My Blog"
  >> b.title = "My Custom Blog"
  >> b.title
  # => "My Custom Blog"

=== Integers

Integer settings work similarly to string settings, but are coerced
into integers when their values are set.

=== Booleans

Here's how you deal with boolean settings. Note that these settings
are flexible with what they accept during assignment.

[false]
  nil, 0, "0", "f", "false", false
[true]
  anything else

Example:

  >> b.enable_akismet?
  # => false
  >> b.enable_akismet = true
  >> b.enable_akismet?
  # => true

=== Objects

Object settings allow you to use any serializable ruby object to
represent a setting's value. They should be used with care though, as
changes to your code can potentially break the records in your
database which reference earlier versions of classes that have
changed.

== License

Copyright (c) 2008 Dray Lacy

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.