mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-13 18:55:45 -04:00
* Add seed ancestry, and seeds.finished_at * Plantings and seeds produce each other * Permissions for seeds actions * View update, for seed actions, and planting ancestry * Routes for seeds * Scopes for harvests * Spec updates for seeds * Removed in-line style * Add seed ancestry, and seeds.finished_at * Plantings and seeds produce each other * Permissions for seeds actions * View update, for seed actions, and planting ancestry * Routes for seeds * Scopes for harvests * Spec updates for seeds * Moved finishable to a concern * Seeds.finished_at * rubocop fixes * Made seeds sown migration the most recent * Specs for home page * Only show current seeds on home page * Seeds appear for logged in or not * Buttons to mark seeds finished * JS for marking seeds finished * Some actions only appear if seed or planting is active * Fixed up display of home page * Fixed typo in admin members#index * Tidying up actions on all the things * Harvest description in #index * Truncate garden description if long * Updated link label in view spec * Show planted_from always, becuase it might be a parent seed * find correct link in spec adding photos to garden * fixed spec finding link to edit garden * Better harvest description truncation * Helping spec find the edit button * specs for the home page * Re-instate crops js, but in the correct file now * Fixed link to garden in actions * Tweaking mobile view
108 lines
2.8 KiB
Ruby
108 lines
2.8 KiB
Ruby
class PlantingsController < ApplicationController
|
|
before_action :authenticate_member!, except: %i(index show)
|
|
after_action :expire_homepage, only: %i(create update destroy)
|
|
after_action :update_crop_medians, only: %i(create update destroy)
|
|
after_action :update_planting_medians, only: :update
|
|
load_and_authorize_resource
|
|
|
|
respond_to :html, :json
|
|
respond_to :csv, :rss, only: [:index]
|
|
responders :flash
|
|
|
|
def index
|
|
@owner = Member.find_by(slug: params[:owner]) if params[:owner]
|
|
@crop = Crop.find_by(slug: params[:crop]) if params[:crop]
|
|
@show_all = params[:all] == '1'
|
|
|
|
@plantings = plantings
|
|
|
|
specifics = if @owner
|
|
"#{@owner.login_name}-"
|
|
elsif @crop
|
|
"#{@crop.name}-"
|
|
end
|
|
|
|
@filename = "Growstuff-#{specifics}Plantings-#{Time.zone.now.to_s(:number)}.csv"
|
|
|
|
respond_with(@plantings)
|
|
end
|
|
|
|
def show
|
|
@planting = Planting.includes(:owner, :crop, :garden, :photos)
|
|
.friendly
|
|
.find(params[:id])
|
|
@photos = @planting.photos.order(date_taken: :desc).includes(:owner).paginate(page: params[:page])
|
|
respond_with @planting
|
|
end
|
|
|
|
def new
|
|
@planting = Planting.new(planted_at: Time.zone.today)
|
|
@seed = Seed.find_by(slug: params[:seed_id]) if params[:seed_id]
|
|
|
|
# using find_by_id here because it returns nil, unlike find
|
|
@crop = Crop.approved.find_by(id: params[:crop_id]) || Crop.new
|
|
@garden = Garden.find_by(owner: current_member, id: params[:garden_id]) || Garden.new
|
|
|
|
respond_with @planting
|
|
end
|
|
|
|
def edit
|
|
# the following are needed to display the form but aren't used
|
|
@crop = Crop.new
|
|
@garden = Garden.new
|
|
end
|
|
|
|
def create
|
|
@planting = Planting.new(planting_params)
|
|
@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
|
|
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
|
|
end
|