Module: FlatMap::ModelMapper::Persistence
- Extended by:
- ActiveSupport::Concern
- Included in:
- FlatMap::ModelMapper
- Defined in:
- lib/flat_map/model_mapper/persistence.rb
Overview
This module enhances and modifies original FlatMap::OpenMapper::Persistance functionality for ActiveRecord models as targets.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#apply(params) ⇒ Boolean
Write a passed set of
params
. -
#id ⇒ Fixnum?
Delegate #id to target, if possible.
-
#model_name ⇒ String
Return a ‘mapper’ string as a model_name.
-
#persisted? ⇒ Boolean
Delegate persistence to target.
-
#save_target ⇒ Boolean
Save
target
. -
#to_key ⇒ String
Delegate to the target’s #to_key method.
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.
76 77 78 79 80 81 82 83 84 |
# File 'lib/flat_map/model_mapper/persistence.rb', line 76 def apply(params) write(params) res = if valid? ActiveRecord::Base.transaction do save end end !!res end |
#id ⇒ Fixnum?
Delegate #id to target, if possible.
104 105 106 |
# File 'lib/flat_map/model_mapper/persistence.rb', line 104 def id target.id if target.respond_to?(:id) end |
#model_name ⇒ String
Return a ‘mapper’ string as a model_name. Used by Rails FormBuilder.
61 62 63 |
# File 'lib/flat_map/model_mapper/persistence.rb', line 61 def model_name 'mapper' end |
#persisted? ⇒ Boolean
Delegate persistence to target.
97 98 99 |
# File 'lib/flat_map/model_mapper/persistence.rb', line 97 def persisted? target.respond_to?(:persisted?) ? target.persisted? : false end |
#save_target ⇒ Boolean
Save target
89 90 91 92 |
# File 'lib/flat_map/model_mapper/persistence.rb', line 89 def save_target return true if owned? target.respond_to?(:save) ? target.save(:validate => false) : true end |
#to_key ⇒ String
Delegate to the target’s #to_key method.
67 68 69 |
# File 'lib/flat_map/model_mapper/persistence.rb', line 67 def to_key target.to_key end |