Class: Syrup::FormObject

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
Callbacks, FormMethods, Persistence, Relations
Defined in:
lib/syrup/form_object.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ FormObject

Returns a new instance of FormObject.



45
46
47
48
49
# File 'lib/syrup/form_object.rb', line 45

def initialize(params={})
  build_relations
  build(params)
  self.assign_attributes(params)
end

Class Method Details

.attributes_protected_by_defaultObject



41
42
43
# File 'lib/syrup/form_object.rb', line 41

def self.attributes_protected_by_default
  []
end

.find(params) ⇒ Object



20
21
22
23
24
25
# File 'lib/syrup/form_object.rb', line 20

def find(params)
  form = self.new
  form.find_relations(params)
  form.after_find(params)
  form
end

.form_name(form_name) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/syrup/form_object.rb', line 27

def form_name(form_name)
  instance_eval <<-EOH
    def self.model_name
      ActiveModel::Name.new(self, nil, '#{form_name.to_s}')
    end
  EOH
end

.reflect_on_associationObject



39
# File 'lib/syrup/form_object.rb', line 39

def self.reflect_on_association(*); end

.standaloneObject



16
17
18
# File 'lib/syrup/form_object.rb', line 16

def standalone
  include Syrup::Form::Standalone
end

.wraps(klass) ⇒ Object



10
11
12
13
14
# File 'lib/syrup/form_object.rb', line 10

def wraps(klass)
  has_one(klass)
  @wrapped_class_name = klass
  include Syrup::Form::Wrapped
end

Instance Method Details

#add_to_transactionObject



40
# File 'lib/syrup/form_object.rb', line 40

def add_to_transaction; end

#after_find(params) ⇒ Object



57
# File 'lib/syrup/form_object.rb', line 57

def after_find(params); end

#build(params) ⇒ Object



56
# File 'lib/syrup/form_object.rb', line 56

def build(params); end

#clear_transaction_record_stateObject



38
# File 'lib/syrup/form_object.rb', line 38

def clear_transaction_record_state(*); end

#createObject



78
79
80
# File 'lib/syrup/form_object.rb', line 78

def create
  run_callbacks(:create) { save_form }
end

#create_recordObject



70
71
72
# File 'lib/syrup/form_object.rb', line 70

def create_record
  run_callbacks(:create) {save_form}
end

#remember_transaction_record_stateObject



36
# File 'lib/syrup/form_object.rb', line 36

def remember_transaction_record_state(*); end

#restore_transaction_record_stateObject



37
# File 'lib/syrup/form_object.rb', line 37

def restore_transaction_record_state(*); end

#run_validations!Object



59
60
61
62
63
64
65
66
# File 'lib/syrup/form_object.rb', line 59

def run_validations!(*)
  super
  self.related_objects.each do |klass, related|
    unless related.valid?
      self.errors.add(klass, related.errors)
    end
  end
end

#update(attributes = []) ⇒ Object



82
83
84
# File 'lib/syrup/form_object.rb', line 82

def update(attributes=[])
  run_callbacks(:update) { save_form }
end

#update_attributes(params) ⇒ Object



51
52
53
54
# File 'lib/syrup/form_object.rb', line 51

def update_attributes(params)
  self.assign_attributes(params)
  self.save
end

#update_record(attributes = []) ⇒ Object



74
75
76
# File 'lib/syrup/form_object.rb', line 74

def update_record(attributes=[])
  run_callbacks(:update) {save_form}
end