Class: Alchemy::User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Taggable
Defined in:
app/models/alchemy/user.rb

Constant Summary collapse

PERMITTED_ATTRIBUTES =
[
  :firstname,
  :lastname,
  :login,
  :email,
  :language,
  :password,
  :password_confirmation,
  :send_credentials,
  :tag_list
]
ROLES =
Config.get(:user_roles)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#send_credentialsObject

Returns the value of attribute send_credentials.



22
23
24
# File 'app/models/alchemy/user.rb', line 22

def send_credentials
  @send_credentials
end

Class Method Details

.human_rolename(role) ⇒ Object



39
40
41
# File 'app/models/alchemy/user.rb', line 39

def human_rolename(role)
  Alchemy.t("user_roles.#{role}")
end

.logged_in_timeoutObject



43
44
45
# File 'app/models/alchemy/user.rb', line 43

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



51
52
53
54
55
56
57
58
# File 'app/models/alchemy/user.rb', line 51

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



81
82
83
# File 'app/models/alchemy/user.rb', line 81

def add_role(role)
  self.alchemy_roles = self.alchemy_roles.push(role.to_s).uniq
end

#alchemy_rolesObject



69
70
71
# File 'app/models/alchemy/user.rb', line 69

def alchemy_roles
  read_attribute(:alchemy_roles).split(' ')
end

#alchemy_roles=(roles_string) ⇒ Object



73
74
75
76
77
78
79
# File 'app/models/alchemy/user.rb', line 73

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_mailObject

Delivers a welcome mail depending from user’s role.



152
153
154
155
156
157
158
# File 'app/models/alchemy/user.rb', line 152

def deliver_welcome_mail
  if has_role?('author') || has_role?('editor') || has_role?('admin')
    Notifications.alchemy_user_created(self).deliver_later
  else
    Notifications.member_created(self).deliver_later
  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

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :flipped (Object) — default: false

    Flip the firstname and lastname



117
118
119
120
121
122
123
124
125
# File 'app/models/alchemy/user.rb', line 117

def fullname(options = {})
  if lastname.blank? && firstname.blank?
    
  else
    options = {:flipped => false}.merge(options)
    fullname = options[:flipped] ? "#{lastname}, #{firstname}" : "#{firstname} #{lastname}"
    fullname.squeeze(" ").strip
  end
end

#has_role?(role) ⇒ Boolean

Returns true if the user has the given role.

Returns:

  • (Boolean)


92
93
94
# File 'app/models/alchemy/user.rb', line 92

def has_role?(role)
  alchemy_roles.include? role.to_s
end

#human_roles_stringObject



140
141
142
143
144
# File 'app/models/alchemy/user.rb', line 140

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

Returns:

  • (Boolean)


86
87
88
# File 'app/models/alchemy/user.rb', line 86

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

Returns:

  • (Boolean)


130
131
132
133
# File 'app/models/alchemy/user.rb', line 130

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?

Returns:

  • (Boolean)


136
137
138
# File 'app/models/alchemy/user.rb', line 136

def logged_out?
  !logged_in?
end

#pages_locked_by_meObject Also known as: locked_pages

Returns all pages locked by user.

A page gets locked, if the user requests to edit the page.



105
106
107
# File 'app/models/alchemy/user.rb', line 105

def pages_locked_by_me
  Page.locked_by(self).order(:updated_at)
end

#roleObject



65
66
67
# File 'app/models/alchemy/user.rb', line 65

def role
  alchemy_roles.first
end

#role_symbolsObject



61
62
63
# File 'app/models/alchemy/user.rb', line 61

def role_symbols
  alchemy_roles.map(&:to_sym)
end

#store_request_time!Object



146
147
148
# File 'app/models/alchemy/user.rb', line 146

def store_request_time!
  update_column(:last_request_at, Time.now)
end

#unlock_pages!Object

Calls unlock on all locked pages



97
98
99
# File 'app/models/alchemy/user.rb', line 97

def unlock_pages!
  pages_locked_by_me.map(&:unlock!)
end