Added percent fields and changed the setters on Deliverables to save the values. #1135

This commit is contained in:
Eric Davis
2008-05-21 10:17:26 -07:00
parent 1330740fa3
commit a9c8897ed4
3 changed files with 145 additions and 7 deletions

View File

@@ -4,13 +4,6 @@ class Deliverable < ActiveRecord::Base
belongs_to :project belongs_to :project
attr_accessor :overhead
attr_accessor :materials
attr_accessor :profit
attr_accessor :cost_per_hour
attr_accessor :total_hours
attr_accessor :fixed_cost
# TODO: mocked # TODO: mocked
def score def score
0 0
@@ -25,4 +18,46 @@ class Deliverable < ActiveRecord::Base
def progress def progress
0 0
end end
#
# These attributes can take a Dollar amount or a %
#
def overhead=(v)
if v.match(/%/)
# Clear amount since this is a %
write_attribute(:overhead, nil)
write_attribute(:overhead_percent, v.gsub(/[% ]/,''))
else
# Clear % since this is a dollar amount
write_attribute(:overhead_percent, nil)
# Take out $, commas, and spaces
write_attribute(:overhead, v.gsub(/[$, ]/,''))
end
end
def materials=(v)
if v.match(/%/)
# Clear amount since this is a %
write_attribute(:materials, nil)
write_attribute(:materials_percent, v.gsub(/[% ]/,''))
else
# Clear % since this is a dollar amount
write_attribute(:materials_percent, nil)
# Take out $, commas, and spaces
write_attribute(:materials, v.gsub(/[$, ]/,''))
end
end
def profit=(v)
if v.match(/%/)
# Clear amount since this is a %
write_attribute(:profit, nil)
write_attribute(:profit_percent, v.gsub(/[% ]/,''))
else
# Clear % since this is a dollar amount
write_attribute(:profit_percent, nil)
# Take out $, commas, and spaces
write_attribute(:profit, v.gsub(/[$, ]/,''))
end
end
end end

View File

@@ -0,0 +1,13 @@
class AddPercentageFieldsToDeliverables < ActiveRecord::Migration
def self.up
add_column :deliverables, :overhead_percent, :integer
add_column :deliverables, :materials_percent, :integer
add_column :deliverables, :profit_percent, :integer
end
def self.down
remove_column :deliverables, :overhead_percent
remove_column :deliverables, :materials_percent
remove_column :deliverables, :profit_percent
end
end

View File

@@ -15,3 +15,93 @@ describe Deliverable, 'associations' do
Deliverable.reflect_on_association(:project).should_not be( nil ) Deliverable.reflect_on_association(:project).should_not be( nil )
end end
end end
describe Deliverable, '.overhead' do
before(:each) do
@deliverable = Deliverable.new({ :subject => 'test' })
end
describe 'with a dollar amount' do
it 'should store the dollar amount' do
@deliverable.overhead = "$1, 000.10"
@deliverable.overhead.should eql(1000.1)
end
it 'should clear the .overhead_percent' do
@deliverable.overhead = "$1, 000.10"
@deliverable.overhead_percent.should eql(nil)
end
end
describe 'with a percentage' do
it 'should store the % of the amount to .overhead_percent' do
@deliverable.overhead = "100 %"
@deliverable.overhead_percent.should eql(100)
end
it 'should clean the .overhead' do
@deliverable.overhead = "100 %"
@deliverable.overhead.should eql(nil)
end
end
end
describe Deliverable, '.materials' do
before(:each) do
@deliverable = Deliverable.new({ :subject => 'test' })
end
describe 'with a dollar amount' do
it 'should store the dollar amount' do
@deliverable.materials = "$1, 000.10"
@deliverable.materials.should eql(1000.1)
end
it 'should clear the .materials_percent' do
@deliverable.materials = "$1, 000.10"
@deliverable.materials_percent.should eql(nil)
end
end
describe 'with a percentage' do
it 'should store the % of the amount to .materials_percent' do
@deliverable.materials = "100 %"
@deliverable.materials_percent.should eql(100)
end
it 'should clean the .materials' do
@deliverable.materials = "100 %"
@deliverable.materials.should eql(nil)
end
end
end
describe Deliverable, '.profit' do
before(:each) do
@deliverable = Deliverable.new({ :subject => 'test' })
end
describe 'with a dollar amount' do
it 'should store the dollar amount' do
@deliverable.profit = "$1, 000.10"
@deliverable.profit.should eql(1000.1)
end
it 'should clear the .profit_percent' do
@deliverable.profit = "$1, 000.10"
@deliverable.profit_percent.should eql(nil)
end
end
describe 'with a percentage' do
it 'should store the % of the amount to .profit_percent' do
@deliverable.profit = "100 %"
@deliverable.profit_percent.should eql(100)
end
it 'should clean the .profit' do
@deliverable.profit = "100 %"
@deliverable.profit.should eql(nil)
end
end
end