diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 83fbb1e80..f31eeb388 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -2,7 +2,12 @@ class CommentsController < ApplicationController include BubbleScoped, BucketScoped def create - @bubble.comment params.require(:comment).expect(:body) + @bubble.capture new_comment redirect_to @bubble end + + private + def new_comment + Comment.new params.expect(comment: [ :body ]) + end end diff --git a/app/helpers/bubbles_helper.rb b/app/helpers/bubbles_helper.rb index 9cb9bd30d..12c833415 100644 --- a/app/helpers/bubbles_helper.rb +++ b/app/helpers/bubbles_helper.rb @@ -8,7 +8,7 @@ module BubblesHelper end def bubble_size(bubble) - activity = bubble.boost_count + bubble.comments.size + activity = bubble.boost_count + bubble.messages.comments.size rank = case activity when 0..5 then "one" diff --git a/app/models/bubble.rb b/app/models/bubble.rb index b23cd48f2..74f9eb1a9 100644 --- a/app/models/bubble.rb +++ b/app/models/bubble.rb @@ -1,5 +1,5 @@ class Bubble < ApplicationRecord - include Assignable, Boostable, Colored, Commentable, Eventable, Messages, Poppable, Searchable, Staged, Taggable + include Assignable, Boostable, Colored, Eventable, Messages, Poppable, Searchable, Staged, Taggable belongs_to :bucket belongs_to :creator, class_name: "User", default: -> { Current.user } @@ -11,7 +11,12 @@ class Bubble < ApplicationRecord scope :reverse_chronologically, -> { order created_at: :desc, id: :desc } scope :chronologically, -> { order created_at: :asc, id: :asc } - scope :ordered_by_activity, -> { left_joins(:comments).group(:id).order(Arel.sql("COUNT(comments.id) + boost_count DESC")) } + scope :ordered_by_activity, -> do + left_joins(:messages).merge(Message.comments).group(:id).order(Arel.sql("COUNT(messages.id) + boost_count DESC")) + end + scope :ordered_by_comments, -> do + left_joins(:messages).merge(Message.comments).group(:id).order("COUNT(messages.id) DESC") + end scope :with_status, ->(status) do status = status.presence_in %w[ popped active unassigned ] diff --git a/app/models/bubble/commentable.rb b/app/models/bubble/commentable.rb deleted file mode 100644 index 8a3106b18..000000000 --- a/app/models/bubble/commentable.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Bubble::Commentable - extend ActiveSupport::Concern - - included do - has_many :comment_messages, -> { comments }, class_name: "Message" - has_many :comments, through: :comment_messages, source: :messageable, source_type: "Comment" - - scope :ordered_by_comments, -> { left_joins(:comment_messages).group(:id).order("COUNT(messages.id) DESC") } - end - - def comment(body) - capture Comment.new(body: body) - end -end diff --git a/app/models/bubble/eventable.rb b/app/models/bubble/eventable.rb index 04e42d1eb..9c6718b80 100644 --- a/app/models/bubble/eventable.rb +++ b/app/models/bubble/eventable.rb @@ -2,20 +2,17 @@ module Bubble::Eventable extend ActiveSupport::Concern included do - has_many :event_summary_messages, -> { event_summaries }, class_name: "Message" - has_many :event_summaries, through: :event_summary_messages, source: :messageable, source_type: "EventSummary" - after_create -> { track_event :created } end private def track_event(action, creator: Current.user, **particulars) transaction do - find_or_create_active_summary.events << Event.new(action: action, creator: creator, particulars: particulars) + find_or_capture_event_summary.events << Event.new(action: action, creator: creator, particulars: particulars) end end - def find_or_create_active_summary - messages.last&.event_summary || capture(event_summaries.build).messageable + def find_or_capture_event_summary + messages.last&.event_summary || capture(EventSummary.new).event_summary end end diff --git a/app/views/comments/_new.html.erb b/app/views/comments/_new.html.erb index a1055f3d5..5c186babc 100644 --- a/app/views/comments/_new.html.erb +++ b/app/views/comments/_new.html.erb @@ -8,7 +8,7 @@
<%= form_with model: Comment.new, url: bucket_bubble_comments_path(bubble.bucket, bubble), class: "flex flex-column gap full-width", data: { controller: "form", action: "keydown.meta+enter->form#submit" } do |form| %> <%= form.text_area :body, class: "input", required: true, rows: 4, - placeholder: (bubble.comments.empty? && bubble.creator == Current.user) ? "Add some notes…" : "Type your comment…" %> + placeholder: (bubble.messages.comments.empty? && bubble.creator == Current.user) ? "Add some notes…" : "Type your comment…" %> <%= form.button class: "btn btn--reversed center" do %> <%= image_tag "check.svg", aria: { hidden: "true" }, size: 24 %> diff --git a/test/controllers/comments_controller_test.rb b/test/controllers/comments_controller_test.rb index 9578a2ffb..b011a1a71 100644 --- a/test/controllers/comments_controller_test.rb +++ b/test/controllers/comments_controller_test.rb @@ -6,7 +6,7 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest end test "create" do - assert_difference "bubbles(:logo).comments.count", +1 do + assert_difference "bubbles(:logo).messages.comments.count", +1 do post bucket_bubble_comments_url(buckets(:writebook), bubbles(:logo), params: { comment: { body: "Agreed." } }) end diff --git a/test/models/bubble_test.rb b/test/models/bubble_test.rb index 26f01c097..64ea36cec 100644 --- a/test/models/bubble_test.rb +++ b/test/models/bubble_test.rb @@ -26,8 +26,8 @@ class BubbleTest < ActiveSupport::TestCase test "mentioning" do bubble = buckets(:writebook).bubbles.create! title: "Insufficient haggis", creator: users(:kevin) - bubbles(:logo).comment "I hate haggis" - bubbles(:text).comment "I love haggis" + bubbles(:logo).capture Comment.new(body: "I hate haggis") + bubbles(:text).capture Comment.new(body: "I love haggis") assert_equal [ bubble, bubbles(:logo), bubbles(:text) ].sort, Bubble.mentioning("haggis").sort end diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb index 8a579da2e..d898535b0 100644 --- a/test/models/comment_test.rb +++ b/test/models/comment_test.rb @@ -6,7 +6,7 @@ class CommentTest < ActiveSupport::TestCase end test "searchable by body" do - message = bubbles(:logo).comment "I'd prefer something more rustic" + message = bubbles(:logo).capture Comment.new(body: "I'd prefer something more rustic") assert_includes Comment.search("something rustic"), message.comment end