Class: Effective::FormInputs::SelectOrText
- Inherits:
-
Effective::FormInput
- Object
- Effective::FormInput
- Effective::FormInputs::SelectOrText
- 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
-
#name_text ⇒ Object
Returns the value of attribute name_text.
-
#select_collection ⇒ Object
Returns the value of attribute select_collection.
-
#select_options ⇒ Object
Returns the value of attribute select_options.
-
#text_options ⇒ Object
Returns the value of attribute text_options.
Attributes inherited from Effective::FormInput
Instance Method Summary collapse
- #email_field? ⇒ Boolean
-
#initialize(name, options, builder:) ⇒ SelectOrText
constructor
A new instance of SelectOrText.
- #select? ⇒ Boolean
- #text? ⇒ Boolean
- #to_html(&block) ⇒ Object
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, , builder:) @name_text = .delete(:name_text) || raise('Please include a text method name') @select_collection = .delete(:collection) || raise('Please include a collection') @shared_options = ([:input_html] || {}) @select_options = { placeholder: '', hint: "Can't find your #{([: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([:select] || .presence || {}).merge(@shared_options) @text_options = { placeholder: '', hint: "Looking for an existing #{([: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([:text] || [:text_field] || .presence || {}).merge(@shared_options) @email_field = .fetch(:email, name_text.to_s.include?('email')) super end |
Instance Attribute Details
#name_text ⇒ Object
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_collection ⇒ Object
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_options ⇒ Object
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 end |
#text_options ⇒ Object
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 end |
Instance Method Details
#email_field? ⇒ Boolean
67 68 69 |
# File 'app/models/effective/form_inputs/select_or_text.rb', line 67 def email_field? @email_field end |
#select? ⇒ 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
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'), .dig(:wrapper, :class)].compact.join(' ') content_tag(:div, ([:wrapper] || {}).merge(class: wrapper_class)) do if select? @builder.send(email_field? ? :email_field : :text_field, name_text, ) + @builder.select(name, select_collection, ) else @builder.select(name, select_collection, ) + @builder.send(email_field? ? :email_field : :text_field, name_text, ) end end end |