diff --git a/app/helpers/deliverables_helper.rb b/app/helpers/deliverables_helper.rb index 29155e0..c46fe6f 100644 --- a/app/helpers/deliverables_helper.rb +++ b/app/helpers/deliverables_helper.rb @@ -3,10 +3,10 @@ module DeliverablesHelper # Helper to generate a form used to calculate the total budget while editing # a Deliverable # TODO Later: Refactor since observers are not used anymore - def field_with_budget_observer_and_totals(form, field) + def field_with_budget_observer_and_totals(form, object, field, percent_field) content_tag(:tr, content_tag(:td, "") + - content_tag(:td, text_field(:deliverable, field, :size => 7)) + + content_tag(:td, number_or_percent_field(object, field, percent_field, :size => 7)) + content_tag(:td, content_tag(:span, 0, @@ -17,6 +17,16 @@ module DeliverablesHelper )) end + + def number_or_percent_field(object, number_field, percent_field, options) + # Build a text_field by hand named after the number field but with the percent_field and % as the value + return text_field_tag('deliverable_' + number_field.to_s, + object.read_attribute(percent_field).to_s + "%", + options.merge({ :name => "deliverable[#{number_field.to_s}]"})) unless object.read_attribute(percent_field).blank? + + # Number and fallback with no values + return text_field(:deliverable, number_field, options) + end # Helper to generate a consistant HTML format for displaying basic data def paragraph_with_data(label, data) diff --git a/app/views/deliverables/_form.html.erb b/app/views/deliverables/_form.html.erb index 7d38ddd..cbd69aa 100644 --- a/app/views/deliverables/_form.html.erb +++ b/app/views/deliverables/_form.html.erb @@ -64,9 +64,9 @@ - <%= field_with_budget_observer_and_totals(f, :overhead) %> - <%= field_with_budget_observer_and_totals(f, :materials) %> - <%= field_with_budget_observer_and_totals(f, :profit) %> + <%= field_with_budget_observer_and_totals(f, @deliverable, :overhead, :overhead_percent) %> + <%= field_with_budget_observer_and_totals(f, @deliverable, :materials, :materials_percent) %> + <%= field_with_budget_observer_and_totals(f, @deliverable, :profit, :profit_percent) %>