Sha256: 7f4eed6e3126aa579eda9a7975582cdadca9167e6f416770106b58d4d4c1bfd7

Contents?: true

Size: 1.9 KB

Versions: 7

Compression:

Stored size: 1.9 KB

Contents

module AbstractInterface
  class ThemedFormHelper
    attr_accessor :template
    
    def initialize template
      self.template = template
    end
    
    def error_messages *errors
      errors = errors.first if errors.size == 1 and errors.first.is_a? Array
      template.render template.themed_partial('forms/errors'), :object => errors
    end
    
    def form_field options, &block
      html_options = options.to_openobject
      options = OpenObject.new
      
      # prepare options
      %w(errors label description required theme).each do |k| 
        v = html_options.delete k
        options[k] = v unless v.nil?
      end
      options.errors = options.errors.to_a

      # CSS style
      html_options.class ||= ""
      html_options << " themed_input"
      
      options.content = template.capture{block.call(html_options)}
      
      html = template.render(template.themed_partial('forms/field'), :object => options)
      template.concat html
    end
    
    def line *items
      template.render template.themed_partial('forms/line'), :object => {:items => items, :delimiter => false}.to_openobject
    end
    
    def line_with_delimiters *items
      template.render template.themed_partial('forms/line'), :object => {:items => items, :delimiter => true}.to_openobject
    end
    
    
    # 
    # Form fields
    # 
    %w(
      check_box_tag
      field_set_tag
      file_field_tag      
      password_field_tag
      radio_button_tag
      select_tag
      text_field_tag
      text_area_tag        
    ).each do |m|
      define_method m do |*args|
        options = args.extract_options!
        template.capture do
          form_field options do |html_options|
            args << html_options
            template.concat(template.send(m, *args))
          end
        end
      end
    end
    
    %w(
      hidden_field_tag
      submit_tag
    ).each{|m| delegate m, :to => :template}
    
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
abstract_interface-0.1.7 lib/abstract_interface/themed_form_helper.rb
abstract_interface-0.1.6 lib/abstract_interface/themed_form_helper.rb
abstract_interface-0.1.5 lib/abstract_interface/themed_form_helper.rb
abstract_interface-0.1.3 lib/abstract_interface/themed_form_helper.rb
abstract_interface-0.1.1 lib/abstract_interface/themed_form_helper.rb
abstract_interface-0.1.0 lib/abstract_interface/themed_form_helper.rb
abstract-interface-0.0.3 lib/abstract_interface/themed_form_helper.rb