From a7680eaaa6316dee6066b3c40e98e9527aa12113 Mon Sep 17 00:00:00 2001 From: David Wilkie Date: Fri, 30 Jan 2015 10:09:41 +0700 Subject: [PATCH] Update charge_requests to use AASM. #108 --- Gemfile | 6 ++++-- Gemfile.lock | 29 ++++++++++++++++++++++------- app/models/charge_request.rb | 25 ++++++++++++++----------- spec/models/charge_request_spec.rb | 2 +- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/Gemfile b/Gemfile index 9d6de24..cc63b30 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,7 @@ gem "rack-timeout" gem "redis" gem "torasup" gem "nuntium_api", :github => "dwilkie/nuntium-api-ruby" +gem "aasm", :github => "aasm/aasm" gem "state_machine" gem "twilio-ruby" gem "uuid" @@ -43,8 +44,9 @@ end group :test, :development do gem 'rspec-rails' - gem "parallel_tests" - gem "foreman" + gem 'parallel_tests' + gem 'foreman' + gem 'pry' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index aa6b623..70346de 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,9 @@ +GIT + remote: git://github.com/aasm/aasm.git + revision: f033ce0c2a12d815a7c184322c2e97f33ce6fe3b + specs: + aasm (4.0.8) + GIT remote: git://github.com/dwilkie/nuntium-api-ruby.git revision: a7a14d15b529026e96e39ae7f3fa885a9272414d @@ -20,7 +26,7 @@ GIT GIT remote: git://github.com/hexorx/countries.git - revision: 9cd73ec9e014be6082cc47374cae928c1d823f90 + revision: d7dc715f6f7e588100ada5272edaa1057df419bd specs: countries (0.10.0) currencies (~> 0.4.2) @@ -81,13 +87,14 @@ GEM activesupport (>= 3.2.0) json (>= 1.7) mime-types (>= 1.16) + coderay (1.1.0) coffee-rails (4.0.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) coffee-script (2.3.0) coffee-script-source execjs - coffee-script-source (1.8.0) + coffee-script-source (1.9.0) currencies (0.4.2) database_cleaner (1.4.0) debug_inspector (0.0.2) @@ -126,7 +133,7 @@ GEM fog-atmos (0.1.0) fog-core fog-xml - fog-aws (0.0.7) + fog-aws (0.0.8) fog-core (~> 1.27) fog-json (~> 1.0) fog-xml (~> 0.1) @@ -135,7 +142,7 @@ GEM fog-core (~> 1.22) fog-json inflecto (~> 0.0.2) - fog-core (1.27.3) + fog-core (1.27.4) builder excon (~> 0.38) formatador (~> 0.2) @@ -161,7 +168,7 @@ GEM fog-serverlove (0.1.1) fog-core fog-json - fog-softlayer (0.3.30) + fog-softlayer (0.4.0) fog-core fog-json fog-storm_on_demand (0.1.0) @@ -216,6 +223,7 @@ GEM systemu (~> 2.6.2) mail (2.6.3) mime-types (>= 1.16, < 3) + method_source (0.8.2) mime-types (2.4.3) mini_portile (0.6.2) minitest (5.5.1) @@ -233,7 +241,11 @@ GEM parallel_tests (1.0.9) parallel pg (0.18.1) - phony (2.10.2) + phony (2.10.3) + pry (0.10.1) + coderay (~> 1.1.0) + method_source (~> 0.8.1) + slop (~> 3.4) rack (1.6.0) rack-protection (1.5.3) rack @@ -262,7 +274,7 @@ GEM rails_12factor (0.0.3) rails_serve_static_assets rails_stdout_logging - rails_serve_static_assets (0.0.3) + rails_serve_static_assets (0.0.4) rails_stdout_logging (0.0.3) railties (4.2.0) actionpack (= 4.2.0) @@ -314,6 +326,7 @@ GEM rack (~> 1.4) rack-protection (~> 1.4) tilt (~> 1.3, >= 1.3.4) + slop (3.6.0) sprockets (2.12.3) hike (~> 1.2) multi_json (~> 1.0) @@ -370,6 +383,7 @@ PLATFORMS ruby DEPENDENCIES + aasm! capybara carrierwave coffee-rails (~> 4.0.0) @@ -393,6 +407,7 @@ DEPENDENCIES parallel_tests pg phony + pry rack-timeout rails (= 4.2.0) rails_12factor diff --git a/app/models/charge_request.rb b/app/models/charge_request.rb index 21542fc..63ebade 100644 --- a/app/models/charge_request.rb +++ b/app/models/charge_request.rb @@ -3,24 +3,27 @@ class ChargeRequest < ActiveRecord::Base belongs_to :requester, :polymorphic => true include Chibi::Analyzable + include AASM validates :user, :operator, :presence => true after_create :request_charge! - state_machine :initial => :created do - state :awaiting_result, :successful, :errored, :failed - - after_transition :awaiting_result => [:successful, :failed, :errored], :do => :notify_requester! + aasm :column => :state, :whiny_transitions => false do + state :created, :initial => true + state :awaiting_result + state :successful + state :errored + state :failed event :await_result do - transition(:created => :awaiting_result) + transitions(:from => :created, :to => :awaiting_result) end - event :process_result do - transition(:awaiting_result => :successful, :if => :result_successful?) - transition(:awaiting_result => :failed, :if => :result_failed?) - transition(:awaiting_result => :errored) + event :process_result, :after => :notify_requester! do + transitions(:from => :awaiting_result, :to => :successful, :guard => :result_successful?) + transitions(:from => :awaiting_result, :to => :failed, :guard => :result_failed?) + transitions(:from => :awaiting_result, :to => :errored) end end @@ -48,7 +51,7 @@ def slow? def set_result!(result, reason) self.result = result self.reason = reason - process_result + process_result! end private @@ -75,7 +78,7 @@ def request_charge! operator, user.mobile_number ) - await_result + await_result! end def result_successful? diff --git a/spec/models/charge_request_spec.rb b/spec/models/charge_request_spec.rb index aaf6b10..5800ef0 100644 --- a/spec/models/charge_request_spec.rb +++ b/spec/models/charge_request_spec.rb @@ -111,7 +111,7 @@ def create_charge_request(*args) end it "should only mark old charge requests that are 'awaiting_result' or 'created' as 'errored'" do - subject.class.timeout! + described_class.timeout! charge_request_awaiting_result.reload.should be_awaiting_result old_charge_request_awaiting_result.reload.should be_errored old_charge_request_awaiting_result.reason.should == "timeout"