From 2dd3331f66c5804401e02a9aa01e1be39504f1ce Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Fri, 2 Jul 2010 15:39:40 -0700 Subject: [PATCH] [#4182] Hooked up nested attributes for Deliverable -> LaborExpense --- app/controllers/deliverables_controller.rb | 3 ++- app/helpers/contracts_helper.rb | 5 ++++ app/models/deliverable.rb | 2 ++ app/models/labor_expense.rb | 3 --- app/views/deliverables/_form.html.erb | 11 ++++++++ app/views/deliverables/edit.html.erb | 2 +- app/views/deliverables/new.html.erb | 2 +- config/locales/en.yml | 3 +++ test/integration/deliverables_edit_test.rb | 9 +++++++ test/integration/deliverables_new_test.rb | 31 ++++++++++++++++++++++ test/unit/labor_expense_test.rb | 4 --- 11 files changed, 65 insertions(+), 10 deletions(-) diff --git a/app/controllers/deliverables_controller.rb b/app/controllers/deliverables_controller.rb index 94c2d25..a3d69a3 100644 --- a/app/controllers/deliverables_controller.rb +++ b/app/controllers/deliverables_controller.rb @@ -6,6 +6,8 @@ class DeliverablesController < InheritedResources::Base before_filter :find_contract before_filter :authorize + helper :contracts + def index redirect_to contract_url(@project, @contract) end @@ -21,7 +23,6 @@ class DeliverablesController < InheritedResources::Base def update @deliverable = begin_of_association_chain.deliverables.find_by_id(params[:id]) params[:deliverable] = params[:fixed_deliverable] || params[:hourly_deliverable] - @deliverable.attributes = params[:deliverable] update! { contract_url(@project, @contract) } end diff --git a/app/helpers/contracts_helper.rb b/app/helpers/contracts_helper.rb index d465d24..0b5d6af 100644 --- a/app/helpers/contracts_helper.rb +++ b/app/helpers/contracts_helper.rb @@ -1,2 +1,7 @@ module ContractsHelper + def setup_nested_deliverable_records(deliverable) + returning(deliverable) do |d| + d.labor_expenses.build if d.labor_expenses.empty? + end + end end diff --git a/app/models/deliverable.rb b/app/models/deliverable.rb index 97d3575..2f34728 100644 --- a/app/models/deliverable.rb +++ b/app/models/deliverable.rb @@ -5,6 +5,8 @@ class Deliverable < ActiveRecord::Base belongs_to :contract belongs_to :manager, :class_name => 'User', :foreign_key => 'manager_id' has_many :labor_expenses + + accepts_nested_attributes_for :labor_expenses # Validations validates_presence_of :title diff --git a/app/models/labor_expense.rb b/app/models/labor_expense.rb index 0e32a0c..142b33d 100644 --- a/app/models/labor_expense.rb +++ b/app/models/labor_expense.rb @@ -5,9 +5,6 @@ class LaborExpense < ActiveRecord::Base belongs_to :deliverable # Validations - validates_presence_of :hours - validates_presence_of :budget - validates_presence_of :deliverable # Accessors diff --git a/app/views/deliverables/_form.html.erb b/app/views/deliverables/_form.html.erb index 4540446..ab60787 100644 --- a/app/views/deliverables/_form.html.erb +++ b/app/views/deliverables/_form.html.erb @@ -22,7 +22,18 @@ <% end %> <%= form.input :total, :input_html => {:size => 10}, :wrapper_html => {:class => 'deliverable_total_input'} %> + + <% form.inputs :name => l(:text_deliverable_finances) do %> + <% form.semantic_fields_for :labor_expenses do |labor_expense| %> + <% labor_expense.inputs :name => l(:field_labor) do %> + <%= labor_expense.input :hours, :label => l(:text_short_hours), :input_html => {:size => 10} %> + <%= labor_expense.input :budget, :label => l(:text_dollar_sign), :input_html => {:size => 10} %> + <% end %> + <% end %> + <% end %> + <% end %> + <% form.buttons do %> <%= form.commit_button :label => l(:button_save) %> <%= link_to(l(:button_cancel), cancel_path) %> diff --git a/app/views/deliverables/edit.html.erb b/app/views/deliverables/edit.html.erb index 8526537..0f7c660 100644 --- a/app/views/deliverables/edit.html.erb +++ b/app/views/deliverables/edit.html.erb @@ -1,5 +1,5 @@ <%= content_tag(:h2, h(resource.title)) %> -<% semantic_form_for [@project, @contract, resource], :url => contract_deliverable_path(@project, @contract, resource), :html => {:class => 'deliverable tabular'} do |form| %> +<% semantic_form_for [@project, @contract, setup_nested_deliverable_records(resource)], :url => contract_deliverable_path(@project, @contract, resource), :html => {:class => 'deliverable tabular'} do |form| %> <%= render :partial => 'form', :object => form, :locals => {:cancel_path => contract_path(@project, @contract)} %> <% end %> diff --git a/app/views/deliverables/new.html.erb b/app/views/deliverables/new.html.erb index d48e99e..601e052 100644 --- a/app/views/deliverables/new.html.erb +++ b/app/views/deliverables/new.html.erb @@ -1,5 +1,5 @@ <%= content_tag(:h2, l(:text_new_deliverable)) %> -<% semantic_form_for [@project, @contract, resource], :url => contract_deliverables_path(@project, @contract), :html => {:class => 'deliverable tabular'} do |form| %> +<% semantic_form_for [@project, @contract, setup_nested_deliverable_records(resource)], :url => contract_deliverables_path(@project, @contract), :html => {:class => 'deliverable tabular'} do |form| %> <%= render :partial => 'form', :object => form, :locals => {:cancel_path => contract_path(@project, @contract)} %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 446f260..da50bc0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -29,3 +29,6 @@ en: field_total: Total field_feature_sign_off: Feature Sign Off field_warranty_sign_off: Warranty Sign Off + text_deliverable_finances: Deliverable Finances + text_short_hours: hrs + text_dollar_sign: '$' diff --git a/test/integration/deliverables_edit_test.rb b/test/integration/deliverables_edit_test.rb index 10c16d9..000582d 100644 --- a/test/integration/deliverables_edit_test.rb +++ b/test/integration/deliverables_edit_test.rb @@ -59,6 +59,10 @@ class DeliverablesEditTest < ActionController::IntegrationTest fill_in "Title", :with => 'An updated title' check "Feature Sign Off" check "Warranty Sign Off" + + fill_in "hrs", :with => '20' + fill_in "$", :with => '$2,000' + click_button "Save" assert_response :success @@ -69,5 +73,10 @@ class DeliverablesEditTest < ActionController::IntegrationTest assert @hourly_deliverable.reload.warranty_sign_off? assert @hourly_deliverable.reload.feature_sign_off? + assert_equal 1, @hourly_deliverable.labor_expenses.count + @labor_expense = @hourly_deliverable.labor_expenses.first + assert_equal 20, @labor_expense.hours + assert_equal 2000.0, @labor_expense.budget + end end diff --git a/test/integration/deliverables_new_test.rb b/test/integration/deliverables_new_test.rb index 72d422e..47c572c 100644 --- a/test/integration/deliverables_new_test.rb +++ b/test/integration/deliverables_new_test.rb @@ -112,4 +112,35 @@ class DeliverablesNewTest < ActionController::IntegrationTest end + should "create new expenses for the deliverables" do + @manager = User.generate! + @role = Role.generate! + User.add_to_project(@manager, @project, @role) + + visit_contract_page(@contract) + click_link 'Add New' + assert_response :success + + fill_in "Title", :with => 'A New Deliverable' + select "Hourly", :from => "Type" + select @manager.name, :from => "Manager" + fill_in "Start", :with => '2010-01-01' + 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' + + click_button "Save" + + assert_response :success + assert_template 'contracts/show' + + @deliverable = Deliverable.last + assert_equal 1, @deliverable.labor_expenses.count + @labor_expense = @deliverable.labor_expenses.first + assert_equal 20, @labor_expense.hours + assert_equal 2000.0, @labor_expense.budget + end + end diff --git a/test/unit/labor_expense_test.rb b/test/unit/labor_expense_test.rb index 222b388..8b92ff7 100644 --- a/test/unit/labor_expense_test.rb +++ b/test/unit/labor_expense_test.rb @@ -3,10 +3,6 @@ require File.dirname(__FILE__) + '/../test_helper' class LaborExpenseTest < ActiveSupport::TestCase should_belong_to :deliverable - should_validate_presence_of :hours - should_validate_presence_of :budget - should_validate_presence_of :deliverable - context "#budget=" do should "strip dollar signs when writing" do e = LaborExpense.new