avatar

14

* 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

by chrisk, 25 Jul, 2007 10:36 PM
3 14  
33 require 'templated_attribute'
44 require 'templated_attribute_helper'
55 
6 ActiveRecord::Base.send(:include, TemplatedAttribute::ActiveRecordMixin)
6 ActiveRecord::Base.send(:include, TemplatedAttribute::ActiveRecordExtensions)
77 
88 ActionView::Base.class_eval do
99   include ActionView::Helpers::FormHelper
10 14  
11 module TemplatedAttribute
2   module ActiveRecordMixin
2   module ActiveRecordExtensions
33     
44     def self.included(base)
5       base.extend(TemplatedMethod)
5       base.extend(ClassMethods)
66     end
77     
8     module TemplatedMethod
8     module ClassMethods
99       # 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.
1111       # They're suggestions to the user about the expected formatting or content of a field.
1212       #
1313       # Unobtrusive javascript is used to populate empty form fields with their template values,
------
5151     
5252     
5353     module InstanceMethods
54       protected
5455       def remove_unchanged_template_values
5556         templated_attributes_options.each_pair do |attr_name, options|
5657           write_attribute(attr_name, nil) if read_attribute(attr_name).strip == options[:value]
7 14  
11 module ActionView
22   module Helpers
33     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
413 
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
814 
915 
10       # Override <tt>ActionView::Helpers::FormHelper#text_field</tt> to append
16       # Overrides <tt>ActionView::Helpers::FormHelper#text_area</tt> to append
1117       # templating behavior using unobtrusive javascript. Note, the javascript
1218       # 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 = {})
1430         object = object_name.classify.constantize
1531         template_options = object.templated_attributes_options[method.to_sym]
1632         
17         text_field_without_templating(object_name, method, options) +
33         send("#{field_helper}_without_templating".to_sym, object_name, method, options) +
1834         javascript_tag("Event.observe(window, 'load', function() {
1935                           new TemplatedAttribute($('#{object_name}_#{method}'),
2036                                                  '#{template_options[:type]}',