diff --git a/app/models/crop.rb b/app/models/crop.rb index a420ddb41..c8506be8a 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -13,6 +13,7 @@ class Crop < ActiveRecord::Base default_scope order("lower(system_name) asc") scope :recent, reorder("created_at desc") + scope :randomized, reorder('random()') # ok on sqlite and psql, but not on mysql validates :en_wikipedia_url, :format => { @@ -89,19 +90,14 @@ class Crop < ActiveRecord::Base # Crop.interesting # returns a list of interesting crops, for use on the homepage etc def Crop.interesting - return Rails.cache.fetch("interesting_crops", :expires_in => 1.day) do - howmany = 12 # max number to find - interesting_crops = Array.new - # it's inefficient to shuffle this up-front, but if we cache - # the crops on the homepage it won't be run too often, and there's - # not *that* many crops. - Crop.all.each do |c| - break if interesting_crops.length == howmany - next unless c.interesting? - interesting_crops.push(c) - end - interesting_crops + howmany = 12 # max number to find + interesting_crops = Array.new + Crop.randomized.each do |c| + break if interesting_crops.length == howmany + next unless c.interesting? + interesting_crops.push(c) end + return interesting_crops end end diff --git a/app/models/planting.rb b/app/models/planting.rb index ede0890c4..cf9e9aea3 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -6,7 +6,8 @@ class Planting < ActiveRecord::Base :quantity, :sunniness, :planted_from belongs_to :garden - belongs_to :crop + belongs_to :crop, :counter_cache => true + has_one :owner, :through => :garden has_and_belongs_to_many :photos before_destroy {|planting| planting.photos.clear} @@ -19,7 +20,6 @@ class Planting < ActiveRecord::Base :plantings_count, :to => :crop, :prefix => true - delegate :owner, :to => :garden default_scope order("created_at desc") @@ -77,17 +77,17 @@ class Planting < ActiveRecord::Base def Planting.interesting howmany = 12 # max amount to collect - return Rails.cache.fetch("interesting_plantings", :expires_in => 3.hours) do - interesting_plantings = Array.new - seen_owners = Hash.new(false) # keep track of which owners we've seen already - Planting.all.each do |p| - break if interesting_plantings.count == howmany # got enough yet? - next unless p.interesting? # skip those that don't have photos - next if seen_owners[p.owner] # skip if we already have one from this owner - seen_owners[p.owner] = true # we've seen this owner - interesting_plantings.push(p) - end - interesting_plantings + interesting_plantings = Array.new + seen_owners = Hash.new(false) # keep track of which owners we've seen already + + Planting.all.each do |p| + break if interesting_plantings.count == howmany # got enough yet? + next unless p.interesting? # skip those that don't have photos + next if seen_owners[p.owner] # skip if we already have one from this owner + seen_owners[p.owner] = true # we've seen this owner + interesting_plantings.push(p) end + + return interesting_plantings end end diff --git a/app/models/seed.rb b/app/models/seed.rb index e6641e79d..e456d7387 100644 --- a/app/models/seed.rb +++ b/app/models/seed.rb @@ -33,7 +33,7 @@ class Seed < ActiveRecord::Base 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 false if owner.location.blank? # don't want unspecified locations return true end diff --git a/app/views/home/_crops.html.haml b/app/views/home/_crops.html.haml index 1c9a8b08b..4d9bdce8b 100644 --- a/app/views/home/_crops.html.haml +++ b/app/views/home/_crops.html.haml @@ -1,13 +1,15 @@ .row-fluid .span6 - %h2 Some of our crops - - @crops.each do |c| - .span3{:style => 'margin:0px; padding: 3px'} - = render :partial => 'crops/image_with_popover', :locals => { :crop => c } + - cache "interesting_crops", :expires_in => 1.day do + %h2 Some of our crops + - @crops.each do |c| + .span3{:style => 'margin:0px; padding: 3px'} + = render :partial => 'crops/image_with_popover', :locals => { :crop => c } .span6 - %h2 Recently planted - = render :partial => 'plantings/list', :locals => { :plantings => @plantings } + - cache "interesting_plantings" do + %h2 Recently planted + = render :partial => 'plantings/list', :locals => { :plantings => @plantings } .row-fluid .span12 diff --git a/db/migrate/20130819004549_add_planting_count_to_crop.rb b/db/migrate/20130819004549_add_planting_count_to_crop.rb new file mode 100644 index 000000000..0ae01f4bb --- /dev/null +++ b/db/migrate/20130819004549_add_planting_count_to_crop.rb @@ -0,0 +1,5 @@ +class AddPlantingCountToCrop < ActiveRecord::Migration + def change + add_column :crops, :plantings_count, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 8263fb087..770254e64 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130723110702) do +ActiveRecord::Schema.define(:version => 20130819004549) do create_table "account_types", :force => true do |t| t.string "name", :null => false @@ -57,6 +57,7 @@ ActiveRecord::Schema.define(:version => 20130723110702) do t.datetime "updated_at", :null => false t.string "slug" t.integer "parent_id" + t.integer "plantings_count" end add_index "crops", ["slug"], :name => "index_crops_on_slug", :unique => true