Sha256: b6f4093c47f62b6c33b168ee190cd370975a1681272f8f68388c5ffc718b3c27

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

module HungryForm
  module Elements
    module Base
      # The Element class is used in every form element. It contains the attrs
      # and methods used by all form elements, such as name, visible, dependency etc
      class Element
        include HungryForm::Elements::Base::Hashable
        attr_accessor :name, :placeholders, :resolver, :visible, :label, :dependency, :attributes
        alias_method :visible?, :visible

        hashable :visible, :dependency, :name, :label

        def initialize(name, parent, resolver, attributes = {})
          @attributes = configuration.dup
          @attributes.merge!(attributes)

          @placeholders ||= {}
          @resolver = resolver

          self.dependency ||= @attributes.delete(:dependency)

          # The element is visible if no visible parameter passed or
          # visible param equals true and the dependency is resolved positively
          self.visible = @attributes.key?(:visible) ? @attributes.delete(:visible) : true

          if dependency
            self.visible &&= resolver.resolve_dependency(dependency)
          end

          # An element's name is prefixed with all parents names up to the page
          self.name = resolver.get_value(name, self)
          self.name = "#{parent.name}_#{name}" unless parent.nil?
          
          # Label can be created from name if there is no label given
          if @attributes[:label]
            self.label = resolver.get_value(@attributes.delete(:label), self)
          else
            self.label = resolver.get_value(name, self).humanize
          end
        end

        def dependency_json
          JSON.generate(dependency) if dependency
        end

        # Configuration params specific to the element
        def configuration
          HungryForm.configuration.send(self.class.name.demodulize.underscore)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hungryform-0.0.11 lib/hungryform/elements/base/element.rb
hungryform-0.0.10 lib/hungryform/elements/base/element.rb