Sha256: 6190e6099f5c76c4d44b2b9319ba00376dd21d0326e2059d55a4fbf3fe748cfc

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

module Lurker
  class FormBuilder < RenderingController
    def initialize(params)
      @_buffer = ''
      add_to_buffer(params)
    end

    def html
      "<legend class=\"hero-legend\"></legend>#{@_buffer}"
    end

    private

    def add_to_buffer(params, parent_labels = [])
      params.each do |label, value|
        if parent_labels.present?
          label = "[#{label}]"
        end

        new_parent_labels = parent_labels.clone << label

        if value.is_a?(Hash)
          add_legend_to_buffer(parent_labels, label)

          add_to_buffer(value, new_parent_labels)
        elsif value.is_a?(Array)
          value.each do |v|
            if v.is_a?(Hash)
              add_legend_to_buffer(parent_labels, label)

              add_to_buffer(v, parent_labels.clone << "#{label}[]")
            else
              add_element_to_buffer(parent_labels, "#{label}[]", v)
            end
          end
        else
          add_element_to_buffer(parent_labels, label, value)
        end
      end
    end

    def add_element_to_buffer(parent_labels, label, value)
      @_buffer += render(
        :partial => 'param_form_element',
        :locals  => {
          :label      => "#{print_labels(parent_labels)}#{label}",
          :label_text => label,
          :value      => value
        }
      )
    end

    def add_legend_to_buffer(parent_labels, label)
      @_buffer += render(
        :partial => 'param_form_legend',
        :locals  => { :label => print_labels(parent_labels.clone << label) }
      )
    end

    def print_labels(parent_labels)
      "#{parent_labels * ''}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lurker-0.5.1 lib/lurker/form_builder.rb
lurker-0.5.0 lib/lurker/form_builder.rb