class Trestle::Form::Field
Attributes
block[R]
builder[R]
name[R]
options[R]
template[R]
Public Class Methods
new(builder, template, name, options={}, &block)
click to toggle source
# File lib/trestle/form/field.rb, line 8 def initialize(builder, template, name, options={}, &block) @builder, @template, @name, @block = builder, template, name, block assign_options!(options) normalize_options! end
Public Instance Methods
defaults()
click to toggle source
# File lib/trestle/form/field.rb, line 39 def defaults Trestle::Options.new(readonly: readonly?) end
disabled?()
click to toggle source
# File lib/trestle/form/field.rb, line 43 def disabled? options[:disabled] end
errors()
click to toggle source
# File lib/trestle/form/field.rb, line 15 def errors error_keys.map { |key| builder.errors(key) }.flatten end
field()
click to toggle source
# File lib/trestle/form/field.rb, line 35 def field raise NotImplementedError end
form_group(opts={}) { || ... }
click to toggle source
# File lib/trestle/form/field.rb, line 19 def form_group(opts={}) if @wrapper @builder.form_group(name, @wrapper.merge(opts)) do yield end else yield end end
normalize_options!()
click to toggle source
# File lib/trestle/form/field.rb, line 51 def normalize_options! extract_wrapper_options! assign_error_class! end
readonly?()
click to toggle source
# File lib/trestle/form/field.rb, line 47 def readonly? options[:readonly] || admin.readonly? end
render()
click to toggle source
# File lib/trestle/form/field.rb, line 29 def render form_group do field end end
Protected Instance Methods
assign_error_class!()
click to toggle source
# File lib/trestle/form/field.rb, line 72 def assign_error_class! @options[:class] = Array(@options[:class]) @options[:class] << error_class if errors.any? end
assign_options!(options)
click to toggle source
# File lib/trestle/form/field.rb, line 57 def assign_options!(options) # Assign @options first so that it can be referenced from within #defaults if required @options = Trestle::Options.new(options) @options = defaults.merge(options) end
error_class()
click to toggle source
# File lib/trestle/form/field.rb, line 77 def error_class "is-invalid" end
error_keys()
click to toggle source
# File lib/trestle/form/field.rb, line 81 def error_keys keys = [name] # Singular associations (belongs_to) keys << name.to_s.sub(/_id$/, '') if name.to_s =~ /_id$/ # Collection associations (has_many / has_and_belongs_to_many) keys << name.to_s.sub(/_ids$/, 's') if name.to_s =~ /_ids$/ keys end
extract_options(*keys)
click to toggle source
# File lib/trestle/form/field.rb, line 93 def extract_options(*keys) extracted = Trestle::Options.new keys.each { |k| extracted[k] = options.delete(k) if options.key?(k) } extracted end
extract_wrapper_options!()
click to toggle source
# File lib/trestle/form/field.rb, line 63 def extract_wrapper_options! wrapper = options.delete(:wrapper) unless wrapper == false @wrapper = extract_options(*Fields::FormGroup::WRAPPER_OPTIONS) @wrapper.merge!(wrapper) if wrapper.is_a?(Hash) end end