Class: Rate

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ObjectDaddy
Defined in:
app/models/rate.rb

Defined Under Namespace

Classes: InvalidParameterException

Constant Summary collapse

CACHING_LOCK_FILE_NAME =
'rate_cache'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.amount_for(user, project = nil, date = Date.today.to_s) ⇒ Object

API to find the amount for a user on a project at a date



67
68
69
70
71
72
# File 'app/models/rate.rb', line 67

def self.amount_for(user, project = nil, date = Date.today.to_s)
  rate = self.for(user, project, date)

  return nil if rate.nil?
  return rate.amount
end

.for(user, project = nil, date = Date.today.to_s) ⇒ Object

API to find the Rate for a user on a project at a date



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/rate.rb', line 50

def self.for(user, project = nil, date = Date.today.to_s)
  # Check input since it's a "public" API
  if Object.const_defined? 'Group' # 0.8.x compatibility
    raise Rate::InvalidParameterException.new("user must be a Principal instance") unless user.is_a?(Principal)
  else
    raise Rate::InvalidParameterException.new("user must be a User instance") unless user.is_a?(User)
  end
  raise Rate::InvalidParameterException.new("project must be a Project instance") unless project.nil? || project.is_a?(Project)
  Rate.check_date_string(date)
    
  rate = self.for_user_project_and_date(user, project, date)
  # Check for a default (non-project) rate
  rate = self.default_for_user_and_date(user, date) if rate.nil? && project
  rate
end

.update_all_time_entries_to_refresh_cache(options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/rate.rb', line 87

def self.update_all_time_entries_to_refresh_cache(options={})
  with_common_lockfile(options[:force]) do
    TimeEntry.find_each do |time_entry| # batch find
      begin
        time_entry.save_cached_cost
      rescue Rate::InvalidParameterException => ex
        puts "Error saving #{time_entry.id}: #{ex.message}"
      end
    end
  end
  store_cache_timestamp('last_cache_clearing_run', Time.now.utc.to_s)
end

.update_all_time_entries_with_missing_cost(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/rate.rb', line 74

def self.update_all_time_entries_with_missing_cost(options={})
  with_common_lockfile(options[:force]) do
    TimeEntry.all(:conditions => {:cost => nil}).each do |time_entry|
      begin
        time_entry.save_cached_cost
      rescue Rate::InvalidParameterException => ex
        puts "Error saving #{time_entry.id}: #{ex.message}"
      end
    end
  end
  store_cache_timestamp('last_caching_run', Time.now.utc.to_s)
end

Instance Method Details

#default?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/rate.rb', line 37

def default?
  return self.project.nil?
end

#locked?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/models/rate.rb', line 29

def locked?
  return self.time_entries.length > 0
end

#specific?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/rate.rb', line 41

def specific?
  return !self.default?
end

#unlocked?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/rate.rb', line 33

def unlocked?
  return !self.locked?
end

#update_time_entry_cost_cacheObject



45
46
47
# File 'app/models/rate.rb', line 45

def update_time_entry_cost_cache
  TimeEntry.update_cost_cache(user, project)
end