From b0a3270d1c2a6b377e41a0ca2b371cf70fcbc9c4 Mon Sep 17 00:00:00 2001 From: Leenday Date: Sun, 11 Oct 2020 20:10:35 +0300 Subject: [PATCH 1/4] add associations and validations --- app/models/task.rb | 7 +++++++ app/models/user.rb | 6 ++++++ test/factories/tasks.rb | 5 +++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/models/task.rb b/app/models/task.rb index 3c23424..84088dd 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -1,2 +1,9 @@ class Task < ApplicationRecord + belongs_to :author, class_name: 'User' + belongs_to :assignee, class_name: 'User', optional: true + + validates :name, presence: true + validates :description, presence: true + validates :author, presence: true + validates :description, length: { maximum: 500 } end diff --git a/app/models/user.rb b/app/models/user.rb index d67da20..b677190 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,9 @@ class User < ApplicationRecord has_secure_password + has_many :my_tasks, class_name: 'Task', foreign_key: :author_id + has_many :assigned_tasks, class_name: 'Task', foreign_key: :assignee_id + + validates :first_name, presence: true, length: { minimum: 2 } + validates :last_name, presence: true, length: { minimum: 2 } + validates :email, presence: true, uniqueness: true, format: { with: /\w*@{1}\w*/ } end diff --git a/test/factories/tasks.rb b/test/factories/tasks.rb index 62ed71a..0edf259 100644 --- a/test/factories/tasks.rb +++ b/test/factories/tasks.rb @@ -2,9 +2,10 @@ factory :task do name description - author_id { 1 } - assignee_id { 1 } state expired_at + + association :author, factory: :manager + association :assignee, factory: :developer end end From 2ade999dde8c2ba4e4a6032b8ba4b19fd1efa718 Mon Sep 17 00:00:00 2001 From: Leenday Date: Tue, 13 Oct 2020 22:04:30 +0300 Subject: [PATCH 2/4] add a state_machine gem and states to task --- Gemfile | 1 + Gemfile.lock | 8 ++++++++ app/models/task.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/Gemfile b/Gemfile index f466cf3..3d2a970 100644 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,7 @@ group :development, :test do gem 'byebug', platforms: %i[mri mingw x64_mingw] gem 'factory_bot_rails' gem 'rubocop' + gem 'state_machines-activerecord' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index f2b0a88..41da6db 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -186,6 +186,13 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) + state_machines (0.5.0) + state_machines-activemodel (0.7.1) + activemodel (>= 4.1) + state_machines (>= 0.5.0) + state_machines-activerecord (0.6.0) + activerecord (>= 4.1) + state_machines-activemodel (>= 0.5.0) thor (1.0.1) thread_safe (0.3.6) tilt (2.0.10) @@ -229,6 +236,7 @@ DEPENDENCIES rubocop sass-rails (>= 6) selenium-webdriver + state_machines-activerecord web-console (>= 3.3.0) webdrivers webpacker (~> 4.0) diff --git a/app/models/task.rb b/app/models/task.rb index 84088dd..2f5cd6b 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -6,4 +6,30 @@ class Task < ApplicationRecord validates :description, presence: true validates :author, presence: true validates :description, length: { maximum: 500 } + + state_machine initial: :new_task do + event :develop do + transition %i[new_task in_qa in_codereview] => :in_development + end + + event :test do + transition in_development: :in_qa + end + + event :codereview do + transition in_qa: :in_codereview + end + + event :reviwed do + transition in_codereview: :ready_for_release + end + + event :release do + transition ready_for_release: :released + end + + event :archive do + transition %i[new_task released] => :archived + end + end end From a5cbc7396b70486dae87ce0aeb1541b9f7b7f641 Mon Sep 17 00:00:00 2001 From: Leenday Date: Thu, 22 Oct 2020 23:57:33 +0300 Subject: [PATCH 3/4] described all states --- app/models/task.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/task.rb b/app/models/task.rb index 2f5cd6b..c11d709 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -8,11 +8,19 @@ class Task < ApplicationRecord validates :description, length: { maximum: 500 } state_machine initial: :new_task do + state :new_task + state :in_development + state :in_qa + state :in_codereview + state :ready_for_release + state :released + state :archived + event :develop do transition %i[new_task in_qa in_codereview] => :in_development end - event :test do + event :qa do transition in_development: :in_qa end From 916f2744f9556a32743a4cbe2cd6064c57aee15a Mon Sep 17 00:00:00 2001 From: Leenday Date: Mon, 16 Nov 2020 22:29:36 +0300 Subject: [PATCH 4/4] take out state-machine gem --- Gemfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 3d2a970..f04db6c 100644 --- a/Gemfile +++ b/Gemfile @@ -26,12 +26,13 @@ gem 'bcrypt', '~> 3.1.7' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.2', require: false +gem 'state_machines-activerecord' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: %i[mri mingw x64_mingw] gem 'factory_bot_rails' gem 'rubocop' - gem 'state_machines-activerecord' end group :development do