Module: RateTimeEntryPatch::InstanceMethods

Defined in:
lib/rate_time_entry_patch.rb

Instance Method Summary collapse

Instance Method Details

#clear_cost_cacheObject



61
62
63
# File 'lib/rate_time_entry_patch.rb', line 61

def clear_cost_cache
  write_attribute(:cost, nil)
end

#cost(options = {}) ⇒ Object

Returns the current cost of the TimeEntry based on it’s rate and hours

Is a read-through cache method



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rate_time_entry_patch.rb', line 35

def cost(options={})
  store_to_db = options[:store] || false
  
  unless read_attribute(:cost)
    if self.rate.nil?
      amount = Rate.amount_for(self.user, self.project, self.spent_on.to_s)
    else
      amount = rate.amount
    end

    if amount.nil?
      write_attribute(:cost, 0.0)
    else
      if store_to_db
        # Write the cost to the database for caching
        update_attribute(:cost, amount.to_f * hours.to_f)
      else
        # Cache to object only
        write_attribute(:cost, amount.to_f * hours.to_f)
      end
    end
  end

  read_attribute(:cost)
end

#recalculate_costObject



70
71
72
73
74
# File 'lib/rate_time_entry_patch.rb', line 70

def recalculate_cost
  clear_cost_cache
  cost(:store => false)
  true # for callback
end

#save_cached_costObject



65
66
67
68
# File 'lib/rate_time_entry_patch.rb', line 65

def save_cached_cost
  clear_cost_cache
  update_attribute(:cost, cost)
end