From b2ab29397eaaf5f0a9d3095a503fd2fd00185ca8 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 19 Aug 2013 17:58:28 +1000 Subject: [PATCH] Cache interesting seeds fragment for homepage. (refresh cache if any of them are deleted) --- app/controllers/seeds_controller.rb | 3 +++ app/models/seed.rb | 19 +++++++++----- app/models/seed_sweeper.rb | 12 +++++++++ app/views/home/_seeds.html.haml | 39 +++++++++++++++-------------- 4 files changed, 48 insertions(+), 25 deletions(-) create mode 100644 app/models/seed_sweeper.rb diff --git a/app/controllers/seeds_controller.rb b/app/controllers/seeds_controller.rb index 0d3fe9ada..a5b6cd526 100644 --- a/app/controllers/seeds_controller.rb +++ b/app/controllers/seeds_controller.rb @@ -1,5 +1,8 @@ class SeedsController < ApplicationController load_and_authorize_resource + + cache_sweeper :seed_sweeper + # GET /seeds # GET /seeds.json def index diff --git a/app/models/seed.rb b/app/models/seed.rb index d98fb567b..e6641e79d 100644 --- a/app/models/seed.rb +++ b/app/models/seed.rb @@ -30,21 +30,28 @@ class Seed < ActiveRecord::Base end end + def interesting? + # assuming we're passed something that's already known to be tradable + # eg. from Seed.tradable scope + return false if s.owner.location.blank? # don't want unspecified locations + return true + end + # Seed.interesting # returns a list of interesting seeds, for use on the homepage etc def Seed.interesting howmany = 12 # max number to find + interesting_seeds = Array.new - return Rails.cache.fetch('interesting_seeds', :expires_in => 6.hours) do - interesting_seeds = Array.new - Seed.tradable.each do |s| - break if interesting_seeds.length == howmany - next if s.owner.location.blank? # don't want unspecified locations + Seed.tradable.each do |s| + break if interesting_seeds.length == howmany + if s.interesting? interesting_seeds.push(s) end - interesting_seeds end + return interesting_seeds + end def seed_slug diff --git a/app/models/seed_sweeper.rb b/app/models/seed_sweeper.rb new file mode 100644 index 000000000..a733b05cb --- /dev/null +++ b/app/models/seed_sweeper.rb @@ -0,0 +1,12 @@ +class SeedSweeper < ActionController::Caching::Sweeper + observe Seed + + def after_destroy(seed) + if seed.tradable? && seed.interesting? + Rails.cache.delete('interesting_seeds') + end + end + +end + + diff --git a/app/views/home/_seeds.html.haml b/app/views/home/_seeds.html.haml index 1c3014a0f..2e4af2552 100644 --- a/app/views/home/_seeds.html.haml +++ b/app/views/home/_seeds.html.haml @@ -1,25 +1,26 @@ %h2 Seeds available to trade -- if @seeds.length > 0 +- cache "interesting_seeds", :expires_in => 3.hours do + - if @seeds.length > 0 - %table.table.table-striped - %tr - %th Owner - %th Crop - %th.hidden-phone.hidden-tablet Description - %th Will trade to - %th From location - %th - - - @seeds.each do |seed| + %table.table.table-striped %tr - %td= link_to seed.owner.login_name, seed.owner - %td= link_to seed.crop.system_name, seed.crop - %td.hidden-phone.hidden-tablet= truncate(seed.description, :length => 40, :separator => ' ') - %td= seed.tradable? ? seed.tradable_to : '' - %td - - if seed.tradable? - = seed.owner.location.blank? ? "unspecified" : truncate(seed.owner.location, :length => 25, :separator => ', ') - %td= link_to 'Details', seed, :class => 'btn btn-mini' + %th Owner + %th Crop + %th.hidden-phone.hidden-tablet Description + %th Will trade to + %th From location + %th + + - @seeds.each do |seed| + %tr + %td= link_to seed.owner.login_name, seed.owner + %td= link_to seed.crop.system_name, seed.crop + %td.hidden-phone.hidden-tablet= truncate(seed.description, :length => 40, :separator => ' ') + %td= seed.tradable? ? seed.tradable_to : '' + %td + - if seed.tradable? + = seed.owner.location.blank? ? "unspecified" : truncate(seed.owner.location, :length => 25, :separator => ', ') + %td= link_to 'Details', seed, :class => 'btn btn-mini' %p= link_to "View all seeds", seeds_path, :class => 'btn btn-primary'