From 675ac5a03f2bef48173f85a559de0e30efc24423 Mon Sep 17 00:00:00 2001 From: Mackenzie Morgan Date: Mon, 26 Jan 2015 22:26:21 -0500 Subject: [PATCH 1/8] add before_save for harvest weight and index for harvest photos --- app/models/harvest.rb | 11 ++++++++++- .../20140905001730_add_harvests_photos_table.rb | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/models/harvest.rb b/app/models/harvest.rb index 48e9eac19..a90a031d6 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -4,7 +4,7 @@ class Harvest < ActiveRecord::Base friendly_id :harvest_slug, use: :slugged attr_accessible :crop_id, :harvested_at, :description, :owner_id, - :quantity, :unit, :weight_quantity, :weight_unit, :plant_part_id, :slug + :quantity, :unit, :weight_quantity, :weight_unit, :plant_part_id, :slug, :si_weight belongs_to :crop belongs_to :owner, :class_name => 'Member' @@ -62,6 +62,15 @@ class Harvest < ActiveRecord::Base after_validation :cleanup_quantities + before_save { + if :weight_unit == "kg" + :si_weight = :weight_quantity + elsif :weight_unit == "lb" + :si_weight = :weight_quantity * 0.45 + elsif :weight_unit == "oz" + :si_weight = 0.028 + } + def cleanup_quantities if quantity == 0 self.quantity = nil diff --git a/db/migrate/20140905001730_add_harvests_photos_table.rb b/db/migrate/20140905001730_add_harvests_photos_table.rb index edacd061b..299d6653e 100644 --- a/db/migrate/20140905001730_add_harvests_photos_table.rb +++ b/db/migrate/20140905001730_add_harvests_photos_table.rb @@ -5,4 +5,6 @@ class AddHarvestsPhotosTable < ActiveRecord::Migration t.integer :harvest_id end end + + add_index(:harvests_photos, [:harvest_id, :photo_id]) end From 0bdab0d9fcd81a7d3e6732e8f801414ad9d15539 Mon Sep 17 00:00:00 2001 From: Mackenzie Morgan Date: Wed, 28 Jan 2015 23:00:16 -0500 Subject: [PATCH 2/8] basics of harvest standardized weights --- app/controllers/harvests_controller.rb | 2 +- .../20150129034206_add_si_weight_to_harvest.rb | 5 +++++ lib/tasks/growstuff.rake | 15 +++++++++++++++ script/deploy-tasks.sh | 3 +++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20150129034206_add_si_weight_to_harvest.rb diff --git a/app/controllers/harvests_controller.rb b/app/controllers/harvests_controller.rb index 3bc151d8c..23cf3cc0b 100644 --- a/app/controllers/harvests_controller.rb +++ b/app/controllers/harvests_controller.rb @@ -107,6 +107,6 @@ class HarvestsController < ApplicationController def harvest_params params.require(:harvest).permit(:crop_id, :harvested_at, :description, :owner_id, - :quantity, :unit, :weight_quantity, :weight_unit, :plant_part_id, :slug) + :quantity, :unit, :weight_quantity, :weight_unit, :plant_part_id, :slug, :si_weight) end end diff --git a/db/migrate/20150129034206_add_si_weight_to_harvest.rb b/db/migrate/20150129034206_add_si_weight_to_harvest.rb new file mode 100644 index 000000000..b1e532d8d --- /dev/null +++ b/db/migrate/20150129034206_add_si_weight_to_harvest.rb @@ -0,0 +1,5 @@ +class AddSiWeightToHarvest < ActiveRecord::Migration + def change + add_column :harvests, :si_weight, :float + end +end diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index 656961fd3..ab04b58c2 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -314,6 +314,21 @@ namespace :growstuff do end end end + + desc "January 2015: fill in si_weight column" + task :populate_si_weight => :environment do + Harvest.find_each do |h| + if h.weight_unit == "lb" + h.si_weight = h.weight_quantity * 0.453592 + elsif h.weight_unit == "oz" + h.si_weight = h.weight_quantity * 0.0283495 + else # kg is all that's left, and that's SI + h.si_weight = h.weight_quantity + end + h.save + end + end + end # end oneoff section end diff --git a/script/deploy-tasks.sh b/script/deploy-tasks.sh index 3d427ed85..c2eaf559f 100755 --- a/script/deploy-tasks.sh +++ b/script/deploy-tasks.sh @@ -18,3 +18,6 @@ rake growstuff:import_crops file=db/seeds/crops-15-squashes.csv echo "2014-12-01 - load alternate names for crops" rake growstuff:oneoff:add_alternate_names + +echo "2015-01-28 - populate the harvest si_weight field" +rake growstuff:populate_si_weight From 2567e7cd747441ee47619800264f1d1115e71770 Mon Sep 17 00:00:00 2001 From: Mackenzie Morgan Date: Thu, 29 Jan 2015 00:27:53 -0500 Subject: [PATCH 3/8] adding tests (not passing, need to figure out saving mid-test), fixing before_save, adding CSV column, and adding task to set the si_weight throughout the db --- app/models/harvest.rb | 19 +++++++++++-------- app/views/harvests/index.csv.shaper | 2 ++ db/schema.rb | 3 ++- lib/tasks/growstuff.rake | 4 ++-- spec/models/harvest_spec.rb | 20 ++++++++++++++++++++ 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/app/models/harvest.rb b/app/models/harvest.rb index 49cd44cae..b949b6f68 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -59,14 +59,17 @@ class Harvest < ActiveRecord::Base after_validation :cleanup_quantities - before_save { - if :weight_unit == "kg" - :si_weight = :weight_quantity - elsif :weight_unit == "lb" - :si_weight = :weight_quantity * 0.45 - elsif :weight_unit == "oz" - :si_weight = 0.028 - } + before_save :set_si_weight + + def set_si_weight + if self.weight_unit == "kg" + self.si_weight = self.weight_quantity + elsif self.weight_unit == "lb" + self.si_weight = self.weight_quantity * 0.45 + elsif self.weight_unit == "oz" + self.si_weight = self.weight_quantity * 0.028 + end + end def cleanup_quantities if quantity == 0 diff --git a/app/views/harvests/index.csv.shaper b/app/views/harvests/index.csv.shaper index 4560dde80..712c827dd 100644 --- a/app/views/harvests/index.csv.shaper +++ b/app/views/harvests/index.csv.shaper @@ -10,6 +10,7 @@ csv.headers :id, :unit, :weight_quantity, :weight_unit, + :si_weight, :date_harvested, :description, :date_added, @@ -32,6 +33,7 @@ csv.headers :id, csv.cell :plant_part_name, h.plant_part ? h.plant_part.to_s : '' csv.cells :quantity, :unit, :weight_quantity, :weight_unit + csv.cell :si_weight, :si_weight csv.cell :date_harvested, h.created_at.to_s(:db) diff --git a/db/schema.rb b/db/schema.rb index 9e593a422..bf80810c7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150127043022) do +ActiveRecord::Schema.define(version: 20150129034206) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -139,6 +139,7 @@ ActiveRecord::Schema.define(version: 20150127043022) do t.decimal "weight_quantity" t.string "weight_unit" t.integer "plant_part_id" + t.float "si_weight" end create_table "harvests_photos", id: false, force: true do |t| diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index ab04b58c2..7ffaba0d3 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -319,9 +319,9 @@ namespace :growstuff do task :populate_si_weight => :environment do Harvest.find_each do |h| if h.weight_unit == "lb" - h.si_weight = h.weight_quantity * 0.453592 + h.si_weight = h.weight_quantity * 0.45 elsif h.weight_unit == "oz" - h.si_weight = h.weight_quantity * 0.0283495 + h.si_weight = h.weight_quantity * 0.028 else # kg is all that's left, and that's SI h.si_weight = h.weight_quantity end diff --git a/spec/models/harvest_spec.rb b/spec/models/harvest_spec.rb index 504c8c414..f90a18a8a 100644 --- a/spec/models/harvest_spec.rb +++ b/spec/models/harvest_spec.rb @@ -118,6 +118,26 @@ describe Harvest do end end + context "standardized weights" do + it 'converts from pounds' do + @harvest = FactoryGirl.build(:harvest, :weight_quantity => 2, :weight_unit => "lb") + @harvest.should be_valid + @harvest.reload.si_weight.should eq 0.9 + end + + it 'converts from ounces' do + @harvest = FactoryGirl.build(:harvest, :weight_quantity => 16, :weight_unit => "oz") + @harvest.should be_valid + @harvest.reload.si_weight.should eq 0.9 + end + + it 'leaves kg alone' do + @harvest = FactoryGirl.build(:harvest, :weight_quantity => 2, :weight_unit => "kg") + @harvest.should be_valid + @harvest.reload.si_weight.should eq 2 + end + end + context 'ordering' do it 'lists most recent harvests first' do @h1 = FactoryGirl.create(:harvest, :created_at => 1.day.ago) From 3f393b093739556f8cbd539764c5cc75ad1b9bfe Mon Sep 17 00:00:00 2001 From: Mackenzie Morgan Date: Fri, 30 Jan 2015 23:17:42 -0500 Subject: [PATCH 4/8] moving si_weight population to one-off --- script/deploy-tasks.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/deploy-tasks.sh b/script/deploy-tasks.sh index c2eaf559f..e5f95aed2 100755 --- a/script/deploy-tasks.sh +++ b/script/deploy-tasks.sh @@ -20,4 +20,4 @@ echo "2014-12-01 - load alternate names for crops" rake growstuff:oneoff:add_alternate_names echo "2015-01-28 - populate the harvest si_weight field" -rake growstuff:populate_si_weight +rake growstuff:oneoff:populate_si_weight From d436fd86f8694abb1311c146a530d1770a20bb55 Mon Sep 17 00:00:00 2001 From: Mackenzie Morgan Date: Sat, 31 Jan 2015 00:22:35 -0500 Subject: [PATCH 5/8] use ruby-units to handle conversion. note: the deploy task isn't working and i don't know why --- Gemfile | 1 + Gemfile.lock | 2 ++ app/models/harvest.rb | 11 ++++------- lib/tasks/growstuff.rake | 10 +++------- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index fcf7b39d4..87d18f8a1 100644 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,7 @@ gem 'figaro' # for handling config via ENV variables gem 'cancancan', '~> 1.9' # for checking member privileges gem 'gibbon' # for Mailchimp newsletter subscriptions gem 'csv_shaper' # CSV export +gem 'ruby-units' # for unit conversion # vendored activemerchant for testing- needed for bogus paypal # gateway monkeypatch diff --git a/Gemfile.lock b/Gemfile.lock index d54873d81..d867e16f5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -275,6 +275,7 @@ GEM rspec-mocks (~> 3.1.0) rspec-support (~> 3.1.0) rspec-support (3.1.2) + ruby-units (1.4.5) ruby_parser (3.1.3) sexp_processor (~> 4.1) sass (3.2.19) @@ -388,6 +389,7 @@ DEPENDENCIES rake (>= 10.0.0) rspec-activemodel-mocks rspec-rails (~> 3.1.0) + ruby-units sass-rails (~> 4.0.4) therubyracer (~> 0.12) uglifier (~> 2.5.3) diff --git a/app/models/harvest.rb b/app/models/harvest.rb index b949b6f68..ce44f97c2 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -61,14 +61,11 @@ class Harvest < ActiveRecord::Base before_save :set_si_weight + # we're storing the harvest weight in grams in the db too + # to make data manipulation easier def set_si_weight - if self.weight_unit == "kg" - self.si_weight = self.weight_quantity - elsif self.weight_unit == "lb" - self.si_weight = self.weight_quantity * 0.45 - elsif self.weight_unit == "oz" - self.si_weight = self.weight_quantity * 0.028 - end + weight_string = "#{self.weight_quantity} #{self.weight_unit}" + self.si_weight = Unit(weight_string).convert_to("g").to_s("%0.2f").delete(" g").to_f end def cleanup_quantities diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index 7ffaba0d3..2c8ad585c 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -318,13 +318,9 @@ namespace :growstuff do desc "January 2015: fill in si_weight column" task :populate_si_weight => :environment do Harvest.find_each do |h| - if h.weight_unit == "lb" - h.si_weight = h.weight_quantity * 0.45 - elsif h.weight_unit == "oz" - h.si_weight = h.weight_quantity * 0.028 - else # kg is all that's left, and that's SI - h.si_weight = h.weight_quantity - end + weight_string = "#{h.weight_quantity} #{h.weight_unit}" + print weight_string + h.si_weight = Unit(weight_string).convert_to("g").to_s("%0.2f").delete(" g").to_f h.save end end From bb9695b27267d9267aa99255dafdb9e30e29e819 Mon Sep 17 00:00:00 2001 From: Mackenzie Morgan Date: Sat, 31 Jan 2015 00:45:31 -0500 Subject: [PATCH 6/8] add check for nil to set_si_weights and the corresponding deploy script and update test --- app/models/harvest.rb | 6 ++++-- lib/tasks/growstuff.rake | 9 +++++---- spec/models/harvest_spec.rb | 12 ++++++------ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/models/harvest.rb b/app/models/harvest.rb index ce44f97c2..135ed9f3c 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -64,8 +64,10 @@ class Harvest < ActiveRecord::Base # we're storing the harvest weight in grams in the db too # to make data manipulation easier def set_si_weight - weight_string = "#{self.weight_quantity} #{self.weight_unit}" - self.si_weight = Unit(weight_string).convert_to("g").to_s("%0.2f").delete(" g").to_f + if self.weight_unit != nil + weight_string = "#{self.weight_quantity} #{self.weight_unit}" + self.si_weight = Unit(weight_string).convert_to("g").to_s("%0.2f").delete(" g").to_f + end end def cleanup_quantities diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index 2c8ad585c..6bc4258fd 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -318,10 +318,11 @@ namespace :growstuff do desc "January 2015: fill in si_weight column" task :populate_si_weight => :environment do Harvest.find_each do |h| - weight_string = "#{h.weight_quantity} #{h.weight_unit}" - print weight_string - h.si_weight = Unit(weight_string).convert_to("g").to_s("%0.2f").delete(" g").to_f - h.save + if h.weight_unit != nil + weight_string = "#{h.weight_quantity} #{h.weight_unit}" + h.si_weight = Unit(weight_string).convert_to("g").to_s("%0.2f").delete(" g").to_f + h.save + end end end diff --git a/spec/models/harvest_spec.rb b/spec/models/harvest_spec.rb index f90a18a8a..a7ec2114c 100644 --- a/spec/models/harvest_spec.rb +++ b/spec/models/harvest_spec.rb @@ -120,21 +120,21 @@ describe Harvest do context "standardized weights" do it 'converts from pounds' do - @harvest = FactoryGirl.build(:harvest, :weight_quantity => 2, :weight_unit => "lb") + @harvest = FactoryGirl.create(:harvest, :weight_quantity => 2, :weight_unit => "lb") @harvest.should be_valid - @harvest.reload.si_weight.should eq 0.9 + @harvest.reload.si_weight.should eq 907.18 end it 'converts from ounces' do - @harvest = FactoryGirl.build(:harvest, :weight_quantity => 16, :weight_unit => "oz") + @harvest = FactoryGirl.create(:harvest, :weight_quantity => 16, :weight_unit => "oz") @harvest.should be_valid - @harvest.reload.si_weight.should eq 0.9 + @harvest.reload.si_weight.should eq 453.59 end it 'leaves kg alone' do - @harvest = FactoryGirl.build(:harvest, :weight_quantity => 2, :weight_unit => "kg") + @harvest = FactoryGirl.create(:harvest, :weight_quantity => 2, :weight_unit => "kg") @harvest.should be_valid - @harvest.reload.si_weight.should eq 2 + @harvest.reload.si_weight.should eq 2000 end end From de5b16e384baa7933a9c22643331d44f648d6d46 Mon Sep 17 00:00:00 2001 From: Mackenzie Morgan Date: Sat, 31 Jan 2015 18:35:29 -0500 Subject: [PATCH 7/8] back to kg for si weight, also now adding si_weight to the csv correctly --- app/models/harvest.rb | 4 ++-- app/views/harvests/index.csv.shaper | 3 +-- lib/tasks/growstuff.rake | 2 +- spec/models/harvest_spec.rb | 6 +++--- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/models/harvest.rb b/app/models/harvest.rb index 135ed9f3c..57e542fd2 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -61,12 +61,12 @@ class Harvest < ActiveRecord::Base before_save :set_si_weight - # we're storing the harvest weight in grams in the db too + # we're storing the harvest weight in kilograms in the db too # to make data manipulation easier def set_si_weight if self.weight_unit != nil weight_string = "#{self.weight_quantity} #{self.weight_unit}" - self.si_weight = Unit(weight_string).convert_to("g").to_s("%0.2f").delete(" g").to_f + self.si_weight = Unit(weight_string).convert_to("kg").to_s("%0.2f").delete(" kg").to_f end end diff --git a/app/views/harvests/index.csv.shaper b/app/views/harvests/index.csv.shaper index 712c827dd..b75b7a879 100644 --- a/app/views/harvests/index.csv.shaper +++ b/app/views/harvests/index.csv.shaper @@ -32,8 +32,7 @@ csv.headers :id, csv.cell :plant_part_id, h.plant_part ? h.plant_part.id : '' csv.cell :plant_part_name, h.plant_part ? h.plant_part.to_s : '' - csv.cells :quantity, :unit, :weight_quantity, :weight_unit - csv.cell :si_weight, :si_weight + csv.cells :quantity, :unit, :weight_quantity, :weight_unit, :si_weight csv.cell :date_harvested, h.created_at.to_s(:db) diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index 6bc4258fd..0e77c6fdd 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -320,7 +320,7 @@ namespace :growstuff do Harvest.find_each do |h| if h.weight_unit != nil weight_string = "#{h.weight_quantity} #{h.weight_unit}" - h.si_weight = Unit(weight_string).convert_to("g").to_s("%0.2f").delete(" g").to_f + h.si_weight = Unit(weight_string).convert_to("kg").to_s("%0.2f").delete(" kg").to_f h.save end end diff --git a/spec/models/harvest_spec.rb b/spec/models/harvest_spec.rb index a7ec2114c..9cbf6a1d8 100644 --- a/spec/models/harvest_spec.rb +++ b/spec/models/harvest_spec.rb @@ -122,19 +122,19 @@ describe Harvest do it 'converts from pounds' do @harvest = FactoryGirl.create(:harvest, :weight_quantity => 2, :weight_unit => "lb") @harvest.should be_valid - @harvest.reload.si_weight.should eq 907.18 + @harvest.reload.si_weight.should eq 0.91 end it 'converts from ounces' do @harvest = FactoryGirl.create(:harvest, :weight_quantity => 16, :weight_unit => "oz") @harvest.should be_valid - @harvest.reload.si_weight.should eq 453.59 + @harvest.reload.si_weight.should eq 0.45 end it 'leaves kg alone' do @harvest = FactoryGirl.create(:harvest, :weight_quantity => 2, :weight_unit => "kg") @harvest.should be_valid - @harvest.reload.si_weight.should eq 2000 + @harvest.reload.si_weight.should eq 2.0 end end From 2c94f61843b223c881f5867b2579fd850c0e1b97 Mon Sep 17 00:00:00 2001 From: Mackenzie Morgan Date: Sun, 1 Feb 2015 21:59:33 -0500 Subject: [PATCH 8/8] use %0.3f instead of %0.2f so we get to gram-granularity, and don't duplicate code --- app/models/harvest.rb | 2 +- lib/tasks/growstuff.rake | 6 +----- spec/models/harvest_spec.rb | 4 ++-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/app/models/harvest.rb b/app/models/harvest.rb index 57e542fd2..c565b2c91 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -66,7 +66,7 @@ class Harvest < ActiveRecord::Base def set_si_weight if self.weight_unit != nil weight_string = "#{self.weight_quantity} #{self.weight_unit}" - self.si_weight = Unit(weight_string).convert_to("kg").to_s("%0.2f").delete(" kg").to_f + self.si_weight = Unit(weight_string).convert_to("kg").to_s("%0.3f").delete(" kg").to_f end end diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index 0e77c6fdd..d347bfaa3 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -318,11 +318,7 @@ namespace :growstuff do desc "January 2015: fill in si_weight column" task :populate_si_weight => :environment do Harvest.find_each do |h| - if h.weight_unit != nil - weight_string = "#{h.weight_quantity} #{h.weight_unit}" - h.si_weight = Unit(weight_string).convert_to("kg").to_s("%0.2f").delete(" kg").to_f - h.save - end + h.set_si_weight end end diff --git a/spec/models/harvest_spec.rb b/spec/models/harvest_spec.rb index 9cbf6a1d8..cd6d5f1ef 100644 --- a/spec/models/harvest_spec.rb +++ b/spec/models/harvest_spec.rb @@ -122,13 +122,13 @@ describe Harvest do it 'converts from pounds' do @harvest = FactoryGirl.create(:harvest, :weight_quantity => 2, :weight_unit => "lb") @harvest.should be_valid - @harvest.reload.si_weight.should eq 0.91 + @harvest.reload.si_weight.should eq 0.907 end it 'converts from ounces' do @harvest = FactoryGirl.create(:harvest, :weight_quantity => 16, :weight_unit => "oz") @harvest.should be_valid - @harvest.reload.si_weight.should eq 0.45 + @harvest.reload.si_weight.should eq 0.454 end it 'leaves kg alone' do