mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-10-22 23:14:28 -04:00
Separated informal/human units from measured weights
This commit is contained in:
1 parent
4ba7a3a48e
commit
c5e8e4298f
11 files changed
+208
-24
No files matched your search
@@ -1,17 +1,40 @@
|
||||
module HarvestsHelper
|
||||
|
||||
def display_quantity(harvest)
|
||||
if ! harvest.quantity.blank?
|
||||
if harvest.unit == 'individual'
|
||||
number_to_human(harvest.quantity, :strip_insignificant_zeros => true)
|
||||
elsif harvest.unit == 'bunch'
|
||||
return pluralize(number_to_human(harvest.quantity, :strip_insignificant_zeros => true), harvest.unit)
|
||||
def display_quantity(harvest)
|
||||
human_quantity = display_human_quantity(harvest)
|
||||
weight = display_weight(harvest)
|
||||
|
||||
if human_quantity && weight
|
||||
return "#{human_quantity}, weighing #{weight}"
|
||||
elsif human_quantity
|
||||
return human_quantity
|
||||
elsif weight
|
||||
return weight
|
||||
else
|
||||
return "#{number_to_human(harvest.quantity, :strip_insignificant_zeros => true)} #{harvest.unit}"
|
||||
return 'not specified'
|
||||
end
|
||||
end
|
||||
|
||||
def display_human_quantity(harvest)
|
||||
if ! harvest.quantity.blank? && harvest.quantity > 0
|
||||
if harvest.unit == 'individual'
|
||||
number_to_human(harvest.quantity, :strip_insignificant_zeros => true)
|
||||
elsif harvest.unit == 'bunch'
|
||||
return pluralize(number_to_human(harvest.quantity, :strip_insignificant_zeros => true), harvest.unit)
|
||||
else
|
||||
return "#{number_to_human(harvest.quantity, :strip_insignificant_zeros => true)} #{harvest.unit}"
|
||||
end
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
def display_weight(harvest)
|
||||
if ! harvest.weight_quantity.blank? && harvest.weight_quantity > 0
|
||||
return "#{number_to_human(harvest.weight_quantity, :strip_insignificant_zeros => true)} #{harvest.weight_unit}"
|
||||
else
|
||||
return nil
|
||||
end
|
||||
else
|
||||
return 'not specified'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
+22
-4
@@ -3,7 +3,7 @@ class Harvest < ActiveRecord::Base
|
||||
friendly_id :harvest_slug, use: :slugged
|
||||
|
||||
attr_accessible :crop_id, :harvested_at, :description, :owner_id,
|
||||
:quantity, :unit, :slug
|
||||
:quantity, :unit, :weight_quantity, :weight_unit, :slug
|
||||
|
||||
belongs_to :crop
|
||||
belongs_to :owner, :class_name => 'Member'
|
||||
@@ -14,16 +14,28 @@ class Harvest < ActiveRecord::Base
|
||||
|
||||
UNITS_VALUES = {
|
||||
"individual" => "individual",
|
||||
"bunches" => "bunch",
|
||||
"kg" => "kg",
|
||||
"lb" => "lb"
|
||||
"bunches" => "bunch"
|
||||
}
|
||||
validates :unit, :inclusion => { :in => UNITS_VALUES.values,
|
||||
:message => "%{value} is not a valid unit" },
|
||||
:allow_nil => true,
|
||||
:allow_blank => true
|
||||
|
||||
validates :weight_quantity,
|
||||
:numericality => { :only_integer => false },
|
||||
:allow_nil => true
|
||||
|
||||
WEIGHT_UNITS_VALUES = {
|
||||
"kg" => "kg",
|
||||
"lb" => "lb"
|
||||
}
|
||||
validates :weight_unit, :inclusion => { :in => WEIGHT_UNITS_VALUES.values,
|
||||
:message => "%{value} is not a valid unit" },
|
||||
:allow_nil => true,
|
||||
:allow_blank => true
|
||||
|
||||
after_validation :clear_unit_if_qty_is_blank
|
||||
after_validation :clear_weight_unit_if_weight_qty_is_blank
|
||||
|
||||
def clear_unit_if_qty_is_blank
|
||||
if quantity.blank?
|
||||
@@ -31,6 +43,12 @@ class Harvest < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def clear_weight_unit_if_weight_qty_is_blank
|
||||
if weight_quantity.blank?
|
||||
self.weight_unit = nil
|
||||
end
|
||||
end
|
||||
|
||||
def harvest_slug
|
||||
"#{owner.login_name}-#{crop}".downcase.gsub(' ', '-')
|
||||
end
|
||||
|
||||
@@ -24,6 +24,12 @@
|
||||
= f.number_field :quantity, :class => 'input-small'
|
||||
= f.select(:unit, Harvest::UNITS_VALUES, {:include_blank => false}, :class => 'input-medium')
|
||||
|
||||
.control-group
|
||||
= f.label 'Weighing:', :class => 'control-label'
|
||||
.controls
|
||||
= f.number_field :weight_quantity, :class => 'input-small'
|
||||
= f.select(:weight_unit, Harvest::WEIGHT_UNITS_VALUES, {:include_blank => false}, :class => 'input-medium')
|
||||
in total
|
||||
.control-group
|
||||
= f.label 'Notes', :class => 'control-label'
|
||||
.controls= f.text_area :description, :rows => 6
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class AddWeightToHarvests < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :harvests, :weight_quantity, :decimal
|
||||
add_column :harvests, :weight_unit, :string
|
||||
end
|
||||
end
|
||||
+7
-5
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20130917075803) do
|
||||
ActiveRecord::Schema.define(:version => 20130925050304) do
|
||||
|
||||
create_table "account_types", :force => true do |t|
|
||||
t.string "name", :null => false
|
||||
@@ -88,15 +88,17 @@ ActiveRecord::Schema.define(:version => 20130917075803) do
|
||||
add_index "gardens", ["slug"], :name => "index_gardens_on_slug", :unique => true
|
||||
|
||||
create_table "harvests", :force => true do |t|
|
||||
t.integer "crop_id", :null => false
|
||||
t.integer "owner_id", :null => false
|
||||
t.integer "crop_id", :null => false
|
||||
t.integer "owner_id", :null => false
|
||||
t.date "harvested_at"
|
||||
t.decimal "quantity"
|
||||
t.string "unit"
|
||||
t.text "description"
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
t.string "slug"
|
||||
t.decimal "weight_quantity"
|
||||
t.string "weight_unit"
|
||||
end
|
||||
|
||||
create_table "members", :force => true do |t|
|
||||
|
||||
@@ -5,8 +5,10 @@ FactoryGirl.define do
|
||||
crop
|
||||
owner
|
||||
harvested_at "2013-09-17"
|
||||
quantity "9.99"
|
||||
unit "kg"
|
||||
quantity "3"
|
||||
unit "individual"
|
||||
weight_quantity 6
|
||||
weight_unit "kg"
|
||||
description "A lovely harvest"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,79 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe HarvestsHelper do
|
||||
describe "display_quantity" do
|
||||
|
||||
it "blank" do
|
||||
harvest = FactoryGirl.create(:harvest,
|
||||
:quantity => nil,
|
||||
:weight_quantity => nil
|
||||
)
|
||||
result = helper.display_quantity(harvest)
|
||||
result.should eq 'not specified'
|
||||
end
|
||||
|
||||
it '3 individual' do
|
||||
harvest = FactoryGirl.create(:harvest,
|
||||
:quantity => 3,
|
||||
:unit => 'individual',
|
||||
:weight_quantity => nil
|
||||
)
|
||||
result = helper.display_quantity(harvest)
|
||||
result.should eq '3'
|
||||
end
|
||||
|
||||
it '1 bunch' do
|
||||
harvest = FactoryGirl.create(:harvest,
|
||||
:quantity => 1,
|
||||
:unit => 'bunch',
|
||||
:weight_quantity => nil
|
||||
)
|
||||
result = helper.display_quantity(harvest)
|
||||
result.should eq '1 bunch'
|
||||
end
|
||||
|
||||
it '3 bunches' do
|
||||
harvest = FactoryGirl.create(:harvest,
|
||||
:quantity => 3,
|
||||
:unit => 'bunch',
|
||||
:weight_quantity => nil
|
||||
)
|
||||
result = helper.display_quantity(harvest)
|
||||
result.should eq '3 bunches'
|
||||
end
|
||||
|
||||
it '3 kg' do
|
||||
harvest = FactoryGirl.create(:harvest,
|
||||
:quantity => nil,
|
||||
:unit => nil,
|
||||
:weight_quantity => 3,
|
||||
:weight_unit => 'kg'
|
||||
)
|
||||
result = helper.display_quantity(harvest)
|
||||
result.should eq '3 kg'
|
||||
end
|
||||
|
||||
it '3 individual weighing 3 kg' do
|
||||
harvest = FactoryGirl.create(:harvest,
|
||||
:quantity => 3,
|
||||
:unit => 'individual',
|
||||
:weight_quantity => 3,
|
||||
:weight_unit => 'kg'
|
||||
)
|
||||
result = helper.display_quantity(harvest)
|
||||
result.should eq '3, weighing 3 kg'
|
||||
end
|
||||
|
||||
it '3 bunches weighing 3 kg' do
|
||||
harvest = FactoryGirl.create(:harvest,
|
||||
:quantity => 3,
|
||||
:unit => 'bunch',
|
||||
:weight_quantity => 3,
|
||||
:weight_unit => 'kg'
|
||||
)
|
||||
result = helper.display_quantity(harvest)
|
||||
result.should eq '3 bunches, weighing 3 kg'
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -41,7 +41,7 @@ describe Harvest do
|
||||
|
||||
context 'units' do
|
||||
it 'all valid units should work' do
|
||||
['individual', 'bunch', 'kg', 'lb', nil, ''].each do |s|
|
||||
['individual', 'bunch', nil, ''].each do |s|
|
||||
@harvest = FactoryGirl.build(:harvest, :unit=> s)
|
||||
@harvest.should be_valid
|
||||
end
|
||||
@@ -54,12 +54,57 @@ describe Harvest do
|
||||
end
|
||||
|
||||
it 'sets unit to blank if quantity is blank' do
|
||||
@harvest = FactoryGirl.build(:harvest, :quantity => '', :unit => 'kg')
|
||||
@harvest = FactoryGirl.build(:harvest, :quantity => '', :unit => 'individual')
|
||||
@harvest.should be_valid
|
||||
@harvest.unit.should eq nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'weight quantity' do
|
||||
it 'allows numeric weight quantities' do
|
||||
@harvest = FactoryGirl.build(:harvest, :weight_quantity => 33)
|
||||
@harvest.should be_valid
|
||||
end
|
||||
|
||||
it 'allows decimal weight quantities' do
|
||||
@harvest = FactoryGirl.build(:harvest, :weight_quantity => 3.3)
|
||||
@harvest.should be_valid
|
||||
end
|
||||
|
||||
it 'allows blank weight quantities' do
|
||||
@harvest = FactoryGirl.build(:harvest, :weight_quantity => '')
|
||||
@harvest.should be_valid
|
||||
end
|
||||
|
||||
it 'allows nil weight quantities' do
|
||||
@harvest = FactoryGirl.build(:harvest, :weight_quantity => nil)
|
||||
@harvest.should be_valid
|
||||
end
|
||||
|
||||
it "doesn't allow non-numeric weight quantities" do
|
||||
@harvest = FactoryGirl.build(:harvest, :weight_quantity => "99a")
|
||||
@harvest.should_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
context 'weight units' do
|
||||
it 'all valid units should work' do
|
||||
['kg', 'lb', nil, ''].each do |s|
|
||||
@harvest = FactoryGirl.build(:harvest, :weight_unit => s)
|
||||
@harvest.should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
it 'should refuse invalid weight unit values' do
|
||||
@harvest = FactoryGirl.build(:harvest, :weight_unit => 'not valid')
|
||||
@harvest.should_not be_valid
|
||||
@harvest.errors[:weight_unit].should include("not valid is not a valid unit")
|
||||
end
|
||||
|
||||
it 'sets weight_unit to blank if quantity is blank' do
|
||||
@harvest = FactoryGirl.build(:harvest, :weight_quantity => '', :weight_unit => 'kg')
|
||||
@harvest.should be_valid
|
||||
@harvest.weight_unit.should eq nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -10,7 +10,9 @@ describe "harvests/edit" do
|
||||
assert_select "form", :action => harvests_path, :method => "post" do
|
||||
assert_select "select#harvest_crop_id", :name => "harvest[crop_id]"
|
||||
assert_select "input#harvest_quantity", :name => "harvest[quantity]"
|
||||
assert_select "input#harvest_weight_quantity", :name => "harvest[quantity]"
|
||||
assert_select "select#harvest_unit", :name => "harvest[unit]"
|
||||
assert_select "select#harvest_weight_unit", :name => "harvest[unit]"
|
||||
assert_select "textarea#harvest_description", :name => "harvest[description]"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,7 +10,9 @@ describe "harvests/new" do
|
||||
assert_select "form", :action => harvests_path, :method => "post" do
|
||||
assert_select "select#harvest_crop_id", :name => "harvest[crop_id]"
|
||||
assert_select "input#harvest_quantity", :name => "harvest[quantity]"
|
||||
assert_select "input#harvest_weight_quantity", :name => "harvest[quantity]"
|
||||
assert_select "select#harvest_unit", :name => "harvest[unit]"
|
||||
assert_select "select#harvest_weight_unit", :name => "harvest[unit]"
|
||||
assert_select "textarea#harvest_description", :name => "harvest[description]"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,7 +11,6 @@ describe "harvests/show" do
|
||||
it "renders attributes" do
|
||||
rendered.should contain @crop.system_name
|
||||
rendered.should contain @harvest.harvested_at.to_s
|
||||
rendered.should contain "9.99 kg"
|
||||
end
|
||||
|
||||
end
|
||||
Reference in new issue
Block a user