[UNSTABLE] Migrating from Commands to StepParams
parent
29c67634e4
commit
e108170f25
|
@ -8,10 +8,26 @@ class Step < ActiveRecord::Base
|
||||||
validates :message_type, presence: true
|
validates :message_type, presence: true
|
||||||
validates :position, presence: true
|
validates :position, presence: true
|
||||||
|
|
||||||
has_may :step_params
|
has_many :step_params, dependent: :destroy
|
||||||
|
|
||||||
|
def command=(step_params)
|
||||||
|
ActiveRecord::Base.transaction do
|
||||||
|
step_params
|
||||||
|
.map { |k, v| StepParam.create!(step_id: id, key: k, value: v) }
|
||||||
|
.inject({}) do |accum, step|
|
||||||
|
accum[step.key] = step.value
|
||||||
|
accum
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def command
|
def command
|
||||||
raise "TODO: Return a hash here."
|
self
|
||||||
|
.step_params
|
||||||
|
.inject({}) do |accum, step|
|
||||||
|
accum[step.key] = step.value
|
||||||
|
accum
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# def all_steps
|
# def all_steps
|
||||||
|
|
|
@ -12,15 +12,22 @@ module Steps
|
||||||
integer :position
|
integer :position
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute
|
def validate
|
||||||
step = Step.new(inputs)
|
command.each do |k, v|
|
||||||
# Make sure position is always > 0.
|
unless k.is_a?(String)
|
||||||
step.position ||= step.sequence.steps.count
|
add_error :command,
|
||||||
|
:not_string,
|
||||||
|
"command.#{k} must be a string."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if step.valid? && step.save
|
def execute
|
||||||
return step
|
ActiveRecord::Base.transaction do
|
||||||
else
|
step = Step.create!(inputs.except(:message_type).merge(position: sequence.steps.count))
|
||||||
add_error :step, :invalid, step.errors.messages
|
command.map { |k, v| step.step_params.create!(key: k, value: v) }
|
||||||
|
# Make sure position is always > 0.
|
||||||
|
step
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
require 'rails_helper'
|
# require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe StepParam, type: :model do
|
# RSpec.describe StepParam, type: :model do
|
||||||
pending "add some examples to (or delete) #{__FILE__}"
|
# pending "add some examples to (or delete) #{__FILE__}"
|
||||||
end
|
# end
|
||||||
|
|
Loading…
Reference in New Issue