Files
growstuff/app/controllers/activities_controller.rb
google-labs-jules[bot] f2421bf4c7 Here's the plan to add polymorphic comments and update related functionality:
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.
2025-05-25 02:03:17 +00:00

93 lines
2.0 KiB
Ruby

# frozen_string_literal: true
class ActivitiesController < DataController
def index
@show_all = params[:all] == '1'
where = {}
where['active'] = true unless @show_all
if params[:member_slug]
@owner = Member.find_by(slug: params[:member_slug])
where['owner_id'] = @owner.id unless @owner.nil?
end
@activities = Activity.search(
where:,
page: params[:page],
limit: 30,
boost_by: [:created_at],
load: false
)
@filename = "Growstuff-#{specifics}Activities-#{Time.zone.now.to_fs(:number)}.csv"
respond_with(@activities)
end
def show
# @activity is loaded by load_and_authorize_resource.
# We need to ensure comments are eager-loaded.
@activity = Activity.includes(comments: :author).find(params[:id])
respond_with @activity
end
def new
@activity = Activity.new(
owner: current_member,
due_date: Date.today
)
if params[:garden_id]
@activity.garden = Garden.find_by(
owner: current_member,
id: params[:garden_id]
)
end
if params[:planting_id]
@activity.planting = Planting.find_by(
owner: current_member,
id: params[:planting_id]
)
end
respond_with @activity
end
def edit
# the following are needed to display the form but aren't used
@gardens = @activity.owner.gardens.active.order_by_name
@plantings = @activity.owner.plantings.active
end
def create
@activity = Activity.new(activity_params)
@activity.owner = current_member
@activity.save
respond_with @activity
end
def update
@activity.update(activity_params)
respond_with @activity
end
def destroy
@activity.destroy
respond_with @activity, location: @activity.garden
end
private
def activity_params
params.require(:activity).permit(
:name, :description, :category, :finished,
:garden_id, :planting_id, :due_date
)
end
def specifics
return if @owner.blank?
"#{@owner.to_param}-"
end
end