Sha256: 182355221bc888f0870831bbaf4906900270ee33df4007d98242bbe2ec385956
Contents?: true
Size: 1.99 KB
Versions: 3
Compression:
Stored size: 1.99 KB
Contents
module Effective module FormInputs class SelectOrText < Effective::FormInput attr_accessor :name_text attr_accessor :select_collection attr_accessor :select_options attr_accessor :text_options VISIBLE = {} HIDDEN_AND_DISABLED = { wrapper: { style: 'display: none;' }, disabled: true } 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 def to_html(&block) content_tag(:div, class: 'effective-select-or-text') do @builder.select(name, select_collection, select_options) + @builder.send(email_field? ? :email_field : :text_field, name_text, text_options) + 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 def select? return true if object.errors[name].present? return false if object.errors[name_text].present? value = object.send(name) (value.present? && select_collection.include?(value)) || (value.blank? && object.send(name_text).blank?) end def text? !select? end def email_field? @email_field end def select_options @select_options.merge(select? ? VISIBLE : HIDDEN_AND_DISABLED) end def text_options @text_options.merge(text? ? VISIBLE : HIDDEN_AND_DISABLED) end end end end
Version data entries
3 entries across 3 versions & 1 rubygems