Class: SimpleFormDependentFields::DependentFieldsBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/simple_form_dependent_fields/dependent_fields_builder.rb

Constant Summary collapse

BASE_DOM_CLASS =
'simple_form_dependent_fields'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, template, options = {}) ⇒ DependentFieldsBuilder

Returns a new instance of DependentFieldsBuilder.



9
10
11
12
13
# File 'lib/simple_form_dependent_fields/dependent_fields_builder.rb', line 9

def initialize(builder, template, options = {})
  @builder = builder
  @template = template
  @options = options
end

Instance Attribute Details

#builderObject

Returns the value of attribute builder.



5
6
7
# File 'lib/simple_form_dependent_fields/dependent_fields_builder.rb', line 5

def builder
  @builder
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/simple_form_dependent_fields/dependent_fields_builder.rb', line 5

def options
  @options
end

#templateObject

Returns the value of attribute template.



5
6
7
# File 'lib/simple_form_dependent_fields/dependent_fields_builder.rb', line 5

def template
  @template
end

Instance Method Details

#dependent_fields(&block) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/simple_form_dependent_fields/dependent_fields_builder.rb', line 17

def dependent_fields(&block)
  html = template.capture(&block)

  tag_html = condition_valid? ? html : nil
  data_template_html = condition_valid? ? {} : { template_html: CGI.escapeHTML(html).html_safe }

  template. :div, tag_html, class: [dom_class, BASE_DOM_CLASS].reject(&:blank?).flatten, data: dom_data.merge(data_template_html)
end