diff --git a/Gemfile b/Gemfile index 7b485f9df..d15f48067 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 667a1979d..09f8547c7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -292,6 +292,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) @@ -408,6 +409,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/controllers/harvests_controller.rb b/app/controllers/harvests_controller.rb index 5927f3df1..4bc3a5d90 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/app/models/harvest.rb b/app/models/harvest.rb index 4e43c6ee7..c565b2c91 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -59,6 +59,17 @@ class Harvest < ActiveRecord::Base after_validation :cleanup_quantities + before_save :set_si_weight + + # 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("kg").to_s("%0.3f").delete(" kg").to_f + end + end + def cleanup_quantities if quantity == 0 self.quantity = nil diff --git a/app/views/harvests/index.csv.shaper b/app/views/harvests/index.csv.shaper index 4560dde80..b75b7a879 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, @@ -31,7 +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.cells :quantity, :unit, :weight_quantity, :weight_unit, :si_weight csv.cell :date_harvested, h.created_at.to_s(:db) 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/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 db0b928d3..35365ae1e 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -315,6 +315,13 @@ namespace :growstuff do end end + desc "January 2015: fill in si_weight column" + task :populate_si_weight => :environment do + Harvest.find_each do |h| + h.set_si_weight + end + end + desc "January 2015: build Elasticsearch index" task :elasticsearch_create_index => :environment do Crop.__elasticsearch__.create_index! force: true diff --git a/script/deploy-tasks.sh b/script/deploy-tasks.sh index 5684afbbf..e8461294c 100755 --- a/script/deploy-tasks.sh +++ b/script/deploy-tasks.sh @@ -19,7 +19,8 @@ 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:oneoff:populate_si_weight + echo "2015-01-30 - build Elasticsearch index" rake growstuff:oneoff:elasticsearch_create_index - - diff --git a/spec/models/harvest_spec.rb b/spec/models/harvest_spec.rb index 504c8c414..cd6d5f1ef 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.create(:harvest, :weight_quantity => 2, :weight_unit => "lb") + @harvest.should be_valid + @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.454 + 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 2.0 + end + end + context 'ordering' do it 'lists most recent harvests first' do @h1 = FactoryGirl.create(:harvest, :created_at => 1.day.ago)