Sha256: 44c10a1bfeeb1bb916d16ca92e0b2dc8a3c3a1425938ab532181855e80f88a6a

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module Form
  module Component
    class Base
      extend Forwardable

      def_delegators :form, :data

      # The +Form::Builder+ instance. It will provide the base name
      # and data object.
      #
      attr_accessor :form

      # The input name. It will be used to compose
      # the full name.
      #
      attr_accessor :name

      # Hold the input options. It will be used as the input's
      # attributes.
      #
      attr_accessor :options

      # Initialize the component and set some variables.
      #
      def initialize(form, name, options = {})
        @form = form
        @name = name
        @options = options
      end

      # Retrieve the value from the form data.
      #
      def value
        if data.respond_to?(name)
          data.public_send(name)
        elsif data.respond_to?(:[])
          data[name.to_sym] || data[name.to_s]
        end
      end

      # Return a composed name like <tt>user[profile][twitter]</tt>.
      #
      def composed_name
        composed_name = [form.base_name, name].flatten.compact
        "#{composed_name.shift}" + composed_name.map {|n| "[#{n}]"}.join("")
      end

      # Just pass all arguments to <tt>I18n.t</tt>.
      #
      def t(*args)
        I18n.t(*args)
      end

      #
      #
      def humanize(name)
        parts = name.to_s.split("_")
        parts.first.gsub!(/\A(.)/) { $1.upcase }
        parts.join(" ")
      end

      #
      #
      def id_attribute
        [form.base_name, name].flatten.compact.join("-")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
form-0.0.1.alpha1 lib/form/component/base.rb