Sha256: 6945f923b8bde34a3081cd32e13cbf7aca7f759928484d74a59ef13f3f93847b

Contents?: true

Size: 1.59 KB

Versions: 12

Compression:

Stored size: 1.59 KB

Contents

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

    def html
      @_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 => "#{print_labels(parent_labels)}#{label}",
          :value      => value
        }
      )
    end

    def add_legend_to_buffer(parent_labels, label)
      return
      @_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

12 entries across 12 versions & 1 rubygems

Version Path
lurker-0.6.8 lib/lurker/form_builder.rb
lurker-0.6.7 lib/lurker/form_builder.rb
lurker-0.6.6 lib/lurker/form_builder.rb
lurker-0.6.5 lib/lurker/form_builder.rb
lurker-0.6.4 lib/lurker/form_builder.rb
lurker-0.6.3 lib/lurker/form_builder.rb
lurker-0.6.2 lib/lurker/form_builder.rb
lurker-0.6.1 lib/lurker/form_builder.rb
lurker-0.5.7 lib/lurker/form_builder.rb
lurker-0.6.0 lib/lurker/form_builder.rb
lurker-0.5.6 lib/lurker/form_builder.rb
lurker-0.5.5 lib/lurker/form_builder.rb