Done with controller for sequence creation - now to make fixes for hound
parent
43ee0daeda
commit
58bbb3e21e
|
@ -1,4 +1,3 @@
|
|||
# Api::SequencesController performs CRUD on stored sequences
|
||||
module Api
|
||||
class SequencesController < Api::AbstractController
|
||||
def create
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
module Sequences
|
||||
class Create < Mutations::Command
|
||||
using MongoidRefinements
|
||||
|
||||
required do
|
||||
model :user
|
||||
string :time_stamp
|
||||
array(:steps) do
|
||||
model :step, builder: Steps::Create
|
||||
model :user, class: User
|
||||
string :name
|
||||
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,5 +1,6 @@
|
|||
module Steps
|
||||
class Create < Mutations::Command
|
||||
using MongoidRefinements
|
||||
|
||||
required do
|
||||
string :message_type, in: Step::MESSAGE_TYPES
|
||||
|
@ -10,28 +11,5 @@ module Steps
|
|||
def execute
|
||||
create(Step, inputs)
|
||||
end
|
||||
|
||||
# TODO: Move this into a refinement
|
||||
# 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@mailinator.com") do |user, inputs|
|
||||
# inputs.email.downcase!
|
||||
# user.password = "SomethingThatYouPassIn"
|
||||
# end
|
||||
def create(klass, inputs = {})
|
||||
binding.pry
|
||||
model = klass.new(inputs)
|
||||
yield(model, inputs) if block_given?
|
||||
if (model.valid? && model.save)
|
||||
model
|
||||
else
|
||||
model.errors.messages
|
||||
end
|
||||
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
|
|
@ -9,12 +9,10 @@ describe Api::SequencesController do
|
|||
let(:user) { FactoryGirl.create(:user) }
|
||||
|
||||
it 'creates a new sequences for a user' do
|
||||
pending "Going to create mutation level tests first."
|
||||
sign_in user
|
||||
input = {name: "Scare Birds",
|
||||
steps: [{
|
||||
message_type: 'move_rel',
|
||||
time_stamp: Time.now,
|
||||
command: {action: 'MOVE RELATIVE',
|
||||
x: 1,
|
||||
y: 2,
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Steps::Create do
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
let(:mutation) { Sequences::Create }
|
||||
let(:step) { {message_type: 'move_rel',
|
||||
command: {action: 'MOVE RELATIVE',
|
||||
x: 1,
|
||||
y: 2,
|
||||
z: 3,
|
||||
speed: 100,
|
||||
delay: 0}} }
|
||||
let(:valid_params) { {user: user,
|
||||
name: 'Hi.',
|
||||
steps: [step]} }
|
||||
|
||||
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
|
Loading…
Reference in New Issue