diff --git a/.DS_Store b/.DS_Store index db04a384c..0d8b3ad52 100755 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/app/controllers/api/sequences_controller.rb b/app/controllers/api/sequences_controller.rb index 9ed2520eb..290dd1765 100644 --- a/app/controllers/api/sequences_controller.rb +++ b/app/controllers/api/sequences_controller.rb @@ -1,4 +1,3 @@ -# Api::SequencesController performs CRUD on stored sequences module Api class SequencesController < Api::AbstractController def create diff --git a/app/mutations/sequences/create.rb b/app/mutations/sequences/create.rb index 5245bd345..c78667f0d 100644 --- a/app/mutations/sequences/create.rb +++ b/app/mutations/sequences/create.rb @@ -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 diff --git a/app/mutations/steps/create.rb b/app/mutations/steps/create.rb index 99b12b978..112d3d545 100644 --- a/app/mutations/steps/create.rb +++ b/app/mutations/steps/create.rb @@ -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 diff --git a/app/mutations/steps/initialize.rb b/app/mutations/steps/initialize.rb new file mode 100644 index 000000000..f6608fd92 --- /dev/null +++ b/app/mutations/steps/initialize.rb @@ -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 diff --git a/config/application.rb b/config/application.rb index 664b12e5c..df2d500aa 100755 --- a/config/application.rb +++ b/config/application.rb @@ -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 diff --git a/lib/mongoid_refinements.rb b/lib/mongoid_refinements.rb new file mode 100644 index 000000000..e1bb8d71a --- /dev/null +++ b/lib/mongoid_refinements.rb @@ -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 diff --git a/spec/controllers/api/sequences/sequences_create_spec.rb b/spec/controllers/api/sequences/sequences_create_spec.rb index af59d3cde..fc99d937f 100644 --- a/spec/controllers/api/sequences/sequences_create_spec.rb +++ b/spec/controllers/api/sequences/sequences_create_spec.rb @@ -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, diff --git a/spec/mutations/sequences/create_spec.rb b/spec/mutations/sequences/create_spec.rb new file mode 100644 index 000000000..09f36bfe8 --- /dev/null +++ b/spec/mutations/sequences/create_spec.rb @@ -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