mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-04-05 07:35:32 -04:00
Moved gardens chart to dedicated controller
This commit is contained in:
16
app/controllers/charts/gardens_controller.rb
Normal file
16
app/controllers/charts/gardens_controller.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
module Charts
|
||||
class GardensController < ApplicationController
|
||||
respond_to :json
|
||||
def timeline
|
||||
@data = []
|
||||
@garden = Garden.find(params[:garden_id])
|
||||
@garden.plantings.where.not(planted_at: nil)
|
||||
.order(finished_at: :desc).each do |p|
|
||||
# use finished_at if we have it, otherwise use predictions
|
||||
finish = p.finished_at.presence || p.finish_predicted_at
|
||||
@data << [p.crop.name, p.planted_at, finish] if finish.present?
|
||||
end
|
||||
render json: @data
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,5 @@
|
||||
class GardensController < ApplicationController
|
||||
before_action :authenticate_member!, except: %i(index show timeline)
|
||||
before_action :authenticate_member!, except: %i(index show)
|
||||
after_action :expire_homepage, only: %i(create delete)
|
||||
load_and_authorize_resource
|
||||
respond_to :html, :json
|
||||
@@ -58,18 +58,6 @@ class GardensController < ApplicationController
|
||||
redirect_to(gardens_by_owner_path(owner: @garden.owner))
|
||||
end
|
||||
|
||||
def timeline
|
||||
@data = []
|
||||
@garden = Garden.find(params[:garden_id])
|
||||
@garden.plantings.where.not(planted_at: nil)
|
||||
.order(finished_at: :desc).each do |p|
|
||||
# use finished_at if we have it, otherwise use predictions
|
||||
finish = p.finished_at.presence || p.finish_predicted_at
|
||||
@data << [p.crop.name, p.planted_at, finish] if finish.present?
|
||||
end
|
||||
render json: @data
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def garden_params
|
||||
|
||||
@@ -28,7 +28,7 @@ Growstuff::Application.routes.draw do
|
||||
get '/plantings/crop/:crop' => 'plantings#index', as: 'plantings_by_crop'
|
||||
|
||||
resources :gardens do
|
||||
get 'timeline' => 'gardens#timeline'
|
||||
get 'timeline' => 'charts/gardens#timeline'
|
||||
end
|
||||
get '/gardens/owner/:owner' => 'gardens#index', as: 'gardens_by_owner'
|
||||
|
||||
|
||||
19
spec/controllers/charts/crops_controller_spec
Normal file
19
spec/controllers/charts/crops_controller_spec
Normal file
@@ -0,0 +1,19 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Charts::CropsController do
|
||||
describe 'GET charts' do
|
||||
let(:crop) { FactoryBot.create :crop }
|
||||
describe 'sunniness' do
|
||||
before { get :sunniness, crop_id: crop.to_param }
|
||||
it { expect(response).to be_success }
|
||||
end
|
||||
describe 'planted_from' do
|
||||
before { get :planted_from, crop_id: crop.to_param }
|
||||
it { expect(response).to be_success }
|
||||
end
|
||||
describe 'harvested_for' do
|
||||
before { get :harvested_for, crop_id: crop.to_param }
|
||||
it { expect(response).to be_success }
|
||||
end
|
||||
end
|
||||
end
|
||||
23
spec/controllers/charts/gardens_controller_spec.rb
Normal file
23
spec/controllers/charts/gardens_controller_spec.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Charts::GardensController do
|
||||
include Devise::Test::ControllerHelpers
|
||||
let(:valid_params) { { name: 'My second Garden' } }
|
||||
|
||||
let(:garden) { FactoryBot.create :garden }
|
||||
context "when not signed in" do
|
||||
describe 'GET timeline' do
|
||||
before { get :timeline, garden_id: garden.to_param }
|
||||
it { expect(response).to be_success }
|
||||
end
|
||||
end
|
||||
context "when signed in" do
|
||||
before(:each) { sign_in member }
|
||||
|
||||
let!(:member) { FactoryBot.create(:member) }
|
||||
describe 'GET timeline' do
|
||||
before { get :timeline, garden_id: garden.to_param }
|
||||
it { expect(response).to be_success }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -46,20 +46,4 @@ describe CropsController do
|
||||
it { expect(response.content_type).to eq("application/rss+xml") }
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET charts' do
|
||||
let(:crop) { FactoryBot.create :crop }
|
||||
describe 'sunniness' do
|
||||
before { get :sunniness, crop_id: crop.to_param }
|
||||
it { expect(response).to be_success }
|
||||
end
|
||||
describe 'planted_from' do
|
||||
before { get :planted_from, crop_id: crop.to_param }
|
||||
it { expect(response).to be_success }
|
||||
end
|
||||
describe 'harvested_for' do
|
||||
before { get :harvested_for, crop_id: crop.to_param }
|
||||
it { expect(response).to be_success }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,10 +14,6 @@ RSpec.describe GardensController, type: :controller do
|
||||
before { put :create, garden: valid_params }
|
||||
it { expect(response).to redirect_to(new_member_session_path) }
|
||||
end
|
||||
describe 'GET timeline' do
|
||||
before { get :timeline, garden_id: garden.to_param }
|
||||
it { expect(response).to be_success }
|
||||
end
|
||||
describe 'changing existing records' do
|
||||
before do
|
||||
allow(Garden).to receive(:find).and_return(:garden)
|
||||
@@ -46,11 +42,6 @@ RSpec.describe GardensController, type: :controller do
|
||||
|
||||
let!(:member) { FactoryBot.create(:member) }
|
||||
|
||||
describe 'GET timeline' do
|
||||
before { get :timeline, garden_id: garden.to_param }
|
||||
it { expect(response).to be_success }
|
||||
end
|
||||
|
||||
describe "for another member's garden" do
|
||||
let(:not_my_garden) { double('garden') }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user