Merge pull request #684 from maco/standard_weights

Standard weights for harvests
This commit is contained in:
Skud
2015-02-02 14:06:39 +11:00
10 changed files with 54 additions and 5 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -0,0 +1,5 @@
class AddSiWeightToHarvest < ActiveRecord::Migration
def change
add_column :harvests, :si_weight, :float
end
end

View File

@@ -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|

View File

@@ -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

View File

@@ -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

View File

@@ -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)