class Yaks::Resource::Form::Field

Public Instance Methods

value() click to toggle source
# File lib/yaks/resource/form/field.rb, line 8
def value
  if type.equal? :select
    selected = options.find(&:selected)
    selected.value if selected
  else
    @value
  end
end
with_value(value) click to toggle source
# File lib/yaks/resource/form/field.rb, line 17
def with_value(value)
  if type.equal? :select
    with(options: select_options_for_value(value))
  else
    with(value: value)
  end
end

Private Instance Methods

select_options_for_value(value) click to toggle source
# File lib/yaks/resource/form/field.rb, line 27
def select_options_for_value(value)
  unset = ->(option) { option.selected && !value().eql?(value) }
  set   = ->(option) { !option.selected && option.value.eql?(value) }

  options.each_with_object([]) do |option, new_opts|
    new_opts << case option
                when unset
                  option.with selected: false
                when set
                  option.with selected: true
                else
                  option
                end
  end
end