mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-18 13:38:24 -04:00
Moved popular plant plarts calculation into sql
This commit is contained in:
@@ -163,11 +163,11 @@ class Crop < ActiveRecord::Base
|
||||
# key: plant part (eg. 'fruit')
|
||||
# value: count of how many times it's been used by harvests
|
||||
def popular_plant_parts
|
||||
popular_plant_parts = Hash.new(0)
|
||||
harvests.each do |h|
|
||||
popular_plant_parts[h.plant_part] += 1 if h.plant_part
|
||||
end
|
||||
popular_plant_parts.sort_by { |_s, freq| freq }.reverse
|
||||
PlantPart.joins(:harvests)
|
||||
.where("crop_id = ?", id)
|
||||
.order("count_harvests_id DESC")
|
||||
.group("plant_parts.id", "plant_parts.name")
|
||||
.count("harvests.id")
|
||||
end
|
||||
|
||||
def interesting?
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
- if crop.harvests.empty?
|
||||
not known.
|
||||
- else
|
||||
- crop.popular_plant_parts.map do |plant_part, frequency|
|
||||
= link_to plant_part.name, plant_part_path(plant_part)
|
||||
- crop.popular_plant_parts.each do |plant_part, frequency|
|
||||
- id, name = plant_part
|
||||
= link_to name, plant_part_path(id: id)
|
||||
(#{frequency})
|
||||
|
||||
@@ -248,9 +248,9 @@ describe Crop do
|
||||
|
||||
it 'counts each plant_part value' do
|
||||
@fruit = FactoryGirl.create(:plant_part)
|
||||
@seed = FactoryGirl.create(:plant_part)
|
||||
@root = FactoryGirl.create(:plant_part)
|
||||
@bulb = FactoryGirl.create(:plant_part)
|
||||
@seed = FactoryGirl.create(:plant_part, name: 'seed')
|
||||
@root = FactoryGirl.create(:plant_part, name: 'root')
|
||||
@bulb = FactoryGirl.create(:plant_part, name: 'bulb')
|
||||
@harvest1 = FactoryGirl.create(:harvest,
|
||||
crop: crop,
|
||||
plant_part: @fruit
|
||||
@@ -267,7 +267,9 @@ describe Crop do
|
||||
crop: crop,
|
||||
plant_part: @root
|
||||
)
|
||||
crop.popular_plant_parts.should == { @fruit => 2, @seed => 1, @root => 1 }
|
||||
crop.popular_plant_parts.should == { [@fruit.id, @fruit.name] => 2,
|
||||
[@seed.id, @seed.name] => 1,
|
||||
[@root.id, @root.name] => 1 }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user