mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-03 21:36:52 -04:00
Break out interesting? into its own method
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
5
db/migrate/20130819004549_add_planting_count_to_crop.rb
Normal file
5
db/migrate/20130819004549_add_planting_count_to_crop.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class AddPlantingCountToCrop < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :crops, :plantings_count, :integer
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user