Class: User
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- User
- Includes:
- RailsAdmin
- Defined in:
- app/models/user.rb
Class Method Summary collapse
- .last_signins(count) ⇒ Object
- .last_signups(count) ⇒ Object
- .paged(page_number) ⇒ Object
- .search_and_order(search, page_number) ⇒ Object
- .users_count ⇒ Object
Instance Method Summary collapse
- #has_role?(role) ⇒ Boolean
- #roles ⇒ Object
-
#roles_enum ⇒ Object
serialize :roles, Array if Settings.single_role_per_user == âfâ.
- #timeout_in ⇒ Object
- #title ⇒ Object
Class Method Details
.last_signins(count) ⇒ Object
46 47 48 49 |
# File 'app/models/user.rb', line 46 def self.last_signins(count) order(last_sign_in_at: :desc).limit(count).select("id","username","last_sign_in_at") end |
.last_signups(count) ⇒ Object
42 43 44 |
# File 'app/models/user.rb', line 42 def self.last_signups(count) order(created_at: :desc).limit(count).select("id","username","created_at") end |
.paged(page_number) ⇒ Object
28 29 30 |
# File 'app/models/user.rb', line 28 def self.paged(page_number) order(admin: :desc, username: :asc).page page_number end |
.search_and_order(search, page_number) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'app/models/user.rb', line 32 def self.search_and_order(search, page_number) if search where("username LIKE ?", "%#{search.downcase}%").order( admin: :desc, username: :asc ).page page_number else order(admin: :desc, username: :asc).page page_number end end |
.users_count ⇒ Object
51 52 53 |
# File 'app/models/user.rb', line 51 def self.users_count where(admin: false, locked: false).count end |
Instance Method Details
#has_role?(role) ⇒ Boolean
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/models/user.rb', line 69 def has_role? role # example called from cancan's app/models/ability.rb # if user.has_role? :admin # for roles array stored in db... take each value, see if it matches the second column in the roles_enum array, if so, retu the 1st col of the enum as a uprcase,space_to_underscore,symbol . begin # roles array (enum) chosen_roles = self.roles.map { |r| r.blank? ? nil : ROLES[r.to_i - 1] } chosen_roles.compact.include? role rescue # single role # ROLES[self.roles.to_i - 1] == role Rails.logger.debug "ROLES: #{self.roles.to_s}" self.roles.to_s == role.to_s end end |
#roles ⇒ Object
86 87 88 |
# File 'app/models/user.rb', line 86 def roles ROLES[super.to_i - 1] end |
#roles_enum ⇒ Object
serialize :roles, Array if Settings.single_role_per_user == âfâ
65 66 67 68 |
# File 'app/models/user.rb', line 65 def roles_enum # Do not EDIT below this line ROLES.each_with_index.map {|a,i| [I18n.t("roles.#{a.to_sym}"), (i+1).to_s]} end |
#timeout_in ⇒ Object
55 56 57 58 |
# File 'app/models/user.rb', line 55 def timeout_in return 30.minutes if admin? 1.day end |
#title ⇒ Object
60 61 62 |
# File 'app/models/user.rb', line 60 def title username end |