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



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

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

.logged_in_timeoutObject



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

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

.ransackable_associations(_auth_object = nil) ⇒ Object



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

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

.ransortable_attributes(_auth_object = nil) ⇒ Object



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

def ransortable_attributes(_auth_object = nil)
  %w[last_sign_in_at]
end

Instance Method Details

#add_role(role) ⇒ Object



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

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

#alchemy_rolesObject



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

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

#alchemy_roles=(roles_string) ⇒ Object



84
85
86
87
88
89
90
# File 'app/models/alchemy/user.rb', line 84

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.



166
167
168
169
170
171
172
# File 'app/models/alchemy/user.rb', line 166

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



130
131
132
133
134
135
136
137
138
# File 'app/models/alchemy/user.rb', line 130

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)


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

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

#human_roles_stringObject



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

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)


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

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)


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

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)


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

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.



117
118
119
# File 'app/models/alchemy/user.rb', line 117

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

#roleObject



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

def role
  alchemy_roles.first
end

#role_symbolsObject



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

def role_symbols
  alchemy_roles.map(&:to_sym)
end

#store_request_time!Object



160
161
162
# File 'app/models/alchemy/user.rb', line 160

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

#unlock_pages!Object

Calls unlock on all locked pages



109
110
111
# File 'app/models/alchemy/user.rb', line 109

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