mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-24 08:52:14 -04:00
moved model access from controller to views to help caching
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user