Class: Tramway::BaseForm

Inherits:
Object
  • Object
show all
Includes:
DuckTyping::ActiveRecordCompatibility, Forms::Fields, Forms::Normalizations, Forms::Properties, Forms::Validations
Defined in:
lib/tramway/base_form.rb

Overview

Provides form object for Tramway

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DuckTyping::ActiveRecordCompatibility

included

Methods included from Forms::Validations

#__apply_validations, included

Methods included from Forms::Normalizations

#__apply_normalizations, included

Methods included from Forms::Fields

included

Methods included from Forms::Properties

#__apply_properties, included

Constructor Details

#initialize(object) ⇒ BaseForm

Returns a new instance of BaseForm.



21
22
23
24
25
26
# File 'lib/tramway/base_form.rb', line 21

def initialize(object)
  @object = object
  @initial_object = object.dup

  self.class.delegate object.class.primary_key, to: :object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/tramway/base_form.rb', line 63

def method_missing(method_name, *args)
  if method_name.to_s.end_with?('=') && args.one?
    object.public_send(method_name, args.first)
  else
    super
  end
end

Instance Attribute Details

#initial_objectObject (readonly)

Returns the value of attribute initial_object.



19
20
21
# File 'lib/tramway/base_form.rb', line 19

def initial_object
  @initial_object
end

#objectObject (readonly)

Returns the value of attribute object.



19
20
21
# File 'lib/tramway/base_form.rb', line 19

def object
  @object
end

Class Method Details

.inherited(subclass) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/tramway/base_form.rb', line 29

def inherited(subclass)
  __initialize_properties subclass
  __initialize_normalizations subclass
  __initialize_validations subclass
  __initialize_fields subclass

  super
end

Instance Method Details

#assign(params) ⇒ Object



59
60
61
# File 'lib/tramway/base_form.rb', line 59

def assign(params)
  __submit params
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/tramway/base_form.rb', line 71

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('=') || super
end

#submit(params) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/tramway/base_form.rb', line 39

def submit(params)
  __submit params

  return false if object.errors.any?

  object.save.tap do
    __object
  end
end

#submit!(params) ⇒ Object

Raises:

  • (ActiveRecord::RecordInvalid)


49
50
51
52
53
54
55
56
57
# File 'lib/tramway/base_form.rb', line 49

def submit!(params)
  __submit params

  raise ActiveRecord::RecordInvalid, object if object.errors.any?

  object.save!.tap do
    __object
  end
end