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