diff --git a/app/models/rate.rb b/app/models/rate.rb index 70504a9..f8b0354 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -8,36 +8,36 @@ class Rate < ActiveRecord::Base belongs_to :project belongs_to :user has_many :time_entries - + validates_presence_of :user_id validates_presence_of :date_in_effect validates_numericality_of :amount - + before_save :unlocked? after_save :update_time_entry_cost_cache before_destroy :unlocked? after_destroy :update_time_entry_cost_cache - - named_scope :history_for_user, lambda { |user, order| + + scope :history_for_user, lambda { |user, order| { :conditions => { :user_id => user.id }, :order => order, :include => :project } } - + def locked? return self.time_entries.length > 0 end - + def unlocked? return !self.locked? end - + def default? return self.project.nil? end - + def specific? return !self.default? end @@ -45,7 +45,7 @@ class Rate < ActiveRecord::Base def update_time_entry_cost_cache TimeEntry.update_cost_cache(user, project) end - + # 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 @@ -56,13 +56,13 @@ class Rate < ActiveRecord::Base 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 - + # API to find the amount for a +user+ on a +project+ at a +date+ def self.amount_for(user, project = nil, date = Date.today.to_s) rate = self.for(user, project, date) @@ -96,7 +96,7 @@ class Rate < ActiveRecord::Base end store_cache_timestamp('last_cache_clearing_run', Time.now.utc.to_s) end - + private def self.for_user_project_and_date(user, project, date) if project.nil? @@ -107,7 +107,7 @@ class Rate < ActiveRecord::Base user.id, date ]) - + else return Rate.find(:first, :order => 'date_in_effect DESC', @@ -117,9 +117,9 @@ class Rate < ActiveRecord::Base project.id, date ]) - end + end end - + def self.default_for_user_and_date(user, date) self.for_user_project_and_date(user, nil, date) end @@ -128,7 +128,7 @@ class Rate < ActiveRecord::Base # a Rate::InvalidParameterException otherwise def self.check_date_string(date) raise Rate::InvalidParameterException.new("date must be a valid Date string (e.g. YYYY-MM-DD)") unless date.is_a?(String) - + begin Date.parse(date) rescue ArgumentError @@ -144,7 +144,7 @@ class Rate < ActiveRecord::Base # Wait 1 second after stealing a forced lock options = {:retries => 0, :suspend => 1} options[:max_age] = 1 if force - + Lockfile(lock_file, options) do block.call end diff --git a/config/routes.rb b/config/routes.rb index 93bd22b..5485a2a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,4 @@ -ActionController::Routing::Routes.draw do |map| - map.resources :rates - map.connect 'rate_caches', :conditions => {:method => :put}, :controller => 'rate_caches', :action => 'update' -end +resources :rates + +match 'rate_caches', :to => 'rate_caches#index', :via => "get" +match 'rate_caches', :to => 'rate_caches#update', :via => "put" diff --git a/init.rb b/init.rb index da7bda4..8f9365a 100644 --- a/init.rb +++ b/init.rb @@ -1,9 +1,7 @@ require 'redmine' # Patches to the Redmine core -require 'dispatcher' - -Dispatcher.to_prepare :redmine_rate do +ActionDispatch::Callbacks.to_prepare do gem 'lockfile' require_dependency 'application_controller' diff --git a/lib/rate_project_hook.rb b/lib/rate_project_hook.rb index 0819d1f..21ebd48 100644 --- a/lib/rate_project_hook.rb +++ b/lib/rate_project_hook.rb @@ -1,10 +1,10 @@ # Hooks to attach to the Redmine Projects. class RateProjectHook < Redmine::Hook::ViewListener - + def protect_against_forgery? false end - + # Renders an additional table header to the membership setting # # Context: @@ -14,7 +14,7 @@ class RateProjectHook < Redmine::Hook::ViewListener return '' unless (User.current.allowed_to?(:view_rate, context[:project]) || User.current.admin?) return "