module Storefront class Form class Builder < Storefront::Form::Base delegate :object, :parent, :to => :model def fieldset(*args, &block) options = default_options!(args.extract_options!) options[:label] ||= args.shift Storefront::Form::Fieldset.new(options).render(&block) end def fields(*args, &block) options = args.extract_options! options[:as] = :fields options[:label] ||= false attr_name = args.shift || attribute.name template.capture_haml do result = field attr_name, options do |_field| template.haml_concat fieldset(&block).gsub(/\n$/, "") end template.haml_concat result.gsub(/\n$/, "") end end def fields_for(*args, &block) options = args.extract_options! attribute = args.shift macro = model.macro_for(attribute) attr_name = nil if options[:as] == :object attr_name = attribute.to_s else attr_name = config.rename_nested_attributes ? "#{attribute}_attributes" : attribute.to_s end # do something here for counts sub_parent = model.object sub_object = args.shift index = options.delete(:index) unless index.present? && index.is_a?(::String) if sub_object.blank? && index.present? sub_object = sub_parent.send(attribute)[index] elsif index.blank? && sub_object.present? && macro == :has_many index = sub_parent.send(attribute).index(sub_object) end end sub_object ||= model.default(attribute) || model.to_s.camelize.constantize.new keys = [model.keys, attr_name] model = Storefront::ModelProxy.new( :object => sub_object, :parent => sub_parent, :keys => keys ) options.merge!( :template => template, :model => model, :parent_index => index, :access_keys => access_keys, :tabindex => tabindex ) Storefront::Form::Builder.new(options).render(&block) end def field(*args, &block) options = args.extract_options! attribute_name = args.shift || attribute.name attribute = Storefront::AttributeProxy.new( :name => attribute_name, :model => @model, :required => options[:required] == true, :disabled => options[:disabled] == true, :top_level => options[:attribute] == false ) options.reverse_merge!( :template => template, :model => model, :attribute => attribute, :parent_index => parent_index, :index => index, :field_html => options[:field_html] || {}, :input_html => options[:input_html] || {}, :label_html => options[:label_html] || {}, :error_html => options[:error_html] || {}, :hint_html => options[:hint_html] || {} ) Storefront::Form::Field.new(options).render(&block) end def button(*args, &block) options = args.extract_options! options.reverse_merge!(:as => :submit) options[:value] = args.shift || "Submit" field(options[:value], options, &block) end def submit(*args, &block) template.capture_haml do result = fieldset :class => config.submit_fieldset_class do |fields| template.haml_concat fields.button(*args).gsub(/\n$/, "") yield(fields) if block_given? end template.haml_concat result.gsub(/\n$/, "") end end def partial(path, options = {}) @template.render :partial => path, :locals => options.merge(:fields => self) end def render(&block) yield(self) end end end end