acts_as_label

Description

This acts_as extension implements a system label and a friendly label on a class and centralizes the logic for performing validations and accessing items by system label.

The extension is particularly useful for tabled, enumerated lists.

The system label is declared as a read-only attribute, allowing models and controllers to code to the value safely, secure in the knowledge that the value will never change.

The friendly value can be changed to any value required by the end-user without affecting the model/controller code in any way.

Usage

If you just want validations and system label accessors, simply add acts_as_label to the model.

class Role < ActiveRecord::Base
  acts_as_label
end

The plugin accepts three optional parameters.

system_label_column

specifies the name of the column to use for the system label (default: system_label)

label_column

specifies the name of the column to use for the label (default: label)

default

specifies the system label value to use as the default record

class Role < ActiveRecord::Base
  acts_as_label :system_label_column => :key, :label_column => :name, :default => :guest
end

If you use the plugin within a single table inheritance (STI) design, the easiest way to specify a default record is by implementing a class method on the subclass.

class Label < ActiveRecord::Base
  acts_as_label
end

class BillingFrequency < Label
  self.default
    BillingFrequency.monthly
  end
end

The extension also allows records to be access by system label as though the system label were a class method. The default option can be accessed in a similar manner using the class method default. This yields more expressive code.

class Role < ActiveRecord::Base
  has_many :users  

  acts_as_label :default => :guest
end

class User < ActiveRecord::Base
  belongs_to :role
end

Role.create!({ :system_label => "SUPERUSER", :label => "Superuser"} )
Role.create!({ :system_label => "GUEST",      :label => "Guest"} )

User.create!({ :name => "John Dugan",     :role => Role.superuser })
User.create!({ :name => "Anonymous Dude", :role => Role.default })

To help avoid excessive querying, the extension overrides the equality operator. This allows record comparisons in a couple of formats:

u.role == Role.superuser	# as you would expect
u.role == :superuser		# to spare a superfluous query (same as u.role.to_sym == :superuser)

Other useful utility methods:

to_s

Returns the friendly label value

to_sym

Returns the downcased, symbolized system label value

Installation (Rails 3)

Install me from RubyGems.org by adding a gem dependency to your Gemfile. Bundler does the rest.

gem “acts_as_label”

$ bundle install

Installation (Rails 2)

Install me from RubyGems.org and add a gem dependency in the appropriate file.

$ gem install acts_as_label

Or install me as a plugin.

$ script/plugin install git://github.com/coroutine/acts_as_label.git

Gemroll

Other gems by Coroutine include:

License

Copyright © 2010 Coroutine LLC.

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.