[UNSTABLE] Make sequence.args user configurable
This commit is contained in:
parent
ccb873f65f
commit
c2c9afb14d
|
@ -6,7 +6,6 @@ module SequenceMigration
|
||||||
CREATED_ON = "NOVEMBER 30 2017"
|
CREATED_ON = "NOVEMBER 30 2017"
|
||||||
|
|
||||||
def up
|
def up
|
||||||
binding.pry
|
|
||||||
sequence.args["locals"] ||= Sequence::NOTHING
|
sequence.args["locals"] ||= Sequence::NOTHING
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -154,7 +154,7 @@ module CeleryScriptSettingsBag
|
||||||
.defineNode(:send_message, [:message, :message_type], [:channel])
|
.defineNode(:send_message, [:message, :message_type], [:channel])
|
||||||
.defineNode(:execute, [:sequence_id])
|
.defineNode(:execute, [:sequence_id])
|
||||||
.defineNode(:_if, [:lhs, :op, :rhs, :_then, :_else], [:pair])
|
.defineNode(:_if, [:lhs, :op, :rhs, :_then, :_else], [:pair])
|
||||||
.defineNode(:sequence, [:version], STEPS)
|
.defineNode(:sequence, [:version, :locals], STEPS)
|
||||||
.defineNode(:home, [:speed, :axis], [])
|
.defineNode(:home, [:speed, :axis], [])
|
||||||
.defineNode(:find_home, [:speed, :axis], [])
|
.defineNode(:find_home, [:speed, :axis], [])
|
||||||
.defineNode(:zero, [:axis], [])
|
.defineNode(:zero, [:axis], [])
|
||||||
|
|
|
@ -3,7 +3,9 @@
|
||||||
# most of the functionality of a programming language such a variables and
|
# most of the functionality of a programming language such a variables and
|
||||||
# conditional logic.
|
# conditional logic.
|
||||||
class Sequence < ApplicationRecord
|
class Sequence < ApplicationRecord
|
||||||
NOTHING = { "kind" => "nothing", "args" => {} }
|
NOTHING = { "kind" => "nothing", "args" => {} }
|
||||||
|
DEFAULT_ARGS = { "locals" => NOTHING,
|
||||||
|
"version" => SequenceMigration::Base.latest_version }
|
||||||
# Does some extra magic for serialized columns for us, such as providing a
|
# Does some extra magic for serialized columns for us, such as providing a
|
||||||
# default value and making hashes have indifferent access.
|
# default value and making hashes have indifferent access.
|
||||||
class CustomSerializer
|
class CustomSerializer
|
||||||
|
@ -47,9 +49,7 @@ class Sequence < ApplicationRecord
|
||||||
before_validation :set_defaults
|
before_validation :set_defaults
|
||||||
|
|
||||||
def set_defaults
|
def set_defaults
|
||||||
self.args ||= {}
|
self.args = {}.merge(DEFAULT_ARGS).merge(self.args)
|
||||||
self.args["version"] ||= SequenceMigration::Base.latest_version
|
|
||||||
self.args["locals"] ||= NOTHING
|
|
||||||
self.color ||= "gray"
|
self.color ||= "gray"
|
||||||
self.kind ||= "sequence"
|
self.kind ||= "sequence"
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,15 +22,13 @@ module Sequences
|
||||||
# first level, though. I would like to recursively strip out "noise" via
|
# first level, though. I would like to recursively strip out "noise" via
|
||||||
# CeleryScript::JSONClimber. I am holding off for now in the name of time.
|
# CeleryScript::JSONClimber. I am holding off for now in the name of time.
|
||||||
(inputs[:body] || []).map! { |x| x.slice(*ALLOWED_NODE_KEYS) }
|
(inputs[:body] || []).map! { |x| x.slice(*ALLOWED_NODE_KEYS) }
|
||||||
|
inputs[:args] = Sequence::DEFAULT_ARGS.merge(inputs[:args] || {})
|
||||||
add_error :body, :syntax_error, checker.error.message if !checker.valid?
|
add_error :body, :syntax_error, checker.error.message if !checker.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
def seq
|
def seq
|
||||||
@seq ||= {body: [],
|
@seq ||= {body: [], args: {}, kind: "sequence"}
|
||||||
args: { version: SequenceMigration::Base.latest_version },
|
.merge(inputs.symbolize_keys.slice(:body, :kind, :args))
|
||||||
kind: "sequence"}.merge(inputs.symbolize_keys.slice(:body,
|
|
||||||
:kind,
|
|
||||||
:args))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def reload_dependencies(sequence)
|
def reload_dependencies(sequence)
|
||||||
|
|
|
@ -76,6 +76,31 @@ describe Api::SequencesController do
|
||||||
expect(validated_count).to eq(new_count)
|
expect(validated_count).to eq(new_count)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'doesnt allow nonsense in `sequence.args.locals`' do
|
||||||
|
input = { name: "Scare Birds",
|
||||||
|
body: [],
|
||||||
|
# Intentional nonsense to check validation logic.
|
||||||
|
args: { locals: { kind: "wait", args: { milliseconds: 5000 } } }
|
||||||
|
}
|
||||||
|
|
||||||
|
sign_in user
|
||||||
|
post :create, body: input.to_json, params: {format: :json}
|
||||||
|
expect(response.status).to eq(422)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'strips excess `args`' do
|
||||||
|
pending
|
||||||
|
input = { name: "Scare Birds",
|
||||||
|
body: [],
|
||||||
|
# Intentional nonsense to check validation logic.
|
||||||
|
args: { foo: "BAR" } }
|
||||||
|
|
||||||
|
sign_in user
|
||||||
|
post :create, body: input.to_json, params: {format: :json}
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
expect(sequence.last.args[:foo]).to eq(nil)
|
||||||
|
end
|
||||||
|
|
||||||
it 'tracks Points' do
|
it 'tracks Points' do
|
||||||
point = FactoryBot.create(:point, device: user.device)
|
point = FactoryBot.create(:point, device: user.device)
|
||||||
SequenceDependency.delete_all
|
SequenceDependency.delete_all
|
||||||
|
|
|
@ -8,6 +8,8 @@ describe Api::SequencesController do
|
||||||
|
|
||||||
let(:user) { FactoryBot.create(:user) }
|
let(:user) { FactoryBot.create(:user) }
|
||||||
|
|
||||||
|
it 'doesnt allow nonsense in `sequence.args.locals`'
|
||||||
|
|
||||||
it 'refreshes sequence dependencies on update' do
|
it 'refreshes sequence dependencies on update' do
|
||||||
SequenceDependency.destroy_all
|
SequenceDependency.destroy_all
|
||||||
old_count = SequenceDependency.count
|
old_count = SequenceDependency.count
|
||||||
|
|
|
@ -6,6 +6,7 @@ describe CeleryScript::Checker do
|
||||||
{
|
{
|
||||||
kind: "sequence",
|
kind: "sequence",
|
||||||
args: {
|
args: {
|
||||||
|
locals: Sequence::NOTHING,
|
||||||
version: 0
|
version: 0
|
||||||
},
|
},
|
||||||
comment: "Properly formatted, syntactically valid sequence.",
|
comment: "Properly formatted, syntactically valid sequence.",
|
||||||
|
|
|
@ -64,7 +64,6 @@ describe SequenceMigration do
|
||||||
expect(s.body[0]["args"]["speed"]).to eq(200)
|
expect(s.body[0]["args"]["speed"]).to eq(200)
|
||||||
expect(s.body[1]["args"]["speed"]).to eq(200)
|
expect(s.body[1]["args"]["speed"]).to eq(200)
|
||||||
expect(s.body[2]["args"]["speed"]).to eq(200)
|
expect(s.body[2]["args"]["speed"]).to eq(200)
|
||||||
expect(s.args["locals"]).to eq(nil)
|
|
||||||
|
|
||||||
s.maybe_migrate
|
s.maybe_migrate
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue