From 68cdb2b50c644c82f7af95f7553c0e9a111e0175 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 19 Aug 2013 20:35:00 +1000 Subject: [PATCH] moved model access from controller to views to help caching --- app/controllers/home_controller.rb | 14 ++++-------- app/views/home/_crops.html.haml | 6 +++--- app/views/home/_members.html.haml | 7 +++--- app/views/home/_seeds.html.haml | 6 ++++-- app/views/home/_stats.html.haml | 8 +++---- spec/controllers/home_controller_spec.rb | 27 ------------------------ spec/views/home/_crops.html.haml_spec.rb | 12 +++++++---- spec/views/home/_seeds.html.haml_spec.rb | 4 ++-- 8 files changed, 29 insertions(+), 55 deletions(-) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index efe130a59..69e9a95a2 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -2,17 +2,11 @@ class HomeController < ApplicationController skip_authorize_resource def index - # for the stats partial - @member_count = Member.confirmed.count - @crop_count = Crop.count - @planting_count = Planting.count - @garden_count = Garden.count - @crops = Crop.interesting.first(6) - @recent_crops = Crop.recent.limit(12) - @plantings = Planting.interesting.first(4) - @seeds = Seed.interesting.first(6) - @members = Member.interesting.first(6) + # we were previously generating a lot of instance variables like + # @members_count and @interesting_crops in here, but now we call + # the relevant class methods directly in the view, so that fragment + # caching will be effective. respond_to do |format| format.html # index.html.haml diff --git a/app/views/home/_crops.html.haml b/app/views/home/_crops.html.haml index 4d9bdce8b..062abfc49 100644 --- a/app/views/home/_crops.html.haml +++ b/app/views/home/_crops.html.haml @@ -2,14 +2,14 @@ .span6 - cache "interesting_crops", :expires_in => 1.day do %h2 Some of our crops - - @crops.each do |c| + - Crop.interesting.each do |c| .span3{:style => 'margin:0px; padding: 3px'} = render :partial => 'crops/image_with_popover', :locals => { :crop => c } .span6 - cache "interesting_plantings" do %h2 Recently planted - = render :partial => 'plantings/list', :locals => { :plantings => @plantings } + = render :partial => 'plantings/list', :locals => { :plantings => Planting.interesting.first(4) } .row-fluid .span12 @@ -17,7 +17,7 @@ %p %strong Recently added crops: - != @recent_crops.map {|c| link_to(c, c) }.join(", ") + != Crop.recent.limit(12).map {|c| link_to(c, c) }.join(", ") %p =link_to "View all crops", crops_path, :class => 'btn btn-primary' diff --git a/app/views/home/_members.html.haml b/app/views/home/_members.html.haml index 6cbfa9edd..c1c806bf7 100644 --- a/app/views/home/_members.html.haml +++ b/app/views/home/_members.html.haml @@ -1,12 +1,13 @@ -- if @members.present? +- members = Member.interesting.first(6) +- if members.present? %h2 Some of our members .visible-desktop.visible-tablet .row-fluid - - @members.each do |m| + - members.each do |m| .span6.homepage-members = render :partial => "members/thumbnail", :locals => { :member => m } .visible-phone - - @members.each do |m| + - members.each do |m| = render :partial => "members/thumbnail", :locals => { :member => m } diff --git a/app/views/home/_seeds.html.haml b/app/views/home/_seeds.html.haml index 2e4af2552..16715af48 100644 --- a/app/views/home/_seeds.html.haml +++ b/app/views/home/_seeds.html.haml @@ -1,7 +1,9 @@ %h2 Seeds available to trade +- seeds = Seed.interesting.first(6) + - cache "interesting_seeds", :expires_in => 3.hours do - - if @seeds.length > 0 + - if seeds.length > 0 %table.table.table-striped %tr @@ -12,7 +14,7 @@ %th From location %th - - @seeds.each do |seed| + - seeds.each do |seed| %tr %td= link_to seed.owner.login_name, seed.owner %td= link_to seed.crop.system_name, seed.crop diff --git a/app/views/home/_stats.html.haml b/app/views/home/_stats.html.haml index 8b1cbb7cb..93c16327d 100644 --- a/app/views/home/_stats.html.haml +++ b/app/views/home/_stats.html.haml @@ -1,10 +1,10 @@ - cache("homepage_stats") do %p.stats So far, - = link_to "#{@member_count.to_i} members", members_path + = link_to "#{Member.confirmed.count.to_i} members", members_path have planted - = link_to "#{@crop_count.to_i} crops", crops_path - = link_to "#{@planting_count.to_i} times", plantings_path + = link_to "#{Crop.count.to_i} crops", crops_path + = link_to "#{Planting.count.to_i} times", plantings_path in = succeed "." do - = link_to "#{@garden_count.to_i} gardens", gardens_path + = link_to "#{Garden.count.to_i} gardens", gardens_path diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 4daf4f2df..bfbfc6adf 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -3,32 +3,5 @@ require 'spec_helper' describe HomeController do describe "GET index" do - it "assigns counts" do - @planting = FactoryGirl.create(:planting) - get :index, {} - assigns(:garden_count).should == 2 # auto-created for member and planting - assigns(:planting_count).should == 1 - assigns(:crop_count).should == 1 - assigns(:member_count).should == 1 - end - - it "assigns plantings" do - @member = FactoryGirl.create(:london_member) - @planting = FactoryGirl.create(:planting, :garden => @member.gardens.first) - @planting.photos << FactoryGirl.create(:photo) - @planting.save - get :index, {} - assigns(:plantings).should eq [@planting] - end - - it 'assigns interesting members' do - @member = FactoryGirl.create(:london_member) - (1..3).each do - FactoryGirl.create(:planting, :garden => @member.gardens.first) - end - get :index, {} - assigns(:members).should eq [@member] - end - end end diff --git a/spec/views/home/_crops.html.haml_spec.rb b/spec/views/home/_crops.html.haml_spec.rb index 7f30b1e2c..43ad03327 100644 --- a/spec/views/home/_crops.html.haml_spec.rb +++ b/spec/views/home/_crops.html.haml_spec.rb @@ -2,11 +2,15 @@ require 'spec_helper' describe 'home/_crops.html.haml', :type => "view" do before(:each) do + # we need to set up an "interesting" crop @crop = FactoryGirl.create(:crop) - assign(:crops, [@crop]) - assign(:recent_crops, [@crop]) - @planting = FactoryGirl.create(:planting) - assign(:plantings, [@planting]) + (1..3).each do + @planting = FactoryGirl.create(:planting, :crop => @crop) + end + @photo = FactoryGirl.create(:photo) + (1..3).each do + @crop.plantings.first.photos << @photo + end render end diff --git a/spec/views/home/_seeds.html.haml_spec.rb b/spec/views/home/_seeds.html.haml_spec.rb index f2f81c60d..bc5f6d2fa 100644 --- a/spec/views/home/_seeds.html.haml_spec.rb +++ b/spec/views/home/_seeds.html.haml_spec.rb @@ -2,8 +2,8 @@ require 'spec_helper' describe 'home/_seeds.html.haml', :type => "view" do before(:each) do - @seed = FactoryGirl.create(:tradable_seed) - assign(:seeds, [@seed]) + @owner = FactoryGirl.create(:london_member) + @seed = FactoryGirl.create(:tradable_seed, :owner => @owner) render end