Class: Effective::FormLogics::ShowIfAny

Inherits:
Effective::FormLogic show all
Defined in:
app/models/effective/form_logics/show_if_any.rb

Instance Attribute Summary

Attributes inherited from Effective::FormLogic

#args

Instance Method Summary collapse

Methods inherited from Effective::FormLogic

#initialize, #input_js_options, #input_js_options_method_name, #tag_name

Constructor Details

This class inherits a constructor from Effective::FormLogic

Instance Method Details

#input_logic_optionsObject



27
28
29
# File 'app/models/effective/form_logics/show_if_any.rb', line 27

def input_logic_options
  args.third.kind_of?(Hash) ? args.third : {}
end

#logic_optionsObject



23
24
25
# File 'app/models/effective/form_logics/show_if_any.rb', line 23

def logic_options
  { name: tag_name(args.first), value: args.second.to_json, needDisable: !show? }.merge(input_logic_options)
end

#optionsObject



19
20
21
# File 'app/models/effective/form_logics/show_if_any.rb', line 19

def options
  { style: ('display: none;' unless show?) }
end

#show?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'app/models/effective/form_logics/show_if_any.rb', line 36

def show?
  Array(args.second).any? do |value|
    selected = object.try(args.first)
    selected == value || selected.to_s == value.to_s
  end
end

#to_html(&block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/effective/form_logics/show_if_any.rb', line 7

def to_html(&block)
  disabled_was = @builder.disabled

  @builder.disabled = true unless show?

  content = (:div, options.merge(input_js_options), &block)

  @builder.disabled = disabled_was

  content
end

#validate!(args) ⇒ Object



31
32
33
34
# File 'app/models/effective/form_logics/show_if_any.rb', line 31

def validate!(args)
  return if args.third.kind_of?(Hash) && args.third[:validate] == false
  raise "expected object to respond to #{args.first}" unless object.respond_to?(args.first)
end