module FormattedForm class Builder < ActionView::Helpers::FormBuilder %w(text_field password_field text_area file_field datetime_select date_select).each do |field_name| define_method field_name do |method, *args| options = args.detect { |a| a.is_a?(Hash) } || {} render_field(field_name, method, options) { super(method, options) } end end def select(method, choices, options = {}, html_options = {}) render_field('select', method, options) { super(method, choices, options, html_options) } end def hidden_field(method, options = {}, html_options = {}) super(method, options) end def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") render_field('check_box', method, options) do super(method, options, checked_value, unchecked_value) end end def radio_button(method, tag_value, options = {}) case tag_value when Array choices = tag_value.collect do |choice| if !choice.is_a?(Array) choice = [choice, choice] elsif choice.length == 1 choice << choice[0] end { :field => super(method, choice[1], options), :label => label_for("#{method}_#{choice[1].to_s.gsub(' ', '_').underscore}", :label => choice[0]) } end else choices = [{ :field => super(method, tag_value), :label => label_for("#{method}_#{tag_value.to_s.gsub(' ', '_').underscore}", :label => tag_value) }] end @template.render(:partial => "formatted_form/radio_button", :locals => { :field_name => 'radio_button', :label_text => label_for(method, options), :choices => choices, :required => options.delete(:required), :before_text => @template.raw(options.delete(:before_text)), :after_text => @template.raw(options.delete(:after_text)), :description => @template.raw(options.delete(:desc)), :error_messages => error_messages_for(method) }) end # f.submit 'Log In', :change_to_text => 'Logging you in ...' def submit(value, options={}, &block) cancel_link = @template.capture(&block) if block_given? cancel_link ||= options[:cancel_url] ? ' or ' + options.delete(:cancel_url) : '' change_to_text = options.delete(:change_to_text) out = @template.content_tag(:div, :class => "form_element submit#{' change_to_text' if change_to_text}") do if options.delete(:image) content = super(value, options.merge(:style=>'visibility:hidden;position: absolute')) content << @template.link_to(@template.content_tag(:span, value), "#", :class => 'submit_image') content << cancel_link.html_safe else super(value, options) + cancel_link.html_safe end end if change_to_text out << @template.content_tag(:div, change_to_text, :class => 'form_element submit_text') end out.html_safe end # generic container for all things form def element(label = ' ', value = '', type = 'text_field', &block) value += @template.capture(&block) if block_given? %{