commit
a152705ca3
|
@ -9,7 +9,7 @@ private
|
|||
if outcome.success?
|
||||
render options.merge(json: outcome)
|
||||
else
|
||||
render options.merge(json: outcome.errors.message_list, status: 422)
|
||||
render options.merge(json: outcome.errors.message, status: 422)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# Api::SequencesController performs CRUD on stored sequences
|
||||
module Api
|
||||
class SequencesController < Api::AbstractController
|
||||
def create
|
||||
|
|
|
@ -7,6 +7,8 @@ class Step
|
|||
embedded_in :sequence
|
||||
|
||||
field :message_type
|
||||
field :time_stamp, type: Time
|
||||
validates :message_type, presence: true
|
||||
|
||||
field :command, type: Hash
|
||||
validates :command, presence: true
|
||||
end
|
||||
|
|
|
@ -1,17 +1,21 @@
|
|||
module Sequences
|
||||
class Create < Mutations::Command
|
||||
using MongoidRefinements
|
||||
|
||||
required do
|
||||
model :user
|
||||
model :user, class: User
|
||||
string :name
|
||||
array(:steps) { model :step, builder: Steps::Create, class: Object }
|
||||
array :steps do
|
||||
model :step, builder: Steps::Initialize, new_records: true
|
||||
end
|
||||
end
|
||||
|
||||
def validate
|
||||
optional do
|
||||
string :color, in: Sequence::COLORS
|
||||
end
|
||||
|
||||
def execute
|
||||
{}
|
||||
create(Sequence, inputs)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,18 +1,15 @@
|
|||
module Steps
|
||||
class Create < Mutations::Command
|
||||
using MongoidRefinements
|
||||
|
||||
required do
|
||||
string :message_type, in: Step::MESSAGE_TYPES
|
||||
end
|
||||
|
||||
def validate
|
||||
hash(:command) { model :*, class: Object }
|
||||
model :sequence
|
||||
end
|
||||
|
||||
def execute
|
||||
# STILL NEED TO:
|
||||
# * Make the steps mutation create a step
|
||||
# * Make the sequence mutation create one, too.
|
||||
binding.pry
|
||||
create(Step, inputs)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
module Steps
|
||||
# Initializes a Step _BUT DOES NOT PERSIST TO THE DATABASE_. This is useful
|
||||
# When you are creating a yet-to-be-saved Sequence and need to embed Steps.
|
||||
class Initialize < Mutations::Command
|
||||
required do
|
||||
string :message_type, in: Step::MESSAGE_TYPES
|
||||
hash(:command) { model :*, class: Object }
|
||||
end
|
||||
|
||||
def execute
|
||||
Step.new inputs
|
||||
end
|
||||
end
|
||||
end
|
|
@ -11,6 +11,7 @@ require "sprockets/railtie"
|
|||
# you've limited to :test, :development, or :production.
|
||||
Bundler.require(:default, Rails.env)
|
||||
|
||||
# TODO: Rename this from DSS to Farmbot or something like that.
|
||||
module Dss
|
||||
class Application < Rails::Application
|
||||
# Settings in config/environments/* take precedence over those specified
|
||||
|
@ -33,6 +34,6 @@ module Dss
|
|||
g.helper_specs false
|
||||
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
|
||||
end
|
||||
|
||||
config.autoload_paths << Rails.root.join('lib')
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
# 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 unifromly.
|
||||
# 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
|
||||
model.errors.messages
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -10,11 +10,15 @@ describe Api::SequencesController do
|
|||
|
||||
it 'creates a new sequences for a user' do
|
||||
sign_in user
|
||||
inputs = {name: "Scare Birds",
|
||||
steps: [{
|
||||
message_type: 'move_rel'
|
||||
}]}
|
||||
post :create, inputs
|
||||
input = { name: "Scare Birds",
|
||||
steps: [{ message_type: 'move_rel',
|
||||
command: { action: 'MOVE RELATIVE',
|
||||
x: 1,
|
||||
y: 2,
|
||||
z: 3,
|
||||
speed: 100,
|
||||
delay: 0} }] }
|
||||
post :create, input
|
||||
expect(response.status).to eq(200)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
FactoryGirl.define do
|
||||
factory :sequence do
|
||||
name
|
||||
color
|
||||
name { Faker::Company.catch_phrase }
|
||||
color { Step::MESSAGE_TYPES.sample }
|
||||
user
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Steps::Create do
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
let(:mutation) { Sequences::Create }
|
||||
let(:step) do
|
||||
{ message_type: 'move_rel',
|
||||
command: { action: 'MOVE RELATIVE',
|
||||
x: 1,
|
||||
y: 2,
|
||||
z: 3,
|
||||
speed: 100,
|
||||
delay: 0 } }
|
||||
end
|
||||
|
||||
let(:valid_params) do
|
||||
{ user: user,
|
||||
name: 'Hi.',
|
||||
steps: [step] }
|
||||
end
|
||||
|
||||
it 'Builds a `sequence`' do
|
||||
outcome = mutation.run(valid_params)
|
||||
expect(outcome.success?).to be_truthy
|
||||
seq = outcome.result
|
||||
|
||||
expect(seq.steps.count).to eq(1)
|
||||
expect(seq.name).to eq('Hi.')
|
||||
expect(seq.user).to eq(user)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,23 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Steps::Create do
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
let(:mutation) { Steps::Create }
|
||||
let(:valid_params) { {message_type: 'move_rel',
|
||||
sequence: FactoryGirl.create(:sequence),
|
||||
command: {action: 'MOVE RELATIVE',
|
||||
x: 1,
|
||||
y: 2,
|
||||
z: 3,
|
||||
speed: 100,
|
||||
delay: 0}} }
|
||||
|
||||
it 'Builds an instance of `step`' do
|
||||
outcome = mutation.run(valid_params)
|
||||
expect(outcome.success?).to be_truthy
|
||||
expect(outcome.result.message_type).to eq(valid_params[:message_type])
|
||||
expect(outcome.result.sequence).to eq(valid_params[:sequence])
|
||||
cmd = outcome.result.command.deep_symbolize_keys
|
||||
expect(cmd).to eq(valid_params[:command])
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue