Module: Savings
- Extended by:
- ActiveSupport::Concern
- Included in:
- ActiveFormObjects::Base
- Defined in:
- lib/concerns/savings.rb
Constant Summary collapse
- @@associated_forms =
{}
Instance Method Summary collapse
- #save ⇒ Object
- #save! ⇒ Object
- #save_associated_forms! ⇒ Object
- #set_associated_forms ⇒ Object
- #with_rescue ⇒ Object
Instance Method Details
#save ⇒ Object
42 43 44 45 46 |
# File 'lib/concerns/savings.rb', line 42 def save save! rescue false end |
#save! ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/concerns/savings.rb', line 30 def save! with_rescue do if @@associated_forms[self.class.name].nil? validate! @resource.update!(@params) @resource else save_associated_forms! end end end |
#save_associated_forms! ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/concerns/savings.rb', line 48 def save_associated_forms! (@@associated_forms[self.class.name] || []).each do |associated| params = associated[:params].nil? ? @params[associated[:name]] : send(associated[:params]) resource_to_save = send("#{associated[:name]}_to_update") rescue nil @TheForm = associated[:form] saved_form = @TheForm.new(params, resource_to_save).save! instance_variable_set("@#{associated[:name]}", saved_form) end end |
#set_associated_forms ⇒ Object
59 60 61 62 63 |
# File 'lib/concerns/savings.rb', line 59 def set_associated_forms (@@associated_forms[self.class.name] || []).each do |form| instance_variable_set("@#{form[:name]}_form", form[:form]) end end |
#with_rescue ⇒ Object
65 66 67 68 69 70 |
# File 'lib/concerns/savings.rb', line 65 def with_rescue yield rescue ActiveRecord::RecordInvalid => e e.record.errors.add(e.record.class.name, '') raise ActiveRecord::RecordInvalid.new(e.record) end |