Module: Graphiti::Resource::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Graphiti::Resource
Defined in:
lib/graphiti/resource/persistence.rb

Instance Method Summary collapse

Instance Method Details

#assign_attributes(model_instance, update_params, meta = nil) ⇒ Object



122
123
124
# File 'lib/graphiti/resource/persistence.rb', line 122

def assign_attributes(model_instance, update_params, meta = nil)
  adapter.assign_attributes(model_instance, update_params)
end

#build(model, meta = nil) ⇒ Object



118
119
120
# File 'lib/graphiti/resource/persistence.rb', line 118

def build(model, meta = nil)
  adapter.build(model)
end

#create(create_params, meta = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/graphiti/resource/persistence.rb', line 72

def create(create_params, meta = nil)
  model_instance = nil

  run_callbacks :persistence, :create, create_params, meta do
    run_callbacks :attributes, :create, create_params, meta do |params|
      model_instance = call_with_meta(:build, model, meta)
      call_with_meta(:assign_attributes, model_instance, params, meta)
      model_instance
    end

    run_callbacks :save, :create, model_instance, meta do
      model_instance = call_with_meta(:save, model_instance, meta)
    end

    model_instance
  end
end

#delete(model_instance, meta = nil) ⇒ Object



130
131
132
# File 'lib/graphiti/resource/persistence.rb', line 130

def delete(model_instance, meta = nil)
  adapter.destroy(model_instance)
end

#destroy(id, meta = nil) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/graphiti/resource/persistence.rb', line 110

def destroy(id, meta = nil)
  model_instance = self.class._find(id: id).data
  run_callbacks :destroy, :destroy, model_instance, meta do
    call_with_meta(:delete, model_instance, meta)
  end
  model_instance
end

#save(model_instance, meta = nil) ⇒ Object



126
127
128
# File 'lib/graphiti/resource/persistence.rb', line 126

def save(model_instance, meta = nil)
  adapter.save(model_instance)
end

#update(update_params, meta = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/graphiti/resource/persistence.rb', line 90

def update(update_params, meta = nil)
  model_instance = nil
  id = update_params[:id]
  update_params = update_params.except(:id)

  run_callbacks :persistence, :update, update_params, meta do
    run_callbacks :attributes, :update, update_params, meta do |params|
      model_instance = self.class._find(id: id).data
      call_with_meta(:assign_attributes, model_instance, params, meta)
      model_instance
    end

    run_callbacks :save, :update, model_instance, meta do
      model_instance = call_with_meta(:save, model_instance, meta)
    end
  end

  model_instance
end