Skip to content

Commit 333b6b7

Browse files
committed
TaskableAssignee basic implementation
1 parent b6ec4ad commit 333b6b7

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

app/models/todoro/task.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ class Task < ApplicationRecord
44
has_many :reminders, dependent: :destroy
55
has_many :task_steps, class_name: "Todoro::TaskStep", dependent: :destroy
66

7+
has_many :task_assignments, dependent: :destroy
8+
has_many :assignees, through: :task_assignments, source: :assignee
9+
10+
711
validates :title, presence: true
812
validates :status, presence: true, inclusion: { in: %w[pending completed] }
913

@@ -17,6 +21,14 @@ def complete!
1721
update(status: "completed", completed_at: Time.zone.now)
1822
end
1923

24+
def assign_to(assignee)
25+
task_assignments.find_or_create_by(assignee: assignee)
26+
end
27+
28+
def unassign(assignee)
29+
task_assignments.where(assignee: assignee).destroy_all
30+
end
31+
2032
private
2133

2234
def set_default_reminders
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Todoro
2+
class TaskAssignment < ApplicationRecord
3+
belongs_to :task, class_name: "Todoro::Task"
4+
belongs_to :assignee, polymorphic: true
5+
6+
validate :assignee_must_be_taskable
7+
8+
private
9+
10+
def assignee_must_be_taskable
11+
unless Todoro.assignable_models.include?(assignee_type)
12+
errors.add(:assignee, "#{assignee_type} is not a registered taskable model")
13+
end
14+
end
15+
end
16+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateTodoroTaskAssignments < ActiveRecord::Migration[ActiveRecord::Migration.current_version]
2+
def change
3+
create_table :todoro_task_assignments do |t|
4+
t.references :task, null: false, foreign_key: { to_table: :todoro_tasks }
5+
t.references :assignee, polymorphic: true, null: false
6+
7+
t.timestamps
8+
end
9+
end
10+
end

lib/todoro.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
require "todoro/taskable"
44

55
module Todoro
6+
mattr_accessor :assignable_models
7+
self.assignable_models = []
8+
69
def self.taskable_models
710
Rails.application.eager_load!
811

lib/todoro/taskable_assignee.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Todoro
2+
module TaskableAssignee
3+
extend ActiveSupport::Concern
4+
5+
included do
6+
has_many :task_assignments, as: :assignee, class_name: "Todoro::TaskAssignment", dependent: :destroy
7+
has_many :tasks, through: :task_assignments, source: :task
8+
end
9+
10+
class_methods do
11+
def taskable_assignee
12+
Todoro.assignable_models << self.name unless Todoro.assignable_models.include?(self.name)
13+
end
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)