Module: FlatMap::OpenMapper::Persistence
- Extended by:
- ActiveSupport::Concern
- Included in:
- FlatMap::OpenMapper
- Defined in:
- lib/flat_map/open_mapper/persistence.rb
Overview
This module provides some integration between mapper and its target, which is usually an ActiveRecord model, as well as some integration between mapper and Rails forms.
In particular, validation and save methods are defined here. And the save
method itself is defined as a callback. Also, Rails multiparam attributes extraction is defined within this module.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#apply(params) ⇒ Boolean
Write a passed set of
params
. -
#errors ⇒ FlatMap::Errors
Overridden to use Errors instead of ActiveModel ones.
-
#persisted? ⇒ Boolean
Return
true
if target was updated. -
#save ⇒ Boolean
Try to save the target and send a
save
method to all mounted mappers. -
#save_target ⇒ true
Return
true
since OpenStruct is always ‘saved’. -
#shallow_save ⇒ Boolean
Perform target save with callbacks call.
-
#valid? ⇒ Boolean
Return
true
if the mapper is valid, i.e. -
#write(params) ⇒ Hash
Extract the multiparam values from the passed
params
.
Instance Method Details
#apply(params) ⇒ Boolean
Write a passed set of params
. Then try to save the model if self
passes validation. Saving is performed in a transaction.
40 41 42 43 |
# File 'lib/flat_map/open_mapper/persistence.rb', line 40 def apply(params) write(params) valid? && save end |
#errors ⇒ FlatMap::Errors
Overridden to use Errors instead of ActiveModel ones.
144 145 146 |
# File 'lib/flat_map/open_mapper/persistence.rb', line 144 def errors @errors ||= FlatMap::Errors.new(self) end |
#persisted? ⇒ Boolean
Return true
if target was updated.
102 103 104 |
# File 'lib/flat_map/open_mapper/persistence.rb', line 102 def persisted? target != OpenStruct.new end |
#save ⇒ Boolean
Try to save the target and send a save
method to all mounted mappers.
The order in which mappings are saved is important, since we save records with :validate => false option. Since Rails will perform auto-saving on associations (and it in its turn will try to save associated record with :validate => true option. To be more precise, with :validate => !autosave option, where autosave corresponds to that option of reflection, which is usually not specified, i.e. nil), we might come to a situation of saving a record with nil foreign key for belongs_to association, which will raise exception. Thus, we want to explicitly save records in order which will allow them to be saved. Return false
if that chain of save
calls returns true
on any of its elements. Return true
otherwise.
Saving is performed as a callback.
77 78 79 80 81 82 83 |
# File 'lib/flat_map/open_mapper/persistence.rb', line 77 def save before_res = save_mountings(before_save_mountings) target_res = self_mountings.map{ |mount| mount.shallow_save }.all? after_res = save_mountings(after_save_mountings) before_res && target_res && after_res end |
#save_target ⇒ true
Return true
since OpenStruct is always ‘saved’.
88 89 90 |
# File 'lib/flat_map/open_mapper/persistence.rb', line 88 def save_target true end |
#shallow_save ⇒ Boolean
Perform target save with callbacks call
95 96 97 |
# File 'lib/flat_map/open_mapper/persistence.rb', line 95 def shallow_save run_callbacks(:save){ save_target } end |
#valid? ⇒ Boolean
Return true
if the mapper is valid, i.e. if it is valid itself, and if all mounted mappers (traits and other mappers) are also valid.
Accepts any parameters, but doesn’t use them to be compatible with ActiveRecord calls.
123 124 125 126 127 128 129 |
# File 'lib/flat_map/open_mapper/persistence.rb', line 123 def valid?(*) res = trait_mountings.map(&:valid?).all? res = super && res # we do want to call super mounting_res = mapper_mountings.map(&:valid?).all? consolidate_errors! res && mounting_res end |
#write(params) ⇒ Hash
Extract the multiparam values from the passed params
. Then use the resulting hash to assign values to the target. Assignment is performed by sending writer methods to self
that correspond to keys in the resulting params
hash.
52 53 54 55 56 57 58 |
# File 'lib/flat_map/open_mapper/persistence.rb', line 52 def write(params) extract_multiparams!(params) params.each do |name, value| self.send("#{name}=", value) end end |