Class: LunaPark::Forms::Simple

Inherits:
Object
  • Object
show all
Includes:
Extensions::Validatable
Defined in:
lib/luna_park/forms/simple.rb

Overview

Form object represents blank document, required to filled right, and can be performed

Examples:

with default behavior

class MyForm < LunaPark::Forms::SingleItem
  validation MyValidator # respond to .validate, #valid?, #errors, #valid_params

  def perform(valid_params)
    "Performed #{valid_params[:foo]}"
  end
end

form = MyForm.new({ foo: 'Foo', excess: 'Excess' })

if form.submit
  form.result # => 'Performed Foo'
else
  form.errors # => { foo: ['is wrong'] }
end

without default behavior

class MyForm < LunaPark::Forms::SingleItem
  validation MyValidator # respond to .validate, #valid?, #errors, #valid_params
end

form = MyForm.new({ foo: 'Foo', excess: 'Excess' })

if form.submit
  form.result # => { foo: 'Foo' }
else
  form.errors # => { foo: ['is wrong'] }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Validatable

included

Constructor Details

#initialize(params = {}) ⇒ Simple

Returns a new instance of Simple.



46
47
48
# File 'lib/luna_park/forms/simple.rb', line 46

def initialize(params = {})
  @params = params
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



44
45
46
# File 'lib/luna_park/forms/simple.rb', line 44

def result
  @result
end

Instance Method Details

#submitObject



50
51
52
53
54
55
56
# File 'lib/luna_park/forms/simple.rb', line 50

def submit
  if valid?
    perform!
    true
  else false
  end
end