Lean into delegated types APIs

This commit is contained in:
Jose Farias
2024-10-27 17:39:30 -06:00
parent f782b43ef5
commit 0727414017
9 changed files with 22 additions and 29 deletions
+6 -1
View File
@@ -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
+1 -1
View File
@@ -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"
+7 -2
View File
@@ -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 ]
-14
View File
@@ -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
+3 -6
View File
@@ -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
+1 -1
View File
@@ -8,7 +8,7 @@
<div class="comment__body txt-align-start margin-block-start-half">
<%= 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 %>
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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
+1 -1
View File
@@ -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