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,
  :gender,
  :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.



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

def send_credentials
  @send_credentials
end

Class Method Details

.genders_for_selectObject



44
45
46
47
48
49
# File 'app/models/alchemy/user.rb', line 44

def genders_for_select
  [
    [Alchemy.t('male'), 'male'],
    [Alchemy.t('female'), 'female']
  ]
end

.human_rolename(role) ⇒ Object



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

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

.logged_in_timeoutObject



51
52
53
# File 'app/models/alchemy/user.rb', line 51

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



59
60
61
62
63
64
65
66
# File 'app/models/alchemy/user.rb', line 59

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



89
90
91
# File 'app/models/alchemy/user.rb', line 89

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

#alchemy_rolesObject



77
78
79
# File 'app/models/alchemy/user.rb', line 77

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

#alchemy_roles=(roles_string) ⇒ Object



81
82
83
84
85
86
87
# File 'app/models/alchemy/user.rb', line 81

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.



160
161
162
163
164
165
166
# File 'app/models/alchemy/user.rb', line 160

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



125
126
127
128
129
130
131
132
133
# File 'app/models/alchemy/user.rb', line 125

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)


100
101
102
# File 'app/models/alchemy/user.rb', line 100

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

#human_roles_stringObject



148
149
150
151
152
# File 'app/models/alchemy/user.rb', line 148

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)


94
95
96
# File 'app/models/alchemy/user.rb', line 94

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)


138
139
140
141
# File 'app/models/alchemy/user.rb', line 138

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)


144
145
146
# File 'app/models/alchemy/user.rb', line 144

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.



113
114
115
# File 'app/models/alchemy/user.rb', line 113

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

#roleObject



73
74
75
# File 'app/models/alchemy/user.rb', line 73

def role
  alchemy_roles.first
end

#role_symbolsObject



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

def role_symbols
  alchemy_roles.map(&:to_sym)
end

#store_request_time!Object



154
155
156
# File 'app/models/alchemy/user.rb', line 154

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

#unlock_pages!Object

Calls unlock on all locked pages



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

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