Permalink
Browse files

added semantic_fields_for method for working with model nested attrib…

…utes
  • Loading branch information...
1 parent 88ceaca commit 164bf84b85b45242b85d1ff52bc0cffeab06b3e7 @ptzn ptzn committed Dec 20, 2009
Showing with 36 additions and 1 deletion.
  1. +36 −1 lib/semantic_form_builder.rb
@@ -73,7 +73,42 @@ 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
#

0 comments on commit 164bf84

Please sign in to comment.