Sha256: ad4ba810f5195c08e3a424c0fe48564f29bc738900a1118b137e93eb1e97293a

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

module ApiTaster
  class FormBuilder < AbstractController::Base
    include AbstractController::Rendering
    include ActionView::Context
    include ActionView::Helpers::CaptureHelper

    self.view_paths = ApiTaster::Engine.root.join('app/views')

    def initialize(params)
      flush_output_buffer
      @_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

        case value
        when Hash
          add_legend_to_buffer(parent_labels, label)
          add_to_buffer(value, new_parent_labels)
        when Array
          value.each do |v|
            case v
            when 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 => 'api_taster/routes/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 => 'api_taster/routes/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

1 entries across 1 versions & 1 rubygems

Version Path
api_taster-0.7.0 lib/api_taster/form_builder.rb