Created
March 30, 2011 13:58
-
-
Save Piotroslav/894436 to your computer and use it in GitHub Desktop.
application_controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Filters added to this controller apply to all controllers in the application. | |
| # Likewise, all the methods added will be available for all controllers. | |
| class ApplicationController < ActionController::Base | |
| helper :all # include all helpers, all the time | |
| protect_from_forgery # See ActionController::RequestForgeryProtection for details | |
| require 'ssl_requirement' | |
| include SslRequirement | |
| ssl_exceptions | |
| # Scrub sensitive parameters from your log | |
| filter_parameter_logging :password | |
| before_filter :login_required before_filter :login_required | |
| before_filter :check_pass_expires, :except => [:login, :forgot_password, | |
| :reset_password, :do_reset_password,:account] #, :change_password, :account, :check_pass_expires] | |
| def check_pass_expires | |
| if current_user.signed_up? | |
| timenow = Time.current.to_i | |
| passexp = current_user.pass_expires.to_i #get_expiration_date | |
| if passexp < timenow | |
| puts "Zupa pomidorowa" | |
| redirect_to user_account_path(current_user) | |
| end | |
| end | |
| puts timenow, current_user.role, current_user.pass_expires.to_i | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| role enum_string(:international, :sales, :resourcing, :playmaker, :commercial) | |
| email_address :email_address, :login => true | |
| administrator :boolean, :default => false | |
| timestamps | |
| pass_expires :time | |
| end | |
| has_many :calls | |
| has_many :companies | |
| has_many :projects | |
| has_many :leads | |
| has_many :signoffs | |
| has_many :todos | |
| 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 reincarnate_pass | |
| acting_user.pass_expires = Date.now + 6.months | |
| 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 | |
| super | |
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote method 'do_reset_password' so when transition 'reset_password' (app/models/users.rb) takes place it would update password expiration date
// transition :reset_password, { :active => :active }, :available_to => :key_holder, :params => [ :password, :password_confirmation ]
Method in app_controller is to forbid any activities unless new password is established