Plugins - Templated Attribute
Add to favoritesTemplated Attribute plugin
This Rails plugin allows you to declare that certain attributes in your model are "templated." A templated attribute has a helpful initial value—kind of like a default value—except that these aren’t valid data or saved in the database. They’re suggestions to the user about the expected formatting or content of a field.
You can view pre-generated rdocs for this plugin at: http://docs.shiftcommathree.com/rails_plugins/templated_attribute
See the blog post for a more detailed introduction: http://www.shiftcommathree.com/articles/templated-attributes
Description
This plugin aims to:
- pre-fill form fields with these templates, but keep these values out of the database
- specify these values only once in the model
- create a user experience that clearly implies that these values are just templates for valid data
There are actually two kinds of templated attributes provided: those with starting values, which are potentially the start of valid data, and labels, which are just helpful, ephemeral reminders. An example of a starting value template is http:// for a website attribute; an example of a label template is (123) 555-1234 for a phone attribue. (We don’t want the user to have to delete our dummy numbers and put in their own; it’s too much work. Instead we think that the reminder will help coax the right format out of the user by itself—so this field gets blanked on focus using Javascript, unlike the website attribute, which remains because it’s the start of valid data.)
Usage
See the documentation for the templated_attribute method.
class User < ActiveRecord::Base
templated_attribute :website, :starting_value => 'http://'
templated_attribute :phone, :label => '(123) 555-1234'
end
Validations work as expected, since unchanged template values get removed in a :before_validation callback. So you can sprinkle on a little :validates_presence_of and :validates_format_of for a really good time.
There’s also some nice, unobtrusive Javascript you can generate to get the behavior I mentioned above. If you’re using form_for, it’s totally automatic. See the documentation for text_area and text_field.
http://www.shiftcommathree.com/articles/templated-attributes
https://svn.shiftcommathree.com/rails_plugins/templated_attribute/
Rails' (MIT)
View Extensions
