Class: Tramway::BaseForm

Inherits:
Object
  • Object
show all
Includes:
DuckTyping::ActiveRecordCompatibility, Forms::Fields, Forms::Normalizations, Forms::Properties
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::Normalizations

#__apply_normalizations, included

Methods included from Forms::Fields

included

Methods included from Forms::Properties

#__apply_properties, included

Constructor Details

#initialize(object) ⇒ BaseForm



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

def initialize(object)
  @object = object

  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



55
56
57
58
59
60
61
# File 'lib/tramway/base_form.rb', line 55

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

#objectObject (readonly)

Returns the value of attribute object.



17
18
19
# File 'lib/tramway/base_form.rb', line 17

def object
  @object
end

Class Method Details

.inherited(subclass) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/tramway/base_form.rb', line 26

def inherited(subclass)
  __initialize_properties subclass
  __initialize_normalizations subclass
  __initialize_fields subclass

  super
end

Instance Method Details

#assign(params) ⇒ Object



51
52
53
# File 'lib/tramway/base_form.rb', line 51

def assign(params)
  __submit params
end

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



63
64
65
# File 'lib/tramway/base_form.rb', line 63

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

#submit(params) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/tramway/base_form.rb', line 35

def submit(params)
  __submit params

  object.save.tap do
    __object
  end
end

#submit!(params) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/tramway/base_form.rb', line 43

def submit!(params)
  __submit params

  object.save!.tap do
    __object
  end
end