mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-11 17:24:50 -04:00
- Added Rails.cache.fetch to `sunniness` and `planted_from` actions. - Refactored crop loading into a `before_action :set_crop`. - Updated specs to verify caching behavior and ensure coverage. Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>
42 lines
985 B
Ruby
42 lines
985 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Charts
|
|
class CropsController < ApplicationController
|
|
respond_to :json
|
|
before_action :set_crop
|
|
|
|
def sunniness
|
|
pie_chart_query 'sunniness'
|
|
end
|
|
|
|
def planted_from
|
|
pie_chart_query 'planted_from'
|
|
end
|
|
|
|
def harvested_for
|
|
data = Rails.cache.fetch("#{@crop.cache_key_with_version}/harvested_for", expires_in: 1.day) do
|
|
Harvest.joins(:plant_part)
|
|
.where(crop: @crop)
|
|
.group("plant_parts.name").count(:id)
|
|
end
|
|
render json: data
|
|
end
|
|
|
|
private
|
|
|
|
def set_crop
|
|
@crop = Crop.find_by!(slug: params[:crop_slug])
|
|
end
|
|
|
|
def pie_chart_query(field)
|
|
data = Rails.cache.fetch("#{@crop.cache_key_with_version}/#{field}", expires_in: 1.day) do
|
|
Planting.where(crop: @crop)
|
|
.where.not(field.to_sym => nil)
|
|
.where.not(field.to_sym => '')
|
|
.group(field.to_sym).count(:id)
|
|
end
|
|
render json: data
|
|
end
|
|
end
|
|
end
|