diff --git a/app/models/deliverable.rb b/app/models/deliverable.rb index d6ec691..dffb5fe 100644 --- a/app/models/deliverable.rb +++ b/app/models/deliverable.rb @@ -4,13 +4,6 @@ class Deliverable < ActiveRecord::Base 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 def score 0 @@ -25,4 +18,46 @@ class Deliverable < ActiveRecord::Base def progress 0 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 diff --git a/db/migrate/005_add_percentage_fields_to_deliverables.rb b/db/migrate/005_add_percentage_fields_to_deliverables.rb new file mode 100644 index 0000000..40b7744 --- /dev/null +++ b/db/migrate/005_add_percentage_fields_to_deliverables.rb @@ -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 diff --git a/spec/models/deliverable_spec.rb b/spec/models/deliverable_spec.rb index 090cca5..cdae0f3 100644 --- a/spec/models/deliverable_spec.rb +++ b/spec/models/deliverable_spec.rb @@ -15,3 +15,93 @@ describe Deliverable, 'associations' do Deliverable.reflect_on_association(:project).should_not be( nil ) 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