diff --git a/app/models/rate.rb b/app/models/rate.rb index e1fe998..a8e766e 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -1,4 +1,6 @@ class Rate < ActiveRecord::Base + class InvalidParameterException < Exception; end + belongs_to :project belongs_to :user has_many :time_entries @@ -27,9 +29,9 @@ class Rate < ActiveRecord::Base # API to find the Rate for a +user+ on a +project+ at a +date+ def self.for(user, project = nil, date = Date.today.to_s) # Check input since it's a "public" API - return nil unless user.is_a?(User) - return nil unless project.nil? || project.is_a?(Project) - + raise Rate::InvalidParameterException.new("user must be a User instance") unless user.is_a?(User) + raise Rate::InvalidParameterException.new("project must be a Project instance") unless project.nil? || project.is_a?(Project) + rate = self.for_user_project_and_date(user, project, date) return nil if rate.nil? diff --git a/spec/models/rate_spec.rb b/spec/models/rate_spec.rb index 5868e57..de22a27 100644 --- a/spec/models/rate_spec.rb +++ b/spec/models/rate_spec.rb @@ -191,16 +191,20 @@ describe Rate, 'for' do end end - it 'with an invalid user should be nil' do + it 'with an invalid user should raise an InvalidParameterException' do object = mock('random_object_with_id_attribute') Rate.should_not_receive(:for_user_project_and_date) - Rate.for(object).should be_nil + lambda { + Rate.for(object) + }.should raise_error(Rate::InvalidParameterException, "user must be a User instance") end it 'with an invalid project should be nil' do object = mock('random_object_with_id_attribute') Rate.should_not_receive(:for_user_project_and_date) - Rate.for(@user, object).should be_nil + lambda { + Rate.for(@user, object) + }.should raise_error(Rate::InvalidParameterException, "project must be a Project instance") end end