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 @@