Class: HungryForm::Elements::Base::Element

Inherits:
Object
  • Object
show all
Includes:
Hashable
Defined in:
lib/hungryform/elements/base/element.rb

Overview

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

Direct Known Subclasses

ActiveElement, Group, Html

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hashable

included, #to_hash

Constructor Details

#initialize(name, parent, resolver, attributes = {}) ⇒ Element

Returns a new instance of Element.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hungryform/elements/base/element.rb', line 13

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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/hungryform/elements/base/element.rb', line 8

def attributes
  @attributes
end

#dependencyObject

Returns the value of attribute dependency.



8
9
10
# File 'lib/hungryform/elements/base/element.rb', line 8

def dependency
  @dependency
end

#labelObject

Returns the value of attribute label.



8
9
10
# File 'lib/hungryform/elements/base/element.rb', line 8

def label
  @label
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/hungryform/elements/base/element.rb', line 8

def name
  @name
end

#placeholdersObject

Returns the value of attribute placeholders.



8
9
10
# File 'lib/hungryform/elements/base/element.rb', line 8

def placeholders
  @placeholders
end

#resolverObject

Returns the value of attribute resolver.



8
9
10
# File 'lib/hungryform/elements/base/element.rb', line 8

def resolver
  @resolver
end

#visibleObject Also known as: visible?

Returns the value of attribute visible.



8
9
10
# File 'lib/hungryform/elements/base/element.rb', line 8

def visible
  @visible
end

Instance Method Details

#configurationObject

Configuration params specific to the element



47
48
49
# File 'lib/hungryform/elements/base/element.rb', line 47

def configuration
  HungryForm.configuration.send(self.class.name.demodulize.underscore)
end

#dependency_jsonObject



42
43
44
# File 'lib/hungryform/elements/base/element.rb', line 42

def dependency_json
  JSON.generate(dependency) if dependency
end