Mongoid eviction notice

pull/290/head
Rick Carlino 2016-11-09 17:00:22 -06:00
parent ad165198e4
commit 677c79039e
8 changed files with 40 additions and 46 deletions

View File

@ -1,6 +1,6 @@
module Devices
class Create < Mutations::Command
using MongoidRefinements
using LegacyRefinementsModule
required do
model :user, class: User

View File

@ -1,6 +1,6 @@
module Regimens
class Create < Mutations::Command
using MongoidRefinements
using LegacyRefinementsModule
required do
model :device, class: Device

View File

@ -1,6 +1,6 @@
module Regimens
class Update < Mutations::Command
using MongoidRefinements
using LegacyRefinementsModule
required do
model :device, class: Device

View File

@ -2,7 +2,7 @@ require 'mutations/time_filter'
module Schedules
class Create < Mutations::Command
using MongoidRefinements
using LegacyRefinementsModule
required do
model :sequence, class: Sequence

View File

@ -2,7 +2,7 @@ require 'mutations/time_filter'
module Schedules
class Update < Mutations::Command
using MongoidRefinements
using LegacyRefinementsModule
required do
model :schedule, class: Schedule

View File

@ -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

View File

@ -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

View File

@ -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({})