mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-02-06 07:31:04 -05:00
Cache interesting seeds fragment for homepage.
(refresh cache if any of them are deleted)
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
class SeedsController < ApplicationController
|
||||
load_and_authorize_resource
|
||||
|
||||
cache_sweeper :seed_sweeper
|
||||
|
||||
# GET /seeds
|
||||
# GET /seeds.json
|
||||
def index
|
||||
|
||||
@@ -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
|
||||
|
||||
12
app/models/seed_sweeper.rb
Normal file
12
app/models/seed_sweeper.rb
Normal file
@@ -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
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user