Class: Alchemy::User
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Alchemy::User
- Defined in:
- app/models/alchemy/user.rb
Constant Summary collapse
- PERMITTED_ATTRIBUTES =
[ :firstname, :lastname, :login, :email, :gender, :language, :password, :password_confirmation, :send_credentials, :tag_list ]
- DEVISE_MODULES =
[ :database_authenticatable, :trackable, :validatable, :timeoutable, :recoverable ]
- ROLES =
Config.get(:user_roles)
Instance Attribute Summary collapse
-
#send_credentials ⇒ Object
Returns the value of attribute send_credentials.
Class Method Summary collapse
- .genders_for_select ⇒ Object
- .human_rolename(role) ⇒ Object
- .logged_in_timeout ⇒ Object
-
.search(query) ⇒ Object
Search users that match query.
Instance Method Summary collapse
- #add_role(role) ⇒ Object
- #alchemy_roles ⇒ Object
- #alchemy_roles=(roles_string) ⇒ Object
-
#deliver_welcome_mail ⇒ Object
Delivers a welcome mail depending from user’s role.
-
#fullname(options = {}) ⇒ Object
(also: #name, #alchemy_display_name)
Returns the firstname and lastname as a string.
-
#has_role?(role) ⇒ Boolean
Returns true if the user has the given role.
- #human_roles_string ⇒ Object
-
#is_admin? ⇒ Boolean
(also: #admin?)
Returns true if the user ahs admin role.
-
#logged_in? ⇒ Boolean
Returns true if the last request not longer ago then the logged_in_time_out.
-
#logged_out? ⇒ Boolean
Opposite of logged_in?.
-
#pages_locked_by_me ⇒ Object
(also: #locked_pages)
Returns all pages locked by user.
- #role ⇒ Object
- #role_symbols ⇒ Object
- #store_request_time! ⇒ Object
-
#unlock_pages! ⇒ Object
Calls unlock on all locked pages.
Instance Attribute Details
#send_credentials ⇒ Object
Returns the value of attribute send_credentials.
36 37 38 |
# File 'app/models/alchemy/user.rb', line 36 def send_credentials @send_credentials end |
Class Method Details
.genders_for_select ⇒ Object
57 58 59 60 61 62 |
# File 'app/models/alchemy/user.rb', line 57 def genders_for_select [ [I18n.t('male'), 'male'], [I18n.t('female'), 'female'] ] end |
.human_rolename(role) ⇒ Object
53 54 55 |
# File 'app/models/alchemy/user.rb', line 53 def human_rolename(role) I18n.t("user_roles.#{role}") end |
.logged_in_timeout ⇒ Object
64 65 66 |
# File 'app/models/alchemy/user.rb', line 64 def logged_in_timeout Config.get(:auto_logout_time).minutes.to_i end |
.search(query) ⇒ Object
Search users that match query
Attributes searched are: login, email, firstname, lastname
72 73 74 75 76 77 78 79 |
# File 'app/models/alchemy/user.rb', line 72 def search(query) query = "%#{query.downcase}%" where arel_table[:login].lower.matches(query) .or arel_table[:email].lower.matches(query) .or arel_table[:firstname].lower.matches(query) .or arel_table[:lastname].lower.matches(query) end |
Instance Method Details
#add_role(role) ⇒ Object
102 103 104 |
# File 'app/models/alchemy/user.rb', line 102 def add_role(role) self.alchemy_roles = self.alchemy_roles.push(role.to_s).uniq end |
#alchemy_roles ⇒ Object
90 91 92 |
# File 'app/models/alchemy/user.rb', line 90 def alchemy_roles read_attribute(:alchemy_roles).split(' ') end |
#alchemy_roles=(roles_string) ⇒ Object
94 95 96 97 98 99 100 |
# File 'app/models/alchemy/user.rb', line 94 def alchemy_roles=(roles_string) if roles_string.is_a? Array write_attribute(:alchemy_roles, roles_string.join(' ')) elsif roles_string.is_a? String write_attribute(:alchemy_roles, roles_string) end end |
#deliver_welcome_mail ⇒ Object
Delivers a welcome mail depending from user’s role.
173 174 175 176 177 178 179 |
# File 'app/models/alchemy/user.rb', line 173 def deliver_welcome_mail if has_role?('author') || has_role?('editor') || has_role?('admin') Notifications.alchemy_user_created(self).deliver else Notifications.member_created(self).deliver end end |
#fullname(options = {}) ⇒ Object Also known as: name, alchemy_display_name
Returns the firstname and lastname as a string
If both are blank, returns the login
138 139 140 141 142 143 144 145 146 |
# File 'app/models/alchemy/user.rb', line 138 def fullname( = {}) if lastname.blank? && firstname.blank? login else = {:flipped => false}.merge() fullname = [:flipped] ? "#{lastname}, #{firstname}" : "#{firstname} #{lastname}" fullname.squeeze(" ").strip end end |
#has_role?(role) ⇒ Boolean
Returns true if the user has the given role.
113 114 115 |
# File 'app/models/alchemy/user.rb', line 113 def has_role?(role) alchemy_roles.include? role.to_s end |
#human_roles_string ⇒ Object
161 162 163 164 165 |
# File 'app/models/alchemy/user.rb', line 161 def human_roles_string alchemy_roles.map do |role| self.class.human_rolename(role) end.to_sentence end |
#is_admin? ⇒ Boolean Also known as: admin?
Returns true if the user ahs admin role
107 108 109 |
# File 'app/models/alchemy/user.rb', line 107 def is_admin? has_role? 'admin' end |
#logged_in? ⇒ Boolean
Returns true if the last request not longer ago then the logged_in_time_out
151 152 153 154 |
# File 'app/models/alchemy/user.rb', line 151 def logged_in? raise "Can not determine the records login state because there is no last_request_at column" if !respond_to?(:last_request_at) !last_request_at.nil? && last_request_at > logged_in_timeout.seconds.ago end |
#logged_out? ⇒ Boolean
Opposite of logged_in?
157 158 159 |
# File 'app/models/alchemy/user.rb', line 157 def logged_out? !logged_in? end |
#pages_locked_by_me ⇒ Object Also known as: locked_pages
Returns all pages locked by user.
A page gets locked, if the user requests to edit the page.
126 127 128 |
# File 'app/models/alchemy/user.rb', line 126 def pages_locked_by_me Page.where(:locked => true).where(:locked_by => self.id).order(:updated_at) end |
#role ⇒ Object
86 87 88 |
# File 'app/models/alchemy/user.rb', line 86 def role alchemy_roles.first end |
#role_symbols ⇒ Object
82 83 84 |
# File 'app/models/alchemy/user.rb', line 82 def role_symbols alchemy_roles.map(&:to_sym) end |
#store_request_time! ⇒ Object
167 168 169 |
# File 'app/models/alchemy/user.rb', line 167 def store_request_time! update_column(:last_request_at, Time.now) end |
#unlock_pages! ⇒ Object
Calls unlock on all locked pages
118 119 120 |
# File 'app/models/alchemy/user.rb', line 118 def unlock_pages! pages_locked_by_me.map(&:unlock!) end |