Class: Effective::FormLogic

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/form_logic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, builder:) ⇒ FormLogic

So this takes in the options for an entire form group.



11
12
13
14
15
16
17
18
# File 'app/models/effective/form_logic.rb', line 11

def initialize(*args, builder:)
  @builder = builder
  @template = builder.template
  @options ||= {}

  validate!(args)
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



5
6
7
# File 'app/models/effective/form_logic.rb', line 5

def args
  @args
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'app/models/effective/form_logic.rb', line 5

def options
  @options
end

Instance Method Details

#input_js_optionsObject



38
39
40
# File 'app/models/effective/form_logic.rb', line 38

def input_js_options
  { 'data-input-js-options' => JSON.generate({method_name: input_js_options_method_name}.merge!(logic_options)) }
end

#input_js_options_method_nameObject



42
43
44
# File 'app/models/effective/form_logic.rb', line 42

def input_js_options_method_name
  "effective_#{self.class.name.split('::').last.underscore}"
end

#tag_name(name, multiple = false, index = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/effective/form_logic.rb', line 25

def tag_name(name, multiple = false, index = nil)
  sanitized_method_name = name.to_s.sub(/\?$/, "")

  case
  when @builder.object_name.empty?
    "#{sanitized_method_name}#{multiple ? "[]" : ""}"
  when index
    "#{@builder.object_name}[#{index}][#{sanitized_method_name}]#{multiple ? "[]" : ""}"
  else
    "#{@builder.object_name}[#{sanitized_method_name}]#{multiple ? "[]" : ""}"
  end
end

#validate!(args) ⇒ Object



20
21
22
# File 'app/models/effective/form_logic.rb', line 20

def validate!(args)
  raise 'expected one or more argument' if args.blank?
end