class Shaf::Formable::Form

Constants

DEFAULT_SUBMIT
DEFAULT_TYPE

Attributes

resource[RW]

Public Class Methods

new(params = {}) click to toggle source
# File lib/shaf/formable/form.rb, line 18
def initialize(params = {})
  @title = params[:title]
  @action = params[:action]
  @name = params[:name]&.to_sym || name_from(@action)
  @method = params[:method] ||= http_method_from(@action)
  @type = params[:type] || DEFAULT_TYPE
  @submit = params[:submit] || DEFAULT_SUBMIT
  @fields = (params[:fields] || {}).map do |name, args|
    Field.new(name, args)
  end
end

Public Instance Methods

[](name) click to toggle source
# File lib/shaf/formable/form.rb, line 76
def [](name)
  fields.find { |field| field.name == name }
end
action=(action) click to toggle source
# File lib/shaf/formable/form.rb, line 43
def action=(action)
  @action = action
  @name ||= name_from action
  @method ||= http_method_from action
end
add_field(name, opts) click to toggle source
# File lib/shaf/formable/form.rb, line 49
def add_field(name, opts)
  @fields << Field.new(name, opts)
end
clone() click to toggle source
# File lib/shaf/formable/form.rb, line 59
def clone
  dup.tap { |obj| obj.freeze if frozen? }
end
dup() click to toggle source
Calls superclass method
# File lib/shaf/formable/form.rb, line 53
def dup
  super.tap do |obj|
    obj.instance_variable_set(:@fields, @fields.map(&:dup))
  end
end
fields=(fields) click to toggle source
# File lib/shaf/formable/form.rb, line 39
def fields=(fields)
  @fields = fields.map { |name, args| Field.new(name, args) }
end
fill!(from: nil) click to toggle source
# File lib/shaf/formable/form.rb, line 63
      def fill!(from: nil)
        resrc = from || resource
        raise FormHasNoResourceError, <<~MSG unless resrc
          Trying to fill form with values from resource, but form '#{name}' has no resource!
        MSG

        fields.each do |field|
          accessor_name = field.accessor_name
          next unless resrc.respond_to? accessor_name
          field.value = resrc.send(accessor_name)
        end
      end
method() click to toggle source
# File lib/shaf/formable/form.rb, line 34
def method
  return unless @method
  @method.to_s.upcase
end
method=(http_method) click to toggle source
# File lib/shaf/formable/form.rb, line 30
def method=(http_method)
  @method = http_method.to_s.upcase
end
to_html() click to toggle source
# File lib/shaf/formable/form.rb, line 80
def to_html
  form_element do
    [
      hidden_method_element,
      fields.map(&:to_html).join("\n"),
      submit_element
    ].compact.join("\n")
  end
end

Private Instance Methods

form_element() { |: nil, '</form>'| ... } click to toggle source
# File lib/shaf/formable/form.rb, line 101
def form_element
  [
    %Q(<form class="form" method=#{method == 'GET' ? 'GET' : 'POST'}#{href ? %Q( action="#{href.to_s}") : ''}>),
    block_given? ? yield : nil,
    '</form>'
  ].compact.join("\n")
end
hidden_method_element() click to toggle source
# File lib/shaf/formable/form.rb, line 109
def hidden_method_element
  return if %w[GET POST].include? method
  %Q(<input type="hidden" name="_method" value="#{method}">)
end
http_method_from(action) click to toggle source
# File lib/shaf/formable/form.rb, line 96
def http_method_from(action)
  return unless action
  action&.to_sym == :edit ? 'PUT' : 'POST'
end
name_from(action) click to toggle source
# File lib/shaf/formable/form.rb, line 92
def name_from(action)
  :"#{action.to_s.tr('_', '-')}-form" if action
end
submit_element() click to toggle source
# File lib/shaf/formable/form.rb, line 114
def submit_element
  %Q(<div class="form--input-group"><input type="submit" class="button" value="#{submit}"</div>)
end