From 44de11d009b42011bb0a73cf09a74c6ed1f6614f Mon Sep 17 00:00:00 2001 From: "konstantin.grabar" Date: Mon, 28 May 2012 21:47:53 +0400 Subject: [PATCH] [refs #505] Add events, states and other base state machine logic --- Gemfile | 1 + Gemfile.lock | 1 + app/models/build_list.rb | 73 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/Gemfile b/Gemfile index f4bcc710b..1691f9efd 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ gem 'paperclip', '~> 3.0.2' gem 'delayed_job_active_record', '~> 0.3.2' gem 'russian', '~> 0.6.0' gem 'highline', '~> 1.6.11' +gem 'state_machine' gem 'jbuilder' diff --git a/Gemfile.lock b/Gemfile.lock index 4b3b34f2f..fda927bb5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -364,6 +364,7 @@ DEPENDENCIES sass-rails (~> 3.2.5) shotgun shoulda + state_machine therubyracer (~> 0.10.1) therubyrhino (~> 1.73.1) trinidad (~> 1.0.2) diff --git a/app/models/build_list.rb b/app/models/build_list.rb index 3cee985fb..f34843d9e 100644 --- a/app/models/build_list.rb +++ b/app/models/build_list.rb @@ -100,6 +100,79 @@ class BuildList < ActiveRecord::Base after_create :place_build after_destroy :delete_container + state_machine :status, :initial => :waiting_for_response do + + event :wait_assembly do + transition :waiting_for_response => :build_pending + end + + event :wait_platform do + transition :waiting_for_response => :platform_pending + end + + event :lose_platform do + transition :waiting_for_response => :platform_not_found + end + + event :lose_project do + transition :waiting_for_response => :project_not_found + end + + event :lose_project_version do + transition :waiting_for_response => :project_version_not_found + end + + event :start do + transition [ + :build_pending, + :platform_pending + ] => :build_started + end + + event :cancel do + transition [ + :build_pending, + :platform_pending + ] => :build_canceled, :if => lambda { |build_list| build_list.can_cancel? } + end + + event :finish_build do + transition [ + :build_started, + :build_canceled + ] => :success + end + + event :fail_build do + transition :build_started => :build_error + end + + event :publish do + transition [ + :success, + :failed_publish + ] => :build_publish, :if => lambda { |build_list| build_list.can_publish? } # TODO: Remove can_publish? + # we do not need this after state machine + end + + event :reject_publish do + transition :success => :rejected_publish, :if => lambda { |build_list| build_list.can_reject_publish? } + end + + event :finish_publish do + transition :build_publish => :build_published + end + + event :fail_publish do + transition :build_publish => :failed_publish + end + + HUMAN_STATUSES.each do |code,name| + state name, :value => code + end + + end + def self.human_status(status) I18n.t("layout.build_lists.statuses.#{HUMAN_STATUSES[status]}") end