Class: Trestle::Form::Fields::Select
- Inherits:
-
Trestle::Form::Field
- Object
- Trestle::Form::Field
- Trestle::Form::Fields::Select
- Defined in:
- lib/trestle/form/fields/select.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Choices
Instance Attribute Summary collapse
-
#choices ⇒ Object
readonly
Returns the value of attribute choices.
-
#html_options ⇒ Object
readonly
Returns the value of attribute html_options.
Attributes inherited from Trestle::Form::Field
#block, #builder, #name, #options, #template
Instance Method Summary collapse
- #clearable? ⇒ Boolean
- #default_choices ⇒ Object
- #default_html_options ⇒ Object
- #field ⇒ Object
-
#initialize(builder, template, name, choices = nil, options = {}, html_options = {}, &block) ⇒ Select
constructor
A new instance of Select.
- #placeholder ⇒ Object
Methods inherited from Trestle::Form::Field
#defaults, #disabled?, #errors, #form_group, #normalize_options!, #readonly?, #render
Constructor Details
#initialize(builder, template, name, choices = nil, options = {}, html_options = {}, &block) ⇒ Select
Returns a new instance of Select.
7 8 9 10 11 12 13 14 |
# File 'lib/trestle/form/fields/select.rb', line 7 def initialize(builder, template, name, choices=nil, ={}, ={}, &block) super(builder, template, name, , &block) @choices = choices || default_choices @choices = Choices.new(@choices) if @choices.nil? || @choices.is_a?(Enumerable) @html_options = .merge() end |
Instance Attribute Details
#choices ⇒ Object (readonly)
Returns the value of attribute choices.
5 6 7 |
# File 'lib/trestle/form/fields/select.rb', line 5 def choices @choices end |
#html_options ⇒ Object (readonly)
Returns the value of attribute html_options.
5 6 7 |
# File 'lib/trestle/form/fields/select.rb', line 5 def @html_options end |
Instance Method Details
#clearable? ⇒ Boolean
40 41 42 |
# File 'lib/trestle/form/fields/select.rb', line 40 def clearable? [:allow_clear] || [:prompt].present? || [:include_blank].present? end |
#default_choices ⇒ Object
24 25 26 |
# File 'lib/trestle/form/fields/select.rb', line 24 def default_choices builder.object.send(name) if builder.object end |
#default_html_options ⇒ Object
20 21 22 |
# File 'lib/trestle/form/fields/select.rb', line 20 def Trestle::Options.new(class: ["form-select"], disabled: disabled? || readonly?, data: { controller: "select", placeholder: placeholder, allow_clear: clearable? }) end |
#field ⇒ Object
16 17 18 |
# File 'lib/trestle/form/fields/select.rb', line 16 def field builder.raw_select(name, choices, , , &block) end |
#placeholder ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/trestle/form/fields/select.rb', line 28 def placeholder prompt = [:prompt] || [:include_blank] if prompt.kind_of?(String) prompt elsif [:prompt] I18n.translate("helpers.select.prompt", default: "Please select") elsif [:include_blank] "" end end |