* change mixin structure to use more standard ClassMethods module, rename ActiveRecordMixin => ActiveRecordExtensions
* fix typo in templated_attribute docs
* protect helper instance method
* refactor/DRYify ActionView::Helpers::FormHelper additions for use with multiple field types
- M rails_plugins/templated_attribute/init.rb view
- M rails_plugins/templated_attribute/lib/templated_attribute.rb view
- M rails_plugins/templated_attribute/lib/templated_attribute_helper.rb view
| 3 | 14 | |
|---|---|---|
| 3 | 3 | require 'templated_attribute' |
| 4 | 4 | require 'templated_attribute_helper' |
| 5 | 5 | |
| 6 | ActiveRecord::Base.send(:include, TemplatedAttribute::ActiveRecordMixin) | |
| 6 | ActiveRecord::Base.send(:include, TemplatedAttribute::ActiveRecordExtensions) | |
| 7 | 7 | |
| 8 | 8 | ActionView::Base.class_eval do |
| 9 | 9 | include ActionView::Helpers::FormHelper |
| 10 | 14 | |
|---|---|---|
| 1 | 1 | module TemplatedAttribute |
| 2 | module ActiveRecordMixin | |
| 2 | module ActiveRecordExtensions | |
| 3 | 3 | |
| 4 | 4 | def self.included(base) |
| 5 | base.extend(TemplatedMethod) | |
| 5 | base.extend(ClassMethods) | |
| 6 | 6 | end |
| 7 | 7 | |
| 8 | module TemplatedMethod | |
| 8 | module ClassMethods | |
| 9 | 9 | # Used to specify a template value for an attribute. A "templated attribute" has a helpful starting |
| 10 | # value--kind like a default value--except that these aren't valid data or saved in the database. | |
| 10 | # value--kind of like a default value--except that these aren't valid data or saved in the database. | |
| 11 | 11 | # They're suggestions to the user about the expected formatting or content of a field. |
| 12 | 12 | # |
| 13 | 13 | # Unobtrusive javascript is used to populate empty form fields with their template values, |
| --- | --- | |
| 51 | 51 | |
| 52 | 52 | |
| 53 | 53 | module InstanceMethods |
| 54 | protected | |
| 54 | 55 | def remove_unchanged_template_values |
| 55 | 56 | templated_attributes_options.each_pair do |attr_name, options| |
| 56 | 57 | write_attribute(attr_name, nil) if read_attribute(attr_name).strip == options[:value] |
| 7 | 14 | |
|---|---|---|
| 1 | 1 | module ActionView |
| 2 | 2 | module Helpers |
| 3 | 3 | module FormHelper |
| 4 | ||
| 5 | ||
| 6 | # Overrides <tt>ActionView::Helpers::FormHelper#text_field</tt> to append | |
| 7 | # templating behavior using unobtrusive javascript. Note, the javascript | |
| 8 | # used requires Prototype to add <tt>onLoad</tt> event listeners. | |
| 9 | def text_field_with_templating(*args) | |
| 10 | field_with_templating :text_field, *args | |
| 11 | end | |
| 12 | alias_method_chain :text_field, :templating | |
| 4 | 13 | |
| 5 | # Save an alias to <tt>ActionView::Helpers::FormHelper#text_field</tt> | |
| 6 | # so we can call it from our overriden version. | |
| 7 | alias :text_field_without_templating :text_field | |
| 8 | 14 | |
| 9 | 15 | |
| 10 | # Override <tt>ActionView::Helpers::FormHelper#text_field</tt> to append | |
| 16 | # Overrides <tt>ActionView::Helpers::FormHelper#text_area</tt> to append | |
| 11 | 17 | # templating behavior using unobtrusive javascript. Note, the javascript |
| 12 | 18 | # used requires Prototype to add <tt>onLoad</tt> event listeners. |
| 13 | def text_field(object_name, method, options = {}) | |
| 19 | def text_area_with_templating(*args) | |
| 20 | field_with_templating :text_area, *args | |
| 21 | end | |
| 22 | alias_method_chain :text_area, :templating | |
| 23 | ||
| 24 | ||
| 25 | ||
| 26 | ||
| 27 | private | |
| 28 | ||
| 29 | def field_with_templating(field_helper, object_name, method, options = {}) | |
| 14 | 30 | object = object_name.classify.constantize |
| 15 | 31 | template_options = object.templated_attributes_options[method.to_sym] |
| 16 | 32 | |
| 17 | text_field_without_templating(object_name, method, options) + | |
| 33 | send("#{field_helper}_without_templating".to_sym, object_name, method, options) + | |
| 18 | 34 | javascript_tag("Event.observe(window, 'load', function() { |
| 19 | 35 | new TemplatedAttribute($('#{object_name}_#{method}'), |
| 20 | 36 | '#{template_options[:type]}', |
