diff --git a/app/helpers/contracts_helper.rb b/app/helpers/contracts_helper.rb index 5cdbc82..fbea455 100644 --- a/app/helpers/contracts_helper.rb +++ b/app/helpers/contracts_helper.rb @@ -2,6 +2,7 @@ module ContractsHelper def setup_nested_deliverable_records(deliverable) returning(deliverable) do |d| d.labor_budgets.build if d.labor_budgets.empty? + d.overhead_budgets.build if d.overhead_budgets.empty? end end end diff --git a/app/models/deliverable.rb b/app/models/deliverable.rb index abc1c91..6ed0369 100644 --- a/app/models/deliverable.rb +++ b/app/models/deliverable.rb @@ -5,8 +5,10 @@ class Deliverable < ActiveRecord::Base belongs_to :contract belongs_to :manager, :class_name => 'User', :foreign_key => 'manager_id' has_many :labor_budgets + has_many :overhead_budgets accepts_nested_attributes_for :labor_budgets + accepts_nested_attributes_for :overhead_budgets # Validations validates_presence_of :title diff --git a/app/models/overhead_budget.rb b/app/models/overhead_budget.rb new file mode 100644 index 0000000..62b8a69 --- /dev/null +++ b/app/models/overhead_budget.rb @@ -0,0 +1,18 @@ +class OverheadBudget < ActiveRecord::Base + unloadable + + # Associations + belongs_to :deliverable + + # Validations + + # Accessors + + def budget=(v) + if v.is_a? String + write_attribute(:budget, v.gsub(/[$ ,]/, '')) + else + write_attribute(:budget, v) + end + end +end diff --git a/app/views/deliverables/_form.html.erb b/app/views/deliverables/_form.html.erb index 5b9ab39..f3e9266 100644 --- a/app/views/deliverables/_form.html.erb +++ b/app/views/deliverables/_form.html.erb @@ -25,11 +25,18 @@ <% form.inputs :name => l(:text_deliverable_finances), :id => 'deliverable-finances' do %> <% form.semantic_fields_for :labor_budgets do |labor_budget| %> - <% labor_budget.inputs :name => l(:field_labor) do %> + <% labor_budget.inputs :name => l(:field_labor), :id => 'deliverable-labor' do %> <%= labor_budget.input :hours, :label => l(:text_short_hours), :input_html => {:size => 10} %> <%= labor_budget.input :budget, :label => l(:text_dollar_sign), :input_html => {:size => 10} %> <% end %> <% end %> + + <% form.semantic_fields_for :overhead_budgets do |overhead_budget| %> + <% overhead_budget.inputs :name => l(:field_overhead), :id => 'deliverable-overhead' do %> + <%= overhead_budget.input :hours, :label => l(:text_short_hours), :input_html => {:size => 10} %> + <%= overhead_budget.input :budget, :label => l(:text_dollar_sign), :input_html => {:size => 10} %> + <% end %> + <% end %> <% end %> <% end %> diff --git a/db/migrate/005_create_overhead_budgets.rb b/db/migrate/005_create_overhead_budgets.rb new file mode 100644 index 0000000..0cf8501 --- /dev/null +++ b/db/migrate/005_create_overhead_budgets.rb @@ -0,0 +1,16 @@ +class CreateOverheadBudgets < ActiveRecord::Migration + def self.up + create_table :overhead_budgets do |t| + t.decimal :hours, :precision => 15, :scale => 4 + t.decimal :budget, :precision => 15, :scale => 4 + t.references :deliverable + t.timestamps + end + + add_index :overhead_budgets, :deliverable_id + end + + def self.down + drop_table :overhead_budgets + end +end diff --git a/test/integration/deliverables_edit_test.rb b/test/integration/deliverables_edit_test.rb index a1254ea..473585c 100644 --- a/test/integration/deliverables_edit_test.rb +++ b/test/integration/deliverables_edit_test.rb @@ -60,8 +60,15 @@ class DeliverablesEditTest < ActionController::IntegrationTest check "Feature Sign Off" check "Warranty Sign Off" - fill_in "hrs", :with => '20' - fill_in "$", :with => '$2,000' + within("#deliverable-labor") do + fill_in "hrs", :with => '20' + fill_in "$", :with => '$2,000' + end + + within("#deliverable-overhead") do + fill_in "hrs", :with => '10' + fill_in "$", :with => '$1,000' + end click_button "Save" @@ -78,5 +85,9 @@ class DeliverablesEditTest < ActionController::IntegrationTest assert_equal 20, @labor_budget.hours assert_equal 2000.0, @labor_budget.budget + assert_equal 1, @hourly_deliverable.overhead_budgets.count + @overhead_budget = @hourly_deliverable.overhead_budgets.first + assert_equal 10, @overhead_budget.hours + assert_equal 1000.0, @overhead_budget.budget end end diff --git a/test/integration/deliverables_new_test.rb b/test/integration/deliverables_new_test.rb index 94f6173..bfa418c 100644 --- a/test/integration/deliverables_new_test.rb +++ b/test/integration/deliverables_new_test.rb @@ -112,7 +112,7 @@ class DeliverablesNewTest < ActionController::IntegrationTest end - should "create new budget item for the deliverables" do + should "create new budget items for the deliverables" do @manager = User.generate! @role = Role.generate! User.add_to_project(@manager, @project, @role) @@ -128,19 +128,32 @@ class DeliverablesNewTest < ActionController::IntegrationTest fill_in "End Date", :with => '2010-12-31' fill_in "Notes", :with => 'Some notes on the deliverable' - fill_in "hrs", :with => '20' - fill_in "$", :with => '$2,000' - + within("#deliverable-labor") do + fill_in "hrs", :with => '20' + fill_in "$", :with => '$2,000' + end + + within("#deliverable-overhead") do + fill_in "hrs", :with => '10' + fill_in "$", :with => '$1,000' + end + click_button "Save" assert_response :success assert_template 'contracts/show' @deliverable = Deliverable.last + assert_equal 1, @deliverable.labor_budgets.count @labor_budget = @deliverable.labor_budgets.first assert_equal 20, @labor_budget.hours assert_equal 2000.0, @labor_budget.budget + + assert_equal 1, @deliverable.overhead_budgets.count + @overhead_budget = @deliverable.overhead_budgets.first + assert_equal 10, @overhead_budget.hours + assert_equal 1000.0, @overhead_budget.budget end end diff --git a/test/unit/deliverable_test.rb b/test/unit/deliverable_test.rb index 22328d8..67da0f8 100644 --- a/test/unit/deliverable_test.rb +++ b/test/unit/deliverable_test.rb @@ -4,6 +4,7 @@ class DeliverableTest < ActiveSupport::TestCase should_belong_to :contract should_belong_to :manager should_have_many :labor_budgets + should_have_many :overhead_budgets should_validate_presence_of :title should_validate_presence_of :type diff --git a/test/unit/overhead_budget_test.rb b/test/unit/overhead_budget_test.rb new file mode 100644 index 0000000..38a3a31 --- /dev/null +++ b/test/unit/overhead_budget_test.rb @@ -0,0 +1,29 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class OverheadBudgetTest < ActiveSupport::TestCase + should_belong_to :deliverable + + context "#budget=" do + should "strip dollar signs when writing" do + e = OverheadBudget.new + e.budget = '$100.00' + + assert_equal 100.00, e.budget.to_f + end + + should "strip commas when writing" do + e = OverheadBudget.new + e.budget = '20,100.00' + + assert_equal 20100.00, e.budget.to_f + end + + should "strip spaces when writing" do + e = OverheadBudget.new + e.budget = '20 100.00' + + assert_equal 20100.00, e.budget.to_f + end + end + +end