Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions app/controllers/charts/crops_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Charts
class CropsController < ApplicationController
respond_to :json
before_action :set_crop

def sunniness
pie_chart_query 'sunniness'
Expand All @@ -13,7 +14,6 @@ def planted_from
end

def harvested_for
@crop = Crop.find_by!(slug: params[:crop_slug])
data = Rails.cache.fetch("#{@crop.cache_key_with_version}/harvested_for", expires_in: 1.day) do
Harvest.joins(:plant_part)
.where(crop: @crop)
Expand All @@ -24,12 +24,18 @@ def harvested_for

private

def pie_chart_query(field)
def set_crop
@crop = Crop.find_by!(slug: params[:crop_slug])
render json: Planting.where(crop: @crop)
.where.not(field.to_sym => nil)
.where.not(field.to_sym => '')
.group(field.to_sym).count(:id)
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
39 changes: 30 additions & 9 deletions spec/controllers/charts/crops_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,42 @@
let(:crop) { create(:crop) }

describe 'sunniness' do
before { get :sunniness, params: { crop_slug: crop.to_param } }

it { expect(response).to be_successful }
it "returns a successful response" do
get :sunniness, params: { crop_slug: crop.to_param }
expect(response).to be_successful
end

it "caches the result" do
cache_key = "#{crop.cache_key_with_version}/sunniness"
expect(Rails.cache).to receive(:fetch).with(cache_key, expires_in: 1.day).and_call_original
get :sunniness, params: { crop_slug: crop.to_param }
end
end

describe 'planted_from' do
before { get :planted_from, params: { crop_slug: crop.to_param } }

it { expect(response).to be_successful }
it "returns a successful response" do
get :planted_from, params: { crop_slug: crop.to_param }
expect(response).to be_successful
end

it "caches the result" do
cache_key = "#{crop.cache_key_with_version}/planted_from"
expect(Rails.cache).to receive(:fetch).with(cache_key, expires_in: 1.day).and_call_original
get :planted_from, params: { crop_slug: crop.to_param }
end
end

describe 'harvested_for' do
before { get :harvested_for, params: { crop_slug: crop.to_param } }

it { expect(response).to be_successful }
it "returns a successful response" do
get :harvested_for, params: { crop_slug: crop.to_param }
expect(response).to be_successful
end

it "caches the result" do
cache_key = "#{crop.cache_key_with_version}/harvested_for"
expect(Rails.cache).to receive(:fetch).with(cache_key, expires_in: 1.day).and_call_original
get :harvested_for, params: { crop_slug: crop.to_param }
end
end
end
end
Loading