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 |
# 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') @select_options = { placeholder: 'Please choose, or...', required: false } .merge([:select] || .presence || {}) @text_options = { placeholder: 'Enter freeform', required: false } .merge([:text] || [:text_field] || .presence || {}) @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
58 59 60 |
# File 'app/models/effective/form_inputs/select_or_text.rb', line 58 def email_field? @email_field end |
#select? ⇒ 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
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) content_tag(:div, class: 'effective-select-or-text') 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 + 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 |