Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < ActiveRecord::Base
- hobo_user_model # Don't put anything above this
- has_paper_trail :ignore => [:key_timestamp]
- fields do
- name :string, :required, :unique
- nickname :string, :unique
- pass_expires :date
- role enum_string(:international, :sales, :resourcing, :playmaker, :commercial)
- email_address :email_address, :login => true
- administrator :boolean, :default => false
- timestamps
- end
- has_many :calls
- has_many :companies
- has_many :projects
- has_many :leads
- has_many :signoffs
- has_many :todos
- belongs_to :access_role
- set_default_order "name ASC"
- named_scope :administrators, :conditions => { :administrator => true }
- named_scope :decider, :conditions => { :role => "playmaker" }
- named_scope :resourcer, :conditions => { :role => "resourcing" }
- named_scope :selling, :conditions => "role = 'sales' or role = 'playmaker'"
- named_scope :show_calls, :from => "(select *, (select count(*) from calls where user_id=users.id and created_at >= '" + (Date.commercial(Date.today.year, Date.today.cweek, 1) - 11 * 7).strftime("%Y-%m-%d") + "' and result != 'nocall') as total_calls from users) users", :conditions => "total_calls > 0"
- named_scope :show_leads, :from => "(select *, (select count(*) from leads where user_id=users.id and created_at >= '" + (Date.commercial(Date.today.year, Date.today.cweek, 1) - 11 * 7).strftime("%Y-%m-%d") + "') as total_leads from users) users", :conditions => "total_leads > 0"
- named_scope :show_projects, :from => "(select *, (select count(*) from projects where user_id=users.id and created_at >= '" + (Date.commercial(Date.today.year, Date.today.cweek, 1) - 11 * 7).strftime("%Y-%m-%d") + "') as total_projects from users) users", :conditions => "total_projects > 0"
- def before_save()
- if changing_password? and password == password_confirmation and current_password != password
- reg = /^(?=.*\d)(?=.*([a-z]))(?=.*([A-Z]))([\x20-\x7E]){8,40}$/
- # puts password, password.length ,reg.match(password), "koniec"
- if reg.match(password)
- reincarnate_pass
- else
- errors.add(:password, Hobo::Translations.ht("hobo.messages.validate_password", :default => "must be at least 8 characters long and must consist of digits, lowercase and uppercase letters."))
- false
- end
- end
- if current_password == password && !current_password.nil?
- errors.add(:password, Hobo::Translations.ht("hobo.messages.validate_password", :default => "Your new password must differ from old one"))
- false
- end
- end
- def after_create
- self.update_attribute(:state, "active")
- end
- def reincarnate_pass
- acting_user.pass_expires = Date::today >> 3
- acting_user.save
- end
- def self.week_number(week)
- return (Date.today - 7 * (week - 1)).cweek
- end
- def get_week_number(week)
- return User.week_number(week)
- end
- def self.get_start_week(week)
- start = (Date.commercial(Date.today.year, Date.today.cweek, 1) - (week - 1) * 7).strftime("%Y-%m-%d")
- return start
- end
- def self.get_end_week(week)
- return User.get_start_week(week - 1)
- end
- def self.get_count(user, start, finish, table)
- user_command = user == nil ? '' : "user_id=" + user.to_s + " and "
- finish_command = finish == nil ? '' : " and created_at < '" + finish + "'"
- no_call_command = table == 'calls' ? " and result != 'nocall'" : ""
- return connection.select_value("select count(*) from " + table + " where " + user_command + "created_at >= '" + start + "'" + finish_command + no_call_command)
- end
- def total_calls
- return User.get_count(read_attribute('id'), User.get_start_week(12), nil, 'calls')
- end
- def total_leads
- return User.get_count(read_attribute('id'), User.get_start_week(12), nil, 'leads')
- end
- def total_projects
- return User.get_count(read_attribute('id'), User.get_start_week(12), nil, 'projects')
- end
- def calls_week(week)
- return User.get_count(read_attribute('id'), User.get_start_week(week), User.get_end_week(week), 'calls')
- end
- def leads_week(week)
- return User.get_count(read_attribute('id'), User.get_start_week(week), User.get_end_week(week), 'leads')
- end
- def projs_week(week)
- return User.get_count(read_attribute('id'), User.get_start_week(week), User.get_end_week(week), 'projects')
- end
- def all_calls_week(week)
- if(week == 0) #total
- return User.get_count(nil, User.get_start_week(12), nil, 'calls')
- end
- return User.get_count(nil, User.get_start_week(week), User.get_end_week(week), 'calls')
- end
- def all_leads_week(week)
- if(week == 0) #total
- return User.get_count(nil, User.get_start_week(12), nil, 'leads')
- end
- return User.get_count(nil, User.get_start_week(week), User.get_end_week(week), 'leads')
- end
- def all_projs_week(week)
- if(week == 0) #total
- return User.get_count(nil, User.get_start_week(12), nil, 'projects')
- end
- return User.get_count(nil, User.get_start_week(week), User.get_end_week(week), 'projects')
- end
- def method_missing(method, *args, &block)
- str = method.to_s
- if(str.length < 11)
- return super
- end
- if(str[0,10] == 'calls_week')
- week = str[10, str.length - 10].to_i
- return super if (week == nil)
- return calls_week(week)
- end
- if(str[0,10] == 'leads_week')
- week = str[10, str.length - 10].to_i
- return super if (week == nil)
- return leads_week(week)
- end
- if(str[0,10] == 'projs_week')
- week = str[10, str.length - 10].to_i
- return super if (week == nil)
- return projs_week(week)
- end
- end
- def self.generate_projects_graph
- return User.generate_graph('projects')
- end
- def self.generate_leads_graph
- return User.generate_graph('leads')
- end
- def self.generate_calls_graph
- return User.generate_graph('calls')
- end
- def self.generate_graph(table)
- no_call_command = table == 'calls' ? " and result != 'nocall'" : ""
- ids = connection.select_values("select id from (select *, (select count(*) from " + table + " where user_id = users.id and created_at >= '" + User.get_start_week(12) + "'" + no_call_command + ") as total from users order by name) users where total > 0")
- title = Title.new(table.capitalize)
- chart = OpenFlashChart.new
- bar_stack = BarStack.new
- max = 1
- r = 3
- g = 1
- b = 2
- colors = []
- 0.upto(ids.length - 1) {
- rs = r.to_s(16)
- gs = g.to_s(16)
- bs = b.to_s(16)
- rs = '0' + rs if (rs.length == 1)
- gs = '0' + gs if (gs.length == 1)
- bs = '0' + bs if (bs.length == 1)
- colors << rs + gs + bs
- r = (r + 93) & 0xFF
- g = (g + 111) & 0xFF
- b = (b + 49) & 0xFF
- }
- 12.downto(1) { |k|
- data = []
- bar_total = 0
- 0.upto(ids.length - 1){ |i|
- v = User.get_count(ids[i], User.get_start_week(k), User.get_end_week(k), table).to_i
- if v > 0
- data << BarStackValue.new(v, colors[i])
- bar_total += v
- end
- }
- if bar_total > max
- max = bar_total
- end
- bar_stack.append_stack(Array.new(data))
- }
- y = YAxis.new
- y.set_range(0, max + 1, 5)
- x = XAxis.new
- weeks = []
- 12.downto(1){ |k|
- weeks << 'w' + User.week_number(k).to_s
- }
- x.set_labels(weeks)
- x_legend = XLegend.new("week")
- x_legend.set_style('{font-size: 20px; color: #000000}')
- y_legend = YLegend.new(table.to_s + " count")
- y_legend.set_style('{font-size: 20px; color: #000000}')
- tooltip = Tooltip.new;
- tooltip.set_hover();
- bar_stack.set_tooltip( '#x_label# [#val#]<br>Total [#total#]' );
- chart.add_element(bar_stack)
- chart.set_title(title)
- chart.set_x_legend(x_legend)
- chart.set_y_legend(y_legend)
- chart.y_axis = y
- chart.set_x_axis(x)
- chart.set_tooltip( tooltip );
- 0.upto(ids.length - 1) { |i|
- line = Line.new
- line.set_key(connection.select_values("select name from users where id = " + ids[i].to_s), 15)
- line.colour = colors[i]
- chart.add_element(line)
- }
- return chart
- end
- # --- Signup lifecycle --- #
- lifecycle do
- state :inactive, :active
- transition :request_password_reset, { :active => :active }, :new_key => true do
- UserMailer.deliver_forgot_password(self, lifecycle.key)
- end
- transition :reset_password, { :active => :active }, :available_to => :key_holder,
- :params => [ :password, :password_confirmation ] do
- #after_save :reincarnate_pass
- #puts "barszcz z grzybami"
- end
- transition :deactivate_user, { :active => :inactive }, :available_to => :all
- transition :deactivate_user, { :inactive => :active }, :available_to => :all
- end
- # --- Permissions --- #
- def create_permitted?
- acting_user.administrator?
- end
- def update_permitted?
- acting_user.administrator? ||
- (acting_user == self && only_changed?(:email_address, :crypted_password,
- :current_password, :password, :password_confirmation))
- # Note: crypted_password has attr_protected so although it is permitted to change, it cannot be changed
- # directly from a form submission.
- end
- def destroy_permitted?
- acting_user.administrator?
- end
- def view_permitted?(field)
- true
- end
- end
Add Comment
Please, Sign In to add comment