Files
growstuff/app/controllers/plantings_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

146 lines
3.9 KiB
Ruby

# frozen_string_literal: true
class PlantingsController < DataController
after_action :update_crop_medians, only: %i(create update destroy)
after_action :update_planting_medians, only: :update
respond_to :ics, only: [:index] # TODO: This can be shifted up when all relevant controllers respond to ical
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
if params[:crop_slug]
@crop = Crop.find_by(slug: params[:crop_slug])
where['crop_id'] = @crop.id unless @crop.nil?
end
@plantings = Planting.search(
where:,
page: params[:page],
limit: 30,
boost_by: [:created_at],
load: false
)
@filename = "Growstuff-#{specifics}Plantings-#{Time.zone.now.to_fs(:number)}.csv"
respond_with(@plantings)
end
def show
# @planting is loaded by load_and_authorize_resource.
# We need to ensure comments are eager-loaded.
@planting = Planting.includes(comments: :author).find(params[:id])
@photos = @planting.photos.includes(:owner).order(date_taken: :desc)
@harvests = Harvest.search(where: { planting_id: @planting.id })
@current_activities = @planting.activities.current.includes(:owner).order(created_at: :desc)
@matching_seeds = matching_seeds
@crop = @planting.crop
# TODO: use elastic search long/lat
@neighbours = @planting.nearby_same_crop
.where.not(id: @planting.id)
.includes(:owner, :crop, :garden)
.limit(6)
respond_with @planting
end
def new
@planting = Planting.new(
planted_at: Time.zone.today,
owner: current_member,
garden: current_member.gardens.first
)
@seed = Seed.find_by(slug: params[:seed_id]) if params[:seed_id]
@crop = Crop.approved.find_by(id: params[:crop_id]) || Crop.new
if params[:garden_id]
@planting.garden = Garden.find_by(
owner: current_member,
id: params[:garden_id]
)
end
respond_with @planting
end
def edit
# the following are needed to display the form but aren't used
@crop = Crop.new
@gardens = @planting.owner.gardens.active.order_by_name
end
def create
@planting = Planting.new(planting_params)
@planting.planted_at = Time.zone.now if @planting.planted_at.blank?
@planting.owner = current_member
@planting.crop = @planting.parent_seed.crop if @planting.parent_seed.present?
@planting.save
respond_with @planting
end
def update
@planting.update(planting_params)
respond_with @planting
end
def destroy
@planting.destroy
respond_with @planting, location: @planting.garden
end
private
def update_crop_medians
@planting.crop.update_lifespan_medians if @planting.crop.present?
end
def update_planting_medians
@planting.update_harvest_days!
end
def planting_params
params[:planted_at] = parse_date(params[:planted_at]) if params[:planted_at]
params.require(:planting).permit(
:crop_id, :description, :garden_id, :planted_at,
:parent_seed_id,
:quantity, :sunniness, :planted_from, :finished,
:finished_at
)
end
def plantings
p = if @owner
@owner.plantings
elsif @crop
@crop.plantings
else
Planting
end
p = p.current unless @show_all
p.joins(:owner, :crop, :garden)
.order(created_at: :desc)
.includes(:crop, :owner, :garden)
.paginate(page: params[:page])
end
def matching_seeds
Seed.where(crop: @planting.crop, owner: @planting.owner)
.where('(finished_at IS NULL OR finished_at >= ?)', @planting.planted_at)
.where('(saved_at IS NULL OR saved_at <= ?)', @planting.planted_at)
end
def specifics
if @owner.present?
"#{@owner.to_param}-"
elsif @crop.present?
"#{@crop.to_param}-"
end
end
end