Module: Graphiti::Resource::Persistence
- Extended by:
- ActiveSupport::Concern
- Included in:
- Graphiti::Resource
- Defined in:
- lib/graphiti/resource/persistence.rb
Instance Attribute Summary collapse
-
#assigned_model ⇒ Object
readonly
The model built by a prior ResourceProxy#assign_attributes, present for the duration of the save that persists it.
Instance Method Summary collapse
-
#assign(assign_params, meta = nil, action_name = nil, model_instance: nil) ⇒ Object
model_instanceis the already-built model from a previous #assign (see ResourceProxy#assign_attributes). - #assign_attributes(model_instance, update_params, meta = nil) ⇒ Object
- #build(model, meta = nil) ⇒ Object
-
#create(create_params, meta = nil) ⇒ Object
Attributes are assigned before the persistence callbacks fire, so around_persistence receives the assigned model - its pre-yield position is the last chance to touch the model before save, inside the transaction.
- #delete(model_instance, meta = nil) ⇒ Object
- #destroy(id, meta = nil) ⇒ Object
- #save(model_instance, meta = nil) ⇒ Object
- #update(update_params, meta = nil) ⇒ Object
- #with_assigned_model(model) ⇒ Object private
Instance Attribute Details
#assigned_model ⇒ Object (readonly)
The model built by a prior ResourceProxy#assign_attributes, present for the duration of the save that persists it
98 99 100 |
# File 'lib/graphiti/resource/persistence.rb', line 98 def assigned_model @assigned_model end |
Instance Method Details
#assign(assign_params, meta = nil, action_name = nil, model_instance: nil) ⇒ Object
model_instance is the already-built model from a previous #assign
(see ResourceProxy#assign_attributes). When given, attributes are
applied to it rather than to a freshly built/found model.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/graphiti/resource/persistence.rb', line 75 def assign(assign_params, = nil, action_name = nil, model_instance: nil) # Only update strips :id (it identifies the record to find) - a create # payload may legitimately carry a client-supplied id to assign. if action_name == :update id = assign_params[:id] assign_params = assign_params.except(:id) end run_callbacks :attributes, action_name, assign_params, do |params| model_instance ||= if action_name == :update self.class._find(id: id).data else (:build, model, ) end (:assign_attributes, model_instance, params, ) model_instance end model_instance end |
#assign_attributes(model_instance, update_params, meta = nil) ⇒ Object
152 153 154 |
# File 'lib/graphiti/resource/persistence.rb', line 152 def assign_attributes(model_instance, update_params, = nil) adapter.assign_attributes(model_instance, update_params) end |
#build(model, meta = nil) ⇒ Object
148 149 150 |
# File 'lib/graphiti/resource/persistence.rb', line 148 def build(model, = nil) adapter.build(model) end |
#create(create_params, meta = nil) ⇒ Object
Attributes are assigned before the persistence callbacks fire, so around_persistence receives the assigned model - its pre-yield position is the last chance to touch the model before save, inside the transaction. Modify attributes in before_attributes instead.
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/graphiti/resource/persistence.rb', line 112 def create(create_params, = nil) model_instance = assigned_model || assign(create_params, , :create) run_callbacks :persistence, :create, model_instance, do run_callbacks :save, :create, model_instance, do model_instance = (:save, model_instance, ) end model_instance end model_instance end |
#delete(model_instance, meta = nil) ⇒ Object
160 161 162 |
# File 'lib/graphiti/resource/persistence.rb', line 160 def delete(model_instance, = nil) adapter.destroy(model_instance) end |
#destroy(id, meta = nil) ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/graphiti/resource/persistence.rb', line 140 def destroy(id, = nil) model_instance = self.class._find(id: id).data run_callbacks :destroy, :destroy, model_instance, do (:delete, model_instance, ) end model_instance end |
#save(model_instance, meta = nil) ⇒ Object
156 157 158 |
# File 'lib/graphiti/resource/persistence.rb', line 156 def save(model_instance, = nil) adapter.save(model_instance) end |
#update(update_params, meta = nil) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/graphiti/resource/persistence.rb', line 126 def update(update_params, = nil) model_instance = assigned_model || assign(update_params, , :update) run_callbacks :persistence, :update, model_instance, do run_callbacks :save, :update, model_instance, do model_instance = (:save, model_instance, ) end model_instance end model_instance end |
#with_assigned_model(model) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
101 102 103 104 105 106 |
# File 'lib/graphiti/resource/persistence.rb', line 101 def with_assigned_model(model) @assigned_model = model yield ensure @assigned_model = nil end |