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
28
29
30
31
32
33
34
35
# 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')

  @shared_options = (options[:input_html] || {})

  @select_options = { 
    placeholder: '', 
    hint: "Can't find your #{(options[:name] || name).to_s.chomp('_id')}? <a class='effective-select-or-text-switch' title='Switch to enter freeform' data-effective-select-or-text='true' href='#'>Click here</a> to add one",
    required: false,
  }.merge(options[:select] || options.presence || {}).merge(@shared_options)

  @text_options = { 
    placeholder: '', 
    hint: "Looking for an existing #{(options[:name] || name).to_s.chomp('_id')}? <a class='effective-select-or-text-switch' title='Switch to search for existing' data-effective-select-or-text='true' href='#'>Click here</a> to search",
    required: false 
  }.merge(options[:text] || options[:text_field] || options.presence || {}).merge(@shared_options)

  @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)


67
68
69
# File 'app/models/effective/form_inputs/select_or_text.rb', line 67

def email_field?
  @email_field
end

#select?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/effective/form_inputs/select_or_text.rb', line 51

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)


63
64
65
# File 'app/models/effective/form_inputs/select_or_text.rb', line 63

def text?
  !select?
end

#to_html(&block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/effective/form_inputs/select_or_text.rb', line 37

def to_html(&block)
  wrapper_class = ['effective-select-or-text', (select? ? 'select-enabled' : 'text-enabled'), options.dig(:wrapper, :class)].compact.join(' ')

  (:div, (options[:wrapper] || {}).merge(class: wrapper_class)) 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
  end
end