Skip to content
Browse files

Merge branch 'master' of git://github.com/ptzn/semantic_form_builder

Conflicts:
	lib/semantic_form_builder.rb
  • Loading branch information...
2 parents 138fe65 + 40aca01 commit 5c6ed2f651668817deea1394dd11705924cc48ae @nesquena committed Dec 21, 2009
Showing with 64 additions and 8 deletions.
  1. +16 −4 lib/semantic_fields_renderer.rb
  2. +48 −4 lib/semantic_form_builder.rb
View
20 lib/semantic_fields_renderer.rb
@@ -50,8 +50,8 @@ def check_box_tag(name, options = {})
options[:class] = append_class_name(options[:class], 'checkbox')
html << content_tag(:dd) do
checked = options.delete(:value).to_s != 'false'
- @super.check_box_tag(name, "1", checked, options) +
- @super.hidden_field_tag(name, "0")
+ @super.hidden_field_tag(name, "0") +
+ @super.check_box_tag(name, "1", checked, options)
end
end
@@ -120,15 +120,27 @@ def self.create_field_element(input_type)
define_method(field_tag_name) do |name, *args| # defines a method called 'semantic_text_field_tag'
field_helper_method = method(field_tag_name.intern)
options = field_tag_item_options(name, input_type, args[0]) # grab the options hash
+
+ errors = options[:error]
html = content_tag(:dt) do
content_tag(:label , "#{options.delete(:label)}:", :for => options[:id])
end
html << content_tag(:dd) do
html_tag = @super.send(field_tag_name, name, options.delete(:value).to_s, options)
- options.delete(:error) ? ActionView::Base.field_error_proc.call(html_tag, @object) : html_tag
+ errors ? ActionView::Base.field_error_proc.call(html_tag, @object) : html_tag
+ end
+
+ if errors
+ error_message = errors.is_a?(Array) ? errors.join(' and ') : errors
+ html << content_tag(:dt)
+ html << content_tag(:dd) do
+ content_tag(:p, error_message, :class => 'inline-errors')
+ end
end
+
+ html
end
end
@@ -172,4 +184,4 @@ def append_class_name(original_value, new_value)
(original_value || "") << " #{new_value}"
end
end
-end
+end
View
52 lib/semantic_form_builder.rb
@@ -73,8 +73,43 @@ class SemanticBuilder < ActionView::Helpers::FormBuilder
def fieldset(name=nil, options = {}, &block)
@renderer = FieldsRenderer.new(@template) # stores the renderer for later use
@template.semantic_fieldset_tag(name, options, &block)
- end
-
+ end
+
+ def semantic_fields_for(record_or_name_or_array, *args, &block)
+ if options.has_key?(:index)
+ index = "[#{options[:index]}]"
+ elsif defined?(@auto_index)
+ self.object_name = @object_name.to_s.sub(/\[\]$/,"")
+ index = "[#{@auto_index}]"
+ else
+ index = ""
+ end
+
+ if options[:builder]
+ args << {} unless args.last.is_a?(Hash)
+ args.last[:builder] ||= options[:builder]
+ end
+
+ case record_or_name_or_array
+ when String, Symbol
+ if nested_attributes_association?(record_or_name_or_array)
+ return fields_for_with_nested_attributes(record_or_name_or_array, args, block)
+ else
+ name = "#{object_name}#{index}[#{record_or_name_or_array}]"
+ end
+ when Array
+ object = record_or_name_or_array.last
+ name = "#{object_name}#{index}[#{ActionController::RecordIdentifier.singular_class_name(object)}]"
+ args.unshift(object)
+ else
+ object = record_or_name_or_array
+ name = "#{object_name}#{index}[#{ActionController::RecordIdentifier.singular_class_name(object)}]"
+ args.unshift(object)
+ end
+
+ @template.semantic_fields_for(name, *args, &block)
+ end
+
# submit element tag for form within a definition item
#
# <dd class="button">
@@ -187,6 +222,14 @@ def simple_date_select(caption, options)
@template.select_year(year_value, :field_name => options[:year], :prefix => object_name.to_s, :start_year => end_year, :end_year => options[:start_year], :include_blank => options[:include_blank])
end
end
+
+ # constructs regular html selector
+ alias_method :original_select, :select
+ def select(attribute, choices, options = {})
+ label = (options.delete(:label) || attribute.to_s.humanize).gsub(' ', '&nbsp;')
+ html = @template.content_tag("dt", @template.content_tag("label" , "#{label}:", :for => "#{object_name(:id)}_#{attribute}" ))
+ html << @template.content_tag("dd", original_select(attribute, choices, options))
+ end
private
@@ -204,6 +247,7 @@ def simple_date_select(caption, options)
#
def self.create_field_element(input_type_name)
method_name = input_type_name #i.e method will be defined as 'text_field'
+ alias_method "original_#{input_type_name}", input_type_name
template_method = "#{input_type_name}_tag" #i.e text_field_tag
define_method(method_name) do |attribute, *args| # defines a method called 'text_field'
raise "Semantic form fields must be contained within a fieldset!" unless @renderer
@@ -221,7 +265,7 @@ def self.create_field_element(input_type_name)
[ 'text_field', 'password_field', 'file_field', 'text_area', 'check_box' ].each do |type_name|
self.create_field_element(type_name)
end
-
+
# Correctly parses the empty array brackets that can be passed into a form and inserts the id
#
# ex:
@@ -240,4 +284,4 @@ def object_name(id=false)
@object_name.to_s.include?("[]") ? @object_name.gsub(/\[\]/, replace_string) : @object_name
end
end
-end
+end

0 comments on commit 5c6ed2f

Please sign in to comment.
Something went wrong with that request. Please try again.