Class: Effective::FormInputs::SelectOrText

Inherits:
Effective::FormInput show all
Defined in:
app/models/effective/form_inputs/select_or_text.rb

Constant Summary collapse

VISIBLE =
{}
HIDDEN =
{ wrapper: { style: 'display: none;' } }

Constants inherited from Effective::FormInput

Effective::FormInput::BLANK, Effective::FormInput::DEFAULT_FEEDBACK_OPTIONS, Effective::FormInput::DEFAULT_INPUT_GROUP_OPTIONS, Effective::FormInput::EMPTY_HASH, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES, Effective::FormInput::HORIZONTAL_LABEL_OPTIONS, Effective::FormInput::HORIZONTAL_WRAPPER_OPTIONS, Effective::FormInput::INLINE_LABEL_OPTIONS, Effective::FormInput::VERTICAL_WRAPPER_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Effective::FormInput

#name, #options

Instance Method Summary collapse

Methods inherited from Effective::FormInput

#feedback_options, #hint_options, #input_group_options, #input_html_options, #input_js_options, #label_options, #wrapper_options

Constructor Details

#initialize(name, options, builder:) ⇒ SelectOrText

Returns a new instance of SelectOrText.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/effective/form_inputs/select_or_text.rb', line 14

def initialize(name, options, builder:)
  @name_text = options.delete(:name_text) || raise('Please include a text method name')
  @select_collection = options.delete(:collection) || raise('Please include a collection')

  @select_options = { placeholder: 'Please choose, or...', required: false }
    .merge(options[:select] || options.presence || {})

  @text_options = { placeholder: 'Enter freeform', required: false }
    .merge(options[:text] || options[:text_field] || options.presence || {})

  @email_field = options.fetch(:email, name_text.to_s.include?('email'))

  super
end

Instance Attribute Details

#name_textObject

Returns the value of attribute name_text.



6
7
8
# File 'app/models/effective/form_inputs/select_or_text.rb', line 6

def name_text
  @name_text
end

#select_collectionObject

Returns the value of attribute select_collection.



7
8
9
# File 'app/models/effective/form_inputs/select_or_text.rb', line 7

def select_collection
  @select_collection
end

#select_optionsObject

Returns the value of attribute select_options.



8
9
10
# File 'app/models/effective/form_inputs/select_or_text.rb', line 8

def select_options
  @select_options
end

#text_optionsObject

Returns the value of attribute text_options.



9
10
11
# File 'app/models/effective/form_inputs/select_or_text.rb', line 9

def text_options
  @text_options
end

Instance Method Details

#email_field?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/effective/form_inputs/select_or_text.rb', line 58

def email_field?
  @email_field
end

#select?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/effective/form_inputs/select_or_text.rb', line 42

def select?
  return true if object.errors[name].present?
  return false if object.errors[name_text].present?

  value = object.send(name)

  return true if (value.present? && select_collection.include?(value))
  return true if (value.to_i > 0 && select_collection.find { |obj| obj.respond_to?(:id) && obj.id == value.to_i })

  return true if value.blank? && object.send(name_text).blank?
end

#text?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/effective/form_inputs/select_or_text.rb', line 54

def text?
  !select?
end

#to_html(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/effective/form_inputs/select_or_text.rb', line 29

def to_html(&block)
  (:div, class: 'effective-select-or-text') do
    if select?
      @builder.send(email_field? ? :email_field : :text_field, name_text, text_options) +
      @builder.select(name, select_collection, select_options)
    else
      @builder.select(name, select_collection, select_options) +
      @builder.send(email_field? ? :email_field : :text_field, name_text, text_options)
    end +
    link_to(icon('rotate-ccw'), '#', class: 'effective-select-or-text-switch', title: 'Switch between choice and freeform', 'data-effective-select-or-text': true)
  end
end