Skip to content

Commit

Permalink
Merge pull request #1184 from sparc-request/sj-hotfix_for_indirect_cost
Browse files Browse the repository at this point in the history
SJ - Fixing indirect cost validation [#152641470]
  • Loading branch information
Stuart-Johnson committed Nov 9, 2017
2 parents 2c14897 + 3b3a4e3 commit 93abd69
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/models/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Protocol < ApplicationRecord
validates :research_master_id, numericality: { only_integer: true }, allow_blank: true
validates :research_master_id, presence: true, if: :rmid_requires_validation?

validates :indirect_cost_rate, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 1000 }, allow_blank: true
validates :indirect_cost_rate, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 1000 }, allow_blank: true, if: :indirect_cost_enabled

attr_accessor :requester_id
attr_accessor :validate_nct
Expand Down Expand Up @@ -552,6 +552,10 @@ def has_incomplete_additional_details?

private

def indirect_cost_enabled
Setting.find_by_key('use_indirect_cost').value
end

def notify_remote_around_update?
true
end
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20171107195635_correct_bad_indirect_cost_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CorrectBadIndirectCostData < ActiveRecord::Migration[5.1]
def change
protocols = Protocol.where(indirect_cost_rate: 0.0)
protocols.each do |protocol|
protocol.indirect_cost_rate = nil
protocol.save(validate: false)
end
end
end
17 changes: 15 additions & 2 deletions spec/models/protocol/valid?_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@
end
end

it { is_expected.to validate_numericality_of(:indirect_cost_rate).is_greater_than_or_equal_to(1) }
it { is_expected.to validate_numericality_of(:indirect_cost_rate).is_less_than_or_equal_to(1000) }
context 'indirect cost is turned off' do
it 'should not validate indirect cost' do
is_expected.not_to validate_numericality_of(:indirect_cost_rate).is_greater_than_or_equal_to(1)
is_expected.not_to validate_numericality_of(:indirect_cost_rate).is_less_than_or_equal_to(1000)
end
end

context 'indirect cost is turned on' do
stub_config("use_indirect_cost", true)

it 'should validate indirect cost' do
is_expected.to validate_numericality_of(:indirect_cost_rate).is_greater_than_or_equal_to(1)
is_expected.to validate_numericality_of(:indirect_cost_rate).is_less_than_or_equal_to(1000)
end
end
end

0 comments on commit 93abd69

Please sign in to comment.