Final pre-release additions to docs
| 27 | 29 | |
|---|---|---|
| 1 | 1 | = Templated Attribute plugin |
| 2 | ||
| 3 | This Rails plugin allows you to declare that certain attributes in your model | |
| 4 | are "templated." A templated attribute has a helpful initial value---kind of | |
| 5 | like a default value---except that these aren't valid data or saved in the | |
| 6 | database. They're suggestions to the user about the expected formatting or | |
| 7 | content of a field. | |
| 2 | 8 | |
| 9 | You can view pre-generated rdocs for this plugin at: | |
| 10 | <http://docs.shiftcommathree.com/rails_plugins/templated_attribute>. | |
| 11 | ||
| 3 | 12 | See the blog post for a more detailed introduction: |
| 4 | <http://shiftcommathree.com/articles/templated_attributes> | |
| 13 | <http://shiftcommathree.com/articles/templated_attributes>. | |
| 5 | 14 | |
| 6 | 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. | |
| 7 | 15 | |
| 8 | 16 | |
| 9 | ||
| 10 | 17 | == Installation |
| 11 | 18 | |
| 12 | 19 | If you're using piston: |
| --- | --- | |
| 37 | 44 | |
| 38 | 45 | This plugin aims to: |
| 39 | 46 | |
| 40 | 1. pre-fill form fields with these templates, but keep these values out of the database | |
| 47 | 1. pre-fill form fields with these templates, but keep these values out of the | |
| 48 | database | |
| 41 | 49 | 2. specify these values only once in the model |
| 42 | 3. create a user experience that clearly implies that these values are just templates for valid data | |
| 50 | 3. create a user experience that clearly implies that these values are just | |
| 51 | templates for valid data | |
| 43 | 52 | |
| 44 | There are actually two kinds of templated attributes provided: those with <b>starting values</b>, which are potentially the start of valid data, and <b>labels</b>, which are just helpful, ephemeral reminders. An example of a starting value template is <tt>http://</tt> for a +website+ attribute; an example of a label template is <tt>(123) 555-1234</tt> 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.) | |
| 53 | There are actually two kinds of templated attributes provided: those with | |
| 54 | <b>starting values</b>, which are potentially the start of valid data, and | |
| 55 | <b>labels</b>, which are just helpful, ephemeral reminders. An example of a | |
| 56 | starting value template is <tt>http://</tt> for a +website+ attribute; an | |
| 57 | example of a label template is <tt>(123) 555-1234</tt> for a +phone+ attribue. | |
| 58 | (We don't want the user to have to delete our dummy numbers and put in their | |
| 59 | own; it's too much work. Instead we think that the reminder will help coax the | |
| 60 | right format out of the user by itself--so this field gets blanked on focus | |
| 61 | using Javascript, unlike the +website+ attribute, which remains because it's | |
| 62 | the start of valid data.) | |
| 45 | 63 | |
| 46 | 64 | |
| 47 | 65 | |
| 48 | 66 | == Usage |
| 49 | 67 | |
| 68 | See the documentation for the | |
| 69 | +templated_attribute+[link:classes/TemplatedAttribute/ActiveRecordExtensions/ClassMethods.html] | |
| 70 | method. | |
| 71 | ||
| 50 | 72 | class User < ActiveRecord::Base |
| 51 | 73 | templated_attribute :website, :starting_value => 'http://' |
| 52 | 74 | templated_attribute :phone, :label => '(123) 555-1234' |
| 53 | 75 | end |
| 54 | 76 | |
| 55 | 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. | |
| 77 | Validations work as expected, since unchanged template values get removed in a | |
| 78 | <tt>:before_validation</tt> callback. So you can sprinkle on a little | |
| 79 | <tt>:validates_presence_of</tt> and <tt>:validates_format_of</tt> for a really | |
| 80 | good time. | |
| 56 | 81 | |
| 57 | 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. It gets installed when you install the plugin, or you can install and remove manually with these `rake` tasks: | |
| 82 | There's also some nice, unobtrusive Javascript you can generate to get the | |
| 83 | behavior I mentioned above. If you're using +form_for+, it's totally automatic. | |
| 84 | See the documentation for | |
| 85 | +text_area+[link:classes/ActionView/Helpers/FormHelper.html] and | |
| 86 | +text_field+[link:classes/ActionView/Helpers/FormHelper.html]. It gets | |
| 87 | installed when you install the plugin, or you can install and remove manually | |
| 88 | with these +rake+ tasks: | |
| 58 | 89 | |
| 59 | 90 | rake templated_attribute:install |
| 60 | 91 | rake templated_attribute:remove |
| 61 | 92 | |
| 62 | To turn off the Javascript for a given `templated_attribute`---say, because the generated stuff doesn't jibe with your fancy-pantsy, AJAX-validating, Grey Poupon of a form---just throw `:templated_javascript => false` in the options hash for `text_field` or `text_area`. You'll have to do any styling and event handling by yourself. | |
| 93 | To turn off the Javascript for a given +templated_attribute+--say, because the | |
| 94 | generated stuff doesn't jibe with your fancy-pantsy, AJAX-validating, Grey | |
| 95 | Poupon of a form--just throw <tt>:templated_javascript => false</tt> in the | |
| 96 | options hash for +text_field+ or +text_area+. You'll have to do any styling | |
| 97 | and event handling by yourself. | |
| 63 | 98 | |
| 64 | I'd like to make this work for fields other than `text_field` and `text_area`; the other contenders were `file_field`, which we can't do because the Javascript security model doesn't let us touch its value at runtime, and `password_field`, which I haven't done because showing the template value would require dynamically switching the element to a `text_field` and back (to avoid all those asterisks). That one's on the list, though. | |
| 99 | I'd like to make this work for fields other than +text_field+ and +text_area+; | |
| 100 | the other contenders were +file_field+, which we can't do because the | |
| 101 | Javascript security model doesn't let us touch its value at runtime, and | |
| 102 | +password_field+, which I haven't done because showing the template value | |
| 103 | would require dynamically switching the element to a +text_field+ and back (to | |
| 104 | avoid all those asterisks). That one's on the list, though. | |
| 65 | 105 | |
| 66 | 106 | |
| 67 | 107 | |
