mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-01-01 22:17:49 -05:00
This change introduces polymorphic comments, allowing you to comment on Photos, Plantings, Harvests, and Activities, in addition to Posts.
Key changes include:
- **Comment Model:**
- Made `Comment.commentable` a polymorphic association.
- Added a data migration to move existing post comments to the new structure.
- Updated notification creation logic for polymorphic commentables.
- **CommentsController:**
- Refactored to handle various commentable types using a `find_commentable` method.
- **Ability Model:**
- Updated permissions for comment creation, editing (author/admin), and deletion (author/commentable owner/admin).
- **Routes:**
- Added nested comment routes for Photos, Plantings, Harvests, Activities, and Posts using a `commentable` concern with shallow routes.
- **Views:**
- Created generic partials for comment forms (`_form.html.haml`) and display (`_comment.html.haml`, `_comments.html.haml`).
- Integrated these partials into the show pages of all commentable types.
- Updated `comments/new` and `comments/edit` views to be generic.
- Relevant parent controller `show` actions now eager-load comments.
- **Testing:**
- Added extensive model, controller (using shared examples), and feature tests to cover the new polymorphic comment functionality, including permissions and UI interactions for all commentable types.
- Updated and created factories as needed.
This fulfills the issue requirements for adding comments to multiple resource types with appropriate permissions.
33 lines
1.0 KiB
Ruby
33 lines
1.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Comment < ApplicationRecord
|
|
belongs_to :author, class_name: 'Member', inverse_of: :comments
|
|
belongs_to :commentable, polymorphic: true, counter_cache: true
|
|
|
|
scope :post_order, -> { order(created_at: :asc) } # for display on post page
|
|
|
|
after_create do
|
|
recipient = if commentable.respond_to?(:author)
|
|
commentable.author.id
|
|
elsif commentable.respond_to?(:owner)
|
|
commentable.owner.id
|
|
end
|
|
sender = author.id
|
|
# don't send notifications to yourself
|
|
if recipient && recipient != sender
|
|
Notification.create(
|
|
recipient_id: recipient,
|
|
sender_id: sender,
|
|
subject: "#{author} commented on your #{commentable.class.name.downcase}",
|
|
body:,
|
|
commentable_id: commentable.id,
|
|
commentable_type: commentable.class.name
|
|
)
|
|
end
|
|
end
|
|
|
|
def to_s
|
|
"#{author.login_name} commented on #{commentable.class.name.downcase} ##{commentable.id}"
|
|
end
|
|
end
|