[#4182] Hooked up nested attributes for Deliverable -> LaborExpense

This commit is contained in:
Eric Davis
2010-07-02 15:39:40 -07:00
parent a8452a834f
commit 2dd3331f66
11 changed files with 65 additions and 10 deletions
+2 -1
View File
@@ -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
+5
View File
@@ -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
+2
View File
@@ -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
-3
View File
@@ -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
+11
View File
@@ -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) %>
+1 -1
View File
@@ -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 %>
+1 -1
View File
@@ -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 %>
+3
View File
@@ -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: '$'
@@ -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
+31
View File
@@ -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
-4
View File
@@ -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