From ae8c75a0514f302a1ada23a77941d519dcf0e9d0 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Thu, 1 Jul 2010 15:22:12 -0700 Subject: [PATCH] [#4181] Add a total field to deliverables. --- app/models/deliverable.rb | 8 +++++++ app/views/deliverables/_form.html.erb | 2 ++ config/locales/en.yml | 2 +- db/migrate/003_add_total_to_deliverables.rb | 11 ++++++++++ test/integration/deliverables_new_test.rb | 2 ++ test/unit/deliverable_test.rb | 23 +++++++++++++++++++++ 6 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 db/migrate/003_add_total_to_deliverables.rb diff --git a/app/models/deliverable.rb b/app/models/deliverable.rb index 5ab3b5a..1f9a49b 100644 --- a/app/models/deliverable.rb +++ b/app/models/deliverable.rb @@ -16,6 +16,14 @@ class Deliverable < ActiveRecord::Base '' end + def total=(v) + if v.is_a? String + write_attribute(:total, v.gsub(/[$ ,]/, '')) + else + write_attribute(:total, v) + end + end + if Rails.env.test? generator_for :title, :method => :next_title diff --git a/app/views/deliverables/_form.html.erb b/app/views/deliverables/_form.html.erb index 99db133..686a985 100644 --- a/app/views/deliverables/_form.html.erb +++ b/app/views/deliverables/_form.html.erb @@ -5,6 +5,8 @@ <%= form.input :start_date, :as => :string, :input_html => {:size => 10}, :hint => calendar_for('deliverable_start_date') %> <%= form.input :end_date, :as => :string, :input_html => {:size => 10}, :hint => calendar_for('deliverable_end_date') %> <%= form.input :notes, :input_html => {:class => 'wiki-edit', :rows => '5'} %> + + <%= form.input :total, :input_html => {:size => 10} %> <% end %> <% form.buttons do %> <%= form.commit_button :label => l(:button_save) %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 3eb61da..ff70c7d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -26,4 +26,4 @@ en: field_labor: Labor field_overhead: Overhead field_fixed: Fixed - + field_total: Total diff --git a/db/migrate/003_add_total_to_deliverables.rb b/db/migrate/003_add_total_to_deliverables.rb new file mode 100644 index 0000000..b8d6c0c --- /dev/null +++ b/db/migrate/003_add_total_to_deliverables.rb @@ -0,0 +1,11 @@ +class AddTotalToDeliverables < ActiveRecord::Migration + def self.up + add_column :deliverables, :total, :decimal, :precision => 15, :scale => 2 + + add_index :deliverables, :total + end + + def self.down + remove_column :deliverables, :total + end +end diff --git a/test/integration/deliverables_new_test.rb b/test/integration/deliverables_new_test.rb index 4c90c76..c8e5f7d 100644 --- a/test/integration/deliverables_new_test.rb +++ b/test/integration/deliverables_new_test.rb @@ -56,6 +56,7 @@ class DeliverablesNewTest < ActionController::IntegrationTest 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 "Total", :with => '1,000.00' click_button "Save" @@ -69,5 +70,6 @@ class DeliverablesNewTest < ActionController::IntegrationTest assert_equal '2010-01-01', @deliverable.start_date.to_s assert_equal '2010-12-31', @deliverable.end_date.to_s assert_equal @manager, @deliverable.manager + assert_equal 1000.0, @deliverable.total.to_f end end diff --git a/test/unit/deliverable_test.rb b/test/unit/deliverable_test.rb index 691a07e..a88b9fd 100644 --- a/test/unit/deliverable_test.rb +++ b/test/unit/deliverable_test.rb @@ -7,4 +7,27 @@ class DeliverableTest < ActiveSupport::TestCase should_validate_presence_of :title should_validate_presence_of :type should_validate_presence_of :manager + + context "#total=" do + should "strip dollar signs when writing" do + d = Deliverable.new + d.total = '$100.00' + + assert_equal 100.00, d.total.to_f + end + + should "strip commas when writing" do + d = Deliverable.new + d.total = '20,100.00' + + assert_equal 20100.00, d.total.to_f + end + + should "strip spaces when writing" do + d = Deliverable.new + d.total = '20 100.00' + + assert_equal 20100.00, d.total.to_f + end + end end