Sha256: f3ba379b77aa3d5cd06a0ee3bfdff0aa50cbbab408032c54430083b5cfa79164

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module Trestle
  class Form
    class Field
      attr_reader :builder, :template, :name, :options, :block

      delegate :admin, :content_tag, :concat, :safe_join, :icon, to: :template

      def initialize(builder, template, name, options={}, &block)
        @builder, @template, @name, @block = builder, template, name, block

        @options = defaults.merge(options)
        extract_options!
      end

      def errors
        errors = builder.object.errors[name]
        errors += builder.object.errors[name.to_s.sub(/_id$/, '')] if name.to_s =~ /_id$/
        errors
      end

      def form_group(opts={})
        @builder.form_group(name, @wrapper.merge(opts)) do
          yield
        end
      end

      def render
        form_group do
          field
        end
      end

      def field
        raise NotImplementedError
      end

      def defaults
        Trestle::Options.new(readonly: admin.readonly?)
      end

      def extract_options!
        @wrapper = extract_wrapper_options(:help, :label, :hide_label).merge(options.delete(:wrapper))
      end

    private
      def extract_wrapper_options(*keys)
        wrapper = Trestle::Options.new
        keys.each { |k| wrapper[k] = options.delete(k) if options.key?(k) }
        wrapper
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trestle-0.8.3 lib/trestle/form/field.rb
trestle-0.8.2 lib/trestle/form/field.rb
trestle-0.8.0 lib/trestle/form/field.rb