Mongoid eviction notice
parent
ad165198e4
commit
677c79039e
|
@ -1,6 +1,6 @@
|
|||
module Devices
|
||||
class Create < Mutations::Command
|
||||
using MongoidRefinements
|
||||
using LegacyRefinementsModule
|
||||
|
||||
required do
|
||||
model :user, class: User
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Regimens
|
||||
class Create < Mutations::Command
|
||||
using MongoidRefinements
|
||||
using LegacyRefinementsModule
|
||||
|
||||
required do
|
||||
model :device, class: Device
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Regimens
|
||||
class Update < Mutations::Command
|
||||
using MongoidRefinements
|
||||
using LegacyRefinementsModule
|
||||
|
||||
required do
|
||||
model :device, class: Device
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'mutations/time_filter'
|
|||
|
||||
module Schedules
|
||||
class Create < Mutations::Command
|
||||
using MongoidRefinements
|
||||
using LegacyRefinementsModule
|
||||
|
||||
required do
|
||||
model :sequence, class: Sequence
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'mutations/time_filter'
|
|||
|
||||
module Schedules
|
||||
class Update < Mutations::Command
|
||||
using MongoidRefinements
|
||||
using LegacyRefinementsModule
|
||||
|
||||
required do
|
||||
model :schedule, class: Schedule
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
# This is a REALLY OLD LEGACY MODULE.
|
||||
# DONT USE IT FOR NEW CODE.
|
||||
module LegacyRefinementsModule
|
||||
refine Mutations::Command do
|
||||
|
||||
# IF YOU ARE TRYING TO UNDERSTAND CODE THAT USES THIS MODULE- DONT!!!
|
||||
# REWRITE IT INSTEAD. THIS IS A LEGACY MODULE THAT NEEDS TO GO AWAY!!
|
||||
def create(klass, inputs = {})
|
||||
model = klass.new(inputs)
|
||||
yield(model, inputs) if block_given?
|
||||
if model.valid? && model.save
|
||||
model
|
||||
else
|
||||
add_error klass.to_s.downcase.to_sym, :invalid, model.errors.messages
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
# IF YOU ARE TRYING TO UNDERSTAND CODE THAT USES THIS MODULE- DONT!!!
|
||||
# REWRITE IT INSTEAD. THIS IS A LEGACY MODULE THAT NEEDS TO GO AWAY!!
|
||||
def update_attributes(model, inputs = {})
|
||||
if model.update_attributes(inputs)
|
||||
model
|
||||
else
|
||||
add_error model.class.to_s.downcase.to_sym,
|
||||
:invalid,
|
||||
model.errors.messages
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,38 +0,0 @@
|
|||
# A set of refinements that help the mutations gem deal with Mongoid
|
||||
module MongoidRefinements
|
||||
refine Mutations::Command do
|
||||
# This method provides a uniform way of ==================================
|
||||
# 1. Creating models inside of Mutations
|
||||
# 2. presenting API model validation errors uniformly.
|
||||
# This is important for keeping API msgs sane.
|
||||
# Returns an instance of Klass -OR- a hash of model validation err.
|
||||
# messages You may optionally pass in a block to transform the model/input
|
||||
# before it gets saved.
|
||||
# Ex: create(User, name: 'Rick', email: "r@m.com") do |user, inputs|
|
||||
# inputs.email.downcase!
|
||||
# user.password = "SomethingThatYouPassIn"
|
||||
# end # ==============================================================
|
||||
def create(klass, inputs = {})
|
||||
model = klass.new(inputs)
|
||||
yield(model, inputs) if block_given?
|
||||
if model.valid? && model.save
|
||||
model
|
||||
else
|
||||
add_error klass.to_s.downcase.to_sym, :invalid, model.errors.messages
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
# See documentation for `create()`. Does the same thing, except for updates.
|
||||
def update_attributes(model, inputs = {})
|
||||
if model.update_attributes(inputs)
|
||||
model
|
||||
else
|
||||
add_error model.class.to_s.downcase.to_sym,
|
||||
:invalid,
|
||||
model.errors.messages
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,12 +1,12 @@
|
|||
class TestCreateMutation < Mutations::Command
|
||||
using MongoidRefinements
|
||||
using LegacyRefinementsModule
|
||||
def execute
|
||||
create(User, {})
|
||||
end
|
||||
end
|
||||
|
||||
class TestUpdateMutation < Mutations::Command
|
||||
using MongoidRefinements
|
||||
using LegacyRefinementsModule
|
||||
required do
|
||||
model :user, class: User
|
||||
end
|
||||
|
@ -16,7 +16,7 @@ class TestUpdateMutation < Mutations::Command
|
|||
end
|
||||
end
|
||||
|
||||
RSpec.describe MongoidRefinements do
|
||||
RSpec.describe LegacyRefinementsModule do
|
||||
describe '#create' do
|
||||
it 'catches model errors' do
|
||||
mutation = TestCreateMutation.run({})
|
||||
|
|
Loading…
Reference in New Issue