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,
  :timezone
]
ROLES =
Alchemy.config.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.



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

def send_credentials
  @send_credentials
end

Class Method Details

.human_rolename(role) ⇒ Object



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

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

.logged_in_timeoutObject



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

def logged_in_timeout
  Alchemy.config.get(:auto_logout_time).minutes.to_i
end

.ransackable_associations(_auth_object = nil) ⇒ Object



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

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

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



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

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

.ransortable_attributes(_auth_object = nil) ⇒ Object



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

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

Instance Method Details

#add_role(role) ⇒ Object



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

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

#alchemy_rolesObject



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

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

#alchemy_roles=(roles_string) ⇒ Object



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

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.



175
176
177
178
179
180
181
# File 'app/models/alchemy/user.rb', line 175

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

#email_required?Boolean

Returns:

  • (Boolean)


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

def email_required?
  ::Devise.authentication_keys.include?(:email)
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



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

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)


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

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

#human_roles_stringObject



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

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)


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

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)


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

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)


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

def logged_out?
  !logged_in?
end

#login_required?Boolean

Returns:

  • (Boolean)


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

def 
  ::Devise.authentication_keys.include?(:login)
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.



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

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

#roleObject



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

def role
  alchemy_roles.first
end

#role_symbolsObject



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

def role_symbols
  alchemy_roles.map(&:to_sym)
end

#store_request_time!Object



169
170
171
# File 'app/models/alchemy/user.rb', line 169

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

#unlock_pages!Object

Calls unlock on all locked pages



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

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