From e108170f25797e940bcec074ebfcde0f98dfa6e2 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Mon, 19 Sep 2016 16:51:38 -0500 Subject: [PATCH] [UNSTABLE] Migrating from Commands to StepParams --- app/models/step.rb | 20 ++++++++++++++++++-- app/mutations/steps/create.rb | 23 +++++++++++++++-------- spec/models/step_param_spec.rb | 8 ++++---- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/app/models/step.rb b/app/models/step.rb index 68c1a4e50..2586ac416 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -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 diff --git a/app/mutations/steps/create.rb b/app/mutations/steps/create.rb index 3b4251b6e..7ebaf7ad4 100644 --- a/app/mutations/steps/create.rb +++ b/app/mutations/steps/create.rb @@ -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 diff --git a/spec/models/step_param_spec.rb b/spec/models/step_param_spec.rb index de5306476..45611668f 100644 --- a/spec/models/step_param_spec.rb +++ b/spec/models/step_param_spec.rb @@ -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