Fix AMS nesting issue that caused runtime error on frontend

pull/261/head
Rick Carlino 2016-09-05 11:46:50 -05:00
parent 9ef0dc5715
commit 23da9801e3
7 changed files with 70 additions and 5 deletions

View File

@ -6,10 +6,13 @@ module Api
end
def create
ship_it = JSON.parse(params.to_json).merge(regimen_params)
mutate Regimens::Create.run(ship_it)
end
def update
mutate Regimens::Create.run(ship_it)
end
def destroy
the_regimen.destroy!
render nothing: true
@ -17,6 +20,12 @@ module Api
private
# PROBLEM: ActiveRecord is trying to prevent us from directly handling arrays.
# SOLUTION: Duplicate the item for now.
def ship_it
JSON.parse(params.to_json).merge(regimen_params)
end
def the_regimen
your_regimens.find(params[:id])
end

View File

@ -0,0 +1,24 @@
module Regimens
class Create < Mutations::Command
using MongoidRefinements
required do
model :device, class: Device
string :name
string :color, in: Sequence::COLORS
array :regimen_items do
hash do
integer :time_offset
integer :sequence_id
end
end
end
def execute
inputs[:regimen_items].map! do |i|
RegimenItem.new(i)
end
Regimen.create(inputs)
end
end
end

View File

@ -1,4 +1,5 @@
class RegimenItemSerializer < ActiveModel::Serializer
# TODO
attributes :id, :regimen_id, :sequence, :time_offset
attributes :id, :regimen_id, :time_offset
has_one :sequence
end

View File

@ -1,3 +1,4 @@
class RegimenSerializer < ActiveModel::Serializer
attributes :id, :name, :color, :device_id, :regimen_items
attributes :id, :name, :color, :device_id
has_many :regimen_items
end

View File

@ -1,3 +1,4 @@
class SequenceSerializer < ActiveModel::Serializer
attributes :id, :name, :color, :steps
attributes :id, :name, :color
has_many :steps
end

View File

@ -0,0 +1,29 @@
require 'spec_helper'
describe Regimens::Create do
let(:sequence) { FactoryGirl.create(:sequence) }
let(:device) { sequence.device }
let(:sequence2) { FactoryGirl.create(:sequence, device: sequence.device) }
it 'updates a regimen' do
# input = {
# device: device,
# name: "My Second Regimen",
# color: "red",
# regimen_items: => [
# {
# time_offset: 518700000,
# sequence_id: 1
# },
# {
# time_offset: 864300000,
# sequence_id: 1
# },
# {
# time_offset: 86700000,
# sequence_id: 1
# }
# ]}
# result = Regimens::Update.run!(input).reload
end
end

View File

@ -48,7 +48,7 @@ RSpec.configure do |config|
SmarfDoc.run!(request, response)
end
end
config.after(:suite) do
SmarfDoc.finish!
end