Class: Caisson::Helpers::Form::Builder::FieldBuilder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/caisson/helpers/form/builder/field_builder/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core, name, options = {}) ⇒ Base

************************************************************************************* CONSTRUCTOR *************************************************************************************



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 15

def initialize(core, name, options={})
  options.reverse_merge!(nature: nil, hint: true)

  @core = core
  @name = name
  @options = options.dup
  @record = core.record
  @show_hint = options[:hint]

  @nature = Caisson::Helpers::Form::Builder::FieldBuilder::Nature.new(@record, name).get(force: options[:nature])
end

Instance Attribute Details

#coreObject (readonly)

Returns the value of attribute core.



7
8
9
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 7

def core
  @core
end

#natureObject (readonly)

Returns the value of attribute nature.



6
7
8
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 6

def nature
  @nature
end

Instance Method Details

#buildObject

************************************************************************************* PUBLIC INSTANCE METHODS *************************************************************************************



30
31
32
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 30

def build
  "Caisson::Helpers::Form::Builder::Field::#{@nature.camelize}".constantize.new(@core, @record, @name, @options).build
end

#checkbox?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 34

def checkbox?
  nature == 'checkbox'
end

#errorsObject



42
43
44
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 42

def errors
  interpreti.field_errors(@name)
end

#errors?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 38

def errors?
  not errors.empty?
end

#hidden?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 46

def hidden?
  @nature == 'hidden'
end

#hint?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 50

def hint?
  not interpreti.field_hint(@name).blank? and @show_hint
end

#labelObject



54
55
56
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 54

def label
  interpreti.field_label(@name)
end

#wrap_field(field_tags) ⇒ Object



58
59
60
61
62
63
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 58

def wrap_field(field_tags)
  css_class = ['field']
  css_class << 'errors' if errors?

  (:div, field_tags + wrapper_hint + wrapper_errors, class: css_class.join(' '))
end

#wrapper_errorsObject



65
66
67
68
69
70
71
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 65

def wrapper_errors
  if errors?
    (:div, errors.map{|e| (:span, e)}.join.html_safe, class: 'error-messages')
  else
    ''
  end
end

#wrapper_hintObject



73
74
75
76
77
78
79
# File 'lib/caisson/helpers/form/builder/field_builder/base.rb', line 73

def wrapper_hint
  if hint?
    (:div, interpreti.field_hint(@name).html_safe, class: 'hint')
  else
    ''
  end
end