From 044117f9c12cf0844fd8e8018e99009cd189fee9 Mon Sep 17 00:00:00 2001 From: Jeffrey Hardy Date: Wed, 2 Oct 2024 12:28:03 -0400 Subject: [PATCH] Extract Bucket::Assignable and rename Assignment#user to #assignee --- app/controllers/assignments_controller.rb | 17 +++-------------- app/helpers/comments_helper.rb | 4 ++-- app/models/assignment.rb | 4 ++-- app/models/bubble.rb | 5 +---- app/models/bubble/assignable.rb | 8 ++++++++ app/views/bubbles/_assignments.html.erb | 4 ++-- ...161258_rename_assignment_user_to_assignee.rb | 5 +++++ db/schema.rb | 6 +++--- test/fixtures/assignments.yml | 4 ++-- 9 files changed, 28 insertions(+), 29 deletions(-) create mode 100644 app/models/bubble/assignable.rb create mode 100644 db/migrate/20241002161258_rename_assignment_user_to_assignee.rb diff --git a/app/controllers/assignments_controller.rb b/app/controllers/assignments_controller.rb index 96c213ded..ec01e3598 100644 --- a/app/controllers/assignments_controller.rb +++ b/app/controllers/assignments_controller.rb @@ -1,24 +1,13 @@ class AssignmentsController < ApplicationController include BubbleScoped, BucketScoped - before_action :set_assignment, only: :update - def create - @assignment = @bubble.assignments.create!(assignment_params) - redirect_to bucket_bubble_url(@bucket, @bubble) - end - - def update - @assignment.update!(assignment_params) + @bubble.assignments.create!(assignee: find_assignee) redirect_to bucket_bubble_url(@bucket, @bubble) end private - def assignment_params - params.require(:assignment).permit(:user_id) - end - - def set_assignment - @assignment = @bubble.assignments.find(params[:id]) + def find_assignee + @bucket.users.active.find(params[:assignee_id]) end end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 456942709..18586dbe5 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -29,7 +29,7 @@ module CommentsHelper def initial_assignment_info(combined_collection) initial_assignment = combined_collection.find { |item| item.is_a?(Assignment) } - "assigned to #{initial_assignment.user.name}" if initial_assignment + "assigned to #{initial_assignment.assignee.name}" if initial_assignment end def render_initial_boosts(combined_collection) @@ -75,6 +75,6 @@ module CommentsHelper end def render_grouped_assignments(assignments) - assignments.map { |assignment| "Assigned to #{assignment.user.name} #{time_ago_in_words(assignment.created_at)} ago" } + assignments.map { |assignment| "Assigned to #{assignment.assignee.name} #{time_ago_in_words(assignment.created_at)} ago" } end end diff --git a/app/models/assignment.rb b/app/models/assignment.rb index 4eed95605..3360eb4e5 100644 --- a/app/models/assignment.rb +++ b/app/models/assignment.rb @@ -1,6 +1,6 @@ class Assignment < ApplicationRecord - belongs_to :user belongs_to :bubble + belongs_to :assignee, class_name: "User" - validates :user_id, uniqueness: { scope: :bubble_id } + validates :assignee, uniqueness: { scope: :bubble } end diff --git a/app/models/bubble.rb b/app/models/bubble.rb index 07277955b..9a1a66a2c 100644 --- a/app/models/bubble.rb +++ b/app/models/bubble.rb @@ -1,5 +1,5 @@ class Bubble < ApplicationRecord - include Colored, Searchable + include Assignable, Colored, Searchable belongs_to :bucket belongs_to :creator, class_name: "User", default: -> { Current.user } @@ -10,9 +10,6 @@ class Bubble < ApplicationRecord has_many :taggings, dependent: :destroy has_many :tags, through: :taggings - has_many :assignments, dependent: :destroy - has_many :assignees, through: :assignments, source: :user - has_one_attached :image, dependent: :purge_later scope :reverse_chronologically, -> { order(created_at: :desc, id: :desc) } diff --git a/app/models/bubble/assignable.rb b/app/models/bubble/assignable.rb new file mode 100644 index 000000000..9c221abbc --- /dev/null +++ b/app/models/bubble/assignable.rb @@ -0,0 +1,8 @@ +module Bubble::Assignable + extend ActiveSupport::Concern + + included do + has_many :assignments, dependent: :destroy + has_many :assignees, through: :assignments + end +end diff --git a/app/views/bubbles/_assignments.html.erb b/app/views/bubbles/_assignments.html.erb index 78a516a06..a3957711a 100644 --- a/app/views/bubbles/_assignments.html.erb +++ b/app/views/bubbles/_assignments.html.erb @@ -1,5 +1,5 @@ "> - <%= form_with model: Assignment.new, url: bucket_bubble_assignments_path(bubble.bucket, bubble), data: { controller: "form" } do |form| %> + <%= form_with url: bucket_bubble_assignments_path(bubble.bucket, bubble), data: { controller: "form" } do |form| %>