Class: Syrup::FormObject
- Inherits:
-
Object
- Object
- Syrup::FormObject
- Extended by:
- ActiveModel::Callbacks
- Includes:
- Callbacks, FormMethods, Persistence, Relations
- Defined in:
- lib/syrup/form_object.rb
Class Method Summary collapse
- .attributes_protected_by_default ⇒ Object
- .find(params) ⇒ Object
- .form_name(form_name) ⇒ Object
- .reflect_on_association ⇒ Object
- .standalone ⇒ Object
- .wraps(klass) ⇒ Object
Instance Method Summary collapse
- #add_to_transaction ⇒ Object
- #after_find(params) ⇒ Object
- #build(params) ⇒ Object
- #clear_transaction_record_state ⇒ Object
- #create ⇒ Object
- #create_record ⇒ Object
-
#initialize(params = {}) ⇒ FormObject
constructor
A new instance of FormObject.
- #remember_transaction_record_state ⇒ Object
- #restore_transaction_record_state ⇒ Object
- #run_validations! ⇒ Object
- #update(attributes = []) ⇒ Object
- #update_attributes(params) ⇒ Object
- #update_record(attributes = []) ⇒ Object
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_default ⇒ Object
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_association ⇒ Object
39 |
# File 'lib/syrup/form_object.rb', line 39 def self.reflect_on_association(*); end |
.standalone ⇒ Object
16 17 18 |
# File 'lib/syrup/form_object.rb', line 16 def standalone include Syrup::Form::Standalone end |
Instance Method Details
#add_to_transaction ⇒ Object
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_state ⇒ Object
38 |
# File 'lib/syrup/form_object.rb', line 38 def clear_transaction_record_state(*); end |
#create ⇒ Object
78 79 80 |
# File 'lib/syrup/form_object.rb', line 78 def create run_callbacks(:create) { save_form } end |
#create_record ⇒ Object
70 71 72 |
# File 'lib/syrup/form_object.rb', line 70 def create_record run_callbacks(:create) {save_form} end |
#remember_transaction_record_state ⇒ Object
36 |
# File 'lib/syrup/form_object.rb', line 36 def remember_transaction_record_state(*); end |
#restore_transaction_record_state ⇒ Object
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..each do |klass, | unless .valid? self.errors.add(klass, .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 |