OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/lib/user_patch.rb

69 lines
1.7 KiB

# TODO: which require statement to use here? require_dependency breaks stuff
#require 'user'
# Patches Redmine's Users dynamically.
module UserPatch
def self.included(base) # :nodoc:
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
# Same as typing in the class
base.class_eval do
unloadable
has_many :rates, :class_name => 'HourlyRate'
after_update :save_rates
end
end
module ClassMethods
end
module InstanceMethods
def current_rate(project_id)
rate_at(Date.today, project_id)
end
def rate_at(date, project_id)
HourlyRate.find(:first, :conditions => [ "user_id = ? and project_id = ? and valid_from <= ?", id, project_id, date], :order => "valid_from DESC")
end
def add_rates(project, rate_attributes)
return unless rate_attributes
rate_attributes.each do |index, attributes|
attributes[:rate] = Rate.clean_currency(attributes[:rate])
attributes[:project] = project
rates.build(attributes) if attributes[:rate].to_f > 0
end
end
def set_existing_rates (project, rate_attributes)
rates.reject(&:new_record?).reject{|r| r.project_id != project.id}.each do |rate|
attributes = rate_attributes[rate.id.to_s]
has_rate = false
if attributes && attributes[:rate]
attributes[:rate] = Rate.clean_currency(attributes[:rate])
has_rate = attributes[:rate].to_f > 0
end
if has_rate
rate.attributes = attributes
else
rates.delete(rate)
end
end
end
def save_rates
rates.each do |rate|
rate.save(false)
end
end
end
end