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.



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

def send_credentials
  @send_credentials
end

Class Method Details

.human_rolename(role) ⇒ Object



59
60
61
# File 'app/models/alchemy/user.rb', line 59

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

.logged_in_timeoutObject



63
64
65
# File 'app/models/alchemy/user.rb', line 63

def logged_in_timeout
  Config.get(:auto_logout_time).minutes.to_i
end

.ransackable_associations(_auth_object = nil) ⇒ Object



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

def ransackable_associations(_auth_object = nil)
  %w[
    taggings
    tags
  ]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object Also known as: searchable_alchemy_resource_attributes



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

def ransackable_attributes(_auth_object = nil)
  %w[
    email
    firstname
    lastname
    login
  ]
end

Instance Method Details

#add_role(role) ⇒ Object



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

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

#alchemy_rolesObject



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

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

#alchemy_roles=(roles_string) ⇒ Object



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

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.



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

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



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

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



150
151
152
153
154
# File 'app/models/alchemy/user.rb', line 150

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)


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

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)


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

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)


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

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



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

def role
  alchemy_roles.first
end

#role_symbolsObject



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

def role_symbols
  alchemy_roles.map(&:to_sym)
end

#store_request_time!Object



156
157
158
# File 'app/models/alchemy/user.rb', line 156

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