diff --git a/Gemfile b/Gemfile index f466cf3..f04db6c 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,8 @@ 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] 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 3c23424..c11d709 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -1,2 +1,43 @@ 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 } + + 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 :qa 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 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