Permalink
Browse files

Added the ability to customize the order for specific types of inputs…

…. This is configured on a type basis and if a type is not found it willfall back to the default order as defined by @@inline_order
  • Loading branch information...
1 parent c02f6f1 commit f066e351e9bb86c0c2133d089a6002a27850a336 Disha Al Baqui committed with mjonuschat May 31, 2010
Showing with 32 additions and 3 deletions.
  1. +4 −0 CHANGELOG
  2. +7 −0 generators/formtastic/templates/formtastic.rb
  3. +4 −3 lib/formtastic.rb
  4. +17 −0 spec/input_spec.rb
View
@@ -1,3 +1,7 @@
+1.2.0
+
+* Added custom_inline_order to allow inline ordering per input type
+
1.1.0
* documentation changes only
@@ -45,6 +45,13 @@
# customize it doing just as below:
# Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors]
+# Additionally, you can customize the order for specific types of inputs.
+# This is configured on a type basis and if a type is not found it will
+# fall back to the default order as defined by #inline_order
+# Formtastic::SemanticFormBuilder.custom_inline_order[:checkbox] = [:errors, :hints, :input]
+# Formtastic::SemanticFormBuilder.custom_inline_order[:select] = [:hints, :input, :errors]
+
+
# Specifies if labels/hints for input fields automatically be looked up using I18n.
# Default value: false. Overridden for specific fields by setting value to true,
# i.e. :label => true, or :hint => true (or opposite depending on initialized value)
View
@@ -8,8 +8,8 @@ module Formtastic #:nodoc:
class SemanticFormBuilder < ActionView::Helpers::FormBuilder
class_inheritable_accessor :default_text_field_size, :default_text_area_height, :all_fields_required_by_default, :include_blank_for_select_by_default,
:required_string, :optional_string, :inline_errors, :label_str_method, :collection_value_methods, :collection_label_methods,
- :inline_order, :file_methods, :priority_countries, :i18n_lookups_by_default, :escape_html_entities_in_hints_and_labels, :default_commit_button_accesskey,
- :instance_reader => false
+ :inline_order, :custom_inline_order, :file_methods, :priority_countries, :i18n_lookups_by_default, :escape_html_entities_in_hints_and_labels,
+ :default_commit_button_accesskey, :instance_reader => false
self.default_text_field_size = 50
self.default_text_area_height = 20
@@ -22,6 +22,7 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
self.collection_label_methods = %w[to_label display_name full_name name title username login value to_s]
self.collection_value_methods = %w[id to_s]
self.inline_order = [ :input, :hints, :errors ]
+ self.custom_inline_order = {}
self.file_methods = [ :file?, :public_filename, :filename ]
self.priority_countries = ["Australia", "Canada", "United Kingdom", "United States"]
self.i18n_lookups_by_default = false
@@ -102,7 +103,7 @@ def input(method, options = {})
options[:label_html][:for] ||= options[:input_html][:id]
end
- input_parts = self.class.inline_order.dup
+ input_parts = (self.class.custom_inline_order[options[:as]] || self.class.inline_order).dup
input_parts = input_parts - [:errors, :hints] if options[:as] == :hidden
list_item_content = input_parts.map do |type|
View
@@ -32,6 +32,23 @@
concat(builder.input(:title))
end
end
+
+ it 'should allow errors, input, hint for a custom type while preserving original inline order' do
+ ::Formtastic::SemanticFormBuilder.custom_inline_order[:checkbox] = [:errors, :input, :hints]
+ ::Formtastic::SemanticFormBuilder.inline_order = [:hints, :input, :errors]
+
+ semantic_form_for(@new_post) do |builder|
+ builder.should_receive(:inline_errors_for).once.ordered
+ builder.should_receive(:inline_input_for).once.ordered
+ builder.should_receive(:inline_hints_for).once.ordered
+ concat(builder.input(:title, :as => :checkbox))
+ builder.should_receive(:inline_hints_for).once.ordered
+ builder.should_receive(:inline_input_for).once.ordered
+ builder.should_receive(:inline_errors_for).once.ordered
+ concat(builder.input(:title))
+ end
+ end
+
end
describe 'arguments and options' do

0 comments on commit f066e35

Please sign in to comment.