From 2faada7f14ac35aaf24c6d30cd7c1b976506327e Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Fri, 10 Jul 2015 17:37:28 +0100 Subject: [PATCH 1/5] Find interesting crops using DB queries --- app/models/crop.rb | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/app/models/crop.rb b/app/models/crop.rb index 2773bbe38..08d4f127f 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -29,6 +29,12 @@ class Crop < ActiveRecord::Base scope :pending_approval, -> { where(:approval_status => "pending") } scope :approved, -> { where(:approval_status => "approved") } scope :rejected, -> { where(:approval_status => "rejected") } + # Crops with enough plantings and photos + # ActiveRecord wizardry copied from + # http://stackoverflow.com/questions/13226913/how-do-i-select-all-records-with-more-than-n-child-records + scope :has_plantings, ->(min_plantings) { select("crops.*").joins(:plantings).group("crops.id").having("count(crops.id) >= ?", min_plantings) } + scope :has_photos, ->(min_photos) { select("crops.*").joins(:photos).group("crops.id").having("count(crops.id) >= ?", min_photos) } + scope :interesting, -> { has_plantings(3).has_photos(3).randomized.limit(12) } ## Wikipedia urls are only necessary when approving a crop validates :en_wikipedia_url, @@ -174,14 +180,6 @@ class Crop < ActiveRecord::Base return popular_plant_parts end - def interesting? - min_plantings = 3 # needs this many plantings to be interesting - min_photos = 3 # needs this many photos to be interesting - return false unless photos.size >= min_photos - return false unless plantings_count >= min_plantings - return true - end - def pending? approval_status == "pending" end @@ -202,19 +200,6 @@ class Crop < ActiveRecord::Base [ "already in database", "not edible", "not enough information", "other" ] end - # Crop.interesting - # returns a list of interesting crops, for use on the homepage etc - def Crop.interesting - howmany = 12 # max number to find - interesting_crops = Array.new - Crop.randomized.each do |c| - break if interesting_crops.size == howmany - next unless c.interesting? - interesting_crops.push(c) - end - return interesting_crops - end - # Crop.create_from_csv(row) # used by db/seeds.rb and rake growstuff:import_crops # CSV fields: From 07c976b1fc2637e3179c415742700f9d4e66ccc1 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Fri, 10 Jul 2015 17:54:53 +0100 Subject: [PATCH 2/5] Filter plantings-with-photos in the DB, not Ruby. --- app/models/planting.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/models/planting.rb b/app/models/planting.rb index f06ccf9ac..70d65e4d2 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -116,11 +116,13 @@ class Planting < ActiveRecord::Base interesting_plantings = Array.new seen_owners = Hash.new(false) # keep track of which owners we've seen already - Planting.all.each do |p| + if require_photo then + candidates = Planting.joins(:photos).uniq + else + candidates = Planting + end + candidates.each do |p| break if interesting_plantings.size == howmany # got enough yet? - if require_photo - next unless p.photos.present? # skip those without photos, if required - end 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) From 492bdd915f249790c3d97d22f49ba77a28e335e4 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Fri, 10 Jul 2015 18:51:55 +0100 Subject: [PATCH 3/5] Move the rest of Planting.interesting into the DB We could maybe make it a scope at this point... --- app/models/planting.rb | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/app/models/planting.rb b/app/models/planting.rb index 70d65e4d2..e2ce8ee4c 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -113,21 +113,15 @@ class Planting < ActiveRecord::Base # we can't do this via a scope (as far as we know) so sadly we have to # do it this way. def Planting.interesting(howmany=12, require_photo=true) - interesting_plantings = Array.new - seen_owners = Hash.new(false) # keep track of which owners we've seen already - if require_photo then candidates = Planting.joins(:photos).uniq else candidates = Planting end - candidates.each do |p| - break if interesting_plantings.size == howmany # got enough yet? - 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 + # Find the most recent acceptable planting for each member + most_recent_ids = candidates.select("max(plantings.id)") + .unscope(:order) + .group("plantings.owner_id") + return candidates.where(id: most_recent_ids).limit(howmany) end end From 9e7957709d1b239954747eb2163ce0d445c9d95d Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Fri, 10 Jul 2015 19:03:18 +0100 Subject: [PATCH 4/5] Remove BUNDLED BY line from Gemfile.lock This keeps getting removed by some automatic process, and it's cluttering up my `git diff` output. --- Gemfile.lock | 3 --- 1 file changed, 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 7e3b4aa92..fef7b8258 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -458,6 +458,3 @@ DEPENDENCIES unicorn webrat will_paginate (~> 3.0) - -BUNDLED WITH - 1.10.3 From fc1dc0e4c37b74258a64d95fb9d286f21bfb757c Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Fri, 10 Jul 2015 19:13:32 +0100 Subject: [PATCH 5/5] Remove unused Planting#interesting? method. --- app/models/planting.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/models/planting.rb b/app/models/planting.rb index e2ce8ee4c..c9ba22398 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -90,10 +90,6 @@ class Planting < ActiveRecord::Base return photos.first end - def interesting? - return photos.present? - end - def calculate_days_before_maturity(planting, crop) p_crop = Planting.where(:crop_id => crop).where.not(:id => planting) differences = p_crop.collect do |p|