mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-01-01 14:07: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.
110 lines
3.1 KiB
Ruby
110 lines
3.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class HarvestsController < DataController
|
|
after_action :update_crop_medians, only: %i(create update destroy)
|
|
|
|
def index
|
|
where = {}
|
|
if params[:member_slug]
|
|
@owner = Member.find_by(slug: params[:member_slug])
|
|
where['owner_id'] = @owner.id
|
|
end
|
|
|
|
if params[:crop_slug]
|
|
@crop = Crop.find_by(slug: params[:crop_slug])
|
|
where['crop_id'] = @crop.id
|
|
end
|
|
|
|
if params[:planting_slug]
|
|
@planting = Planting.find_by(slug: params[:planting_slug])
|
|
where['planting_id'] = @planting.id
|
|
end
|
|
|
|
@harvests = Harvest.search('*', where:,
|
|
limit: 100,
|
|
page: params[:page],
|
|
load: false,
|
|
boost_by: [:created_at])
|
|
|
|
@filename = csv_filename
|
|
|
|
respond_with(@harvests)
|
|
end
|
|
|
|
def show
|
|
# @harvest is loaded by load_and_authorize_resource.
|
|
# We need to ensure comments are eager-loaded.
|
|
@harvest = Harvest.includes(comments: :author).find(params[:id])
|
|
@matching_plantings = matching_plantings if @harvest.owner == current_member
|
|
@photos = @harvest.photos.order(created_at: :desc).paginate(page: params[:page])
|
|
respond_with(@harvest)
|
|
end
|
|
|
|
def new
|
|
@harvest = Harvest.new(harvested_at: Time.zone.today)
|
|
@planting = Planting.find_by(slug: params[:planting_slug]) if params[:planting_slug]
|
|
@crop = Crop.find_by(id: params[:crop_id])
|
|
respond_with(@harvest)
|
|
end
|
|
|
|
def edit
|
|
@planting = @harvest.planting if @harvest.planting_id
|
|
respond_with(@harvest)
|
|
end
|
|
|
|
def create
|
|
@harvest.crop_id = @harvest.planting.crop_id if @harvest.planting_id
|
|
@harvest.harvested_at = Time.zone.now if @harvest.harvested_at.blank?
|
|
@harvest.save
|
|
if params[:return] == 'planting'
|
|
respond_with(@harvest, location: @harvest.planting)
|
|
else
|
|
respond_with(@harvest)
|
|
end
|
|
end
|
|
|
|
def update
|
|
@harvest.update(harvest_params)
|
|
respond_with(@harvest)
|
|
end
|
|
|
|
def destroy
|
|
@harvest.destroy
|
|
respond_with(@harvest)
|
|
end
|
|
|
|
private
|
|
|
|
def harvest_params
|
|
params.require(:harvest)
|
|
.permit(:planting_id, :crop_id, :harvested_at, :description,
|
|
:quantity, :unit, :weight_quantity, :weight_unit,
|
|
:plant_part_id, :slug, :si_weight)
|
|
.merge(owner_id: current_member.id)
|
|
end
|
|
|
|
def matching_plantings
|
|
Planting.where(crop: @harvest.crop, owner: @harvest.owner)
|
|
.where('(planted_at IS NULL OR planted_at <= ?)', @harvest.harvested_at)
|
|
.where('(finished_at IS NULL OR finished_at >= ?)', @harvest.harvested_at)
|
|
end
|
|
|
|
def csv_filename
|
|
specifics = if @owner
|
|
"#{@owner.to_param}-"
|
|
elsif @crop
|
|
"#{@crop.to_param}-"
|
|
end
|
|
"Growstuff-#{specifics}Harvests-#{Time.zone.now.to_fs(:number)}.csv"
|
|
end
|
|
|
|
def update_crop_medians
|
|
# We only update medians to predict plantings
|
|
# if this harvest is not linked to a planting, then do nothing
|
|
return if @harvest.planting.nil?
|
|
|
|
@harvest.planting.update_harvest_days!
|
|
@harvest.crop.update_harvest_medians
|
|
end
|
|
end
|