Class: Token
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Token
- Includes:
- HasOwner, Toggleable
- Defined in:
- app/models/token.rb
Constant Summary collapse
- PER_PAGE =
25
Class Method Summary collapse
- .creation_parameters ⇒ Object
- .entity_parameters ⇒ Object
- .from_cookie(input) ⇒ Object
- .page_for_administration(page, filter = {}) ⇒ Object
- .page_for_owner(user, page) ⇒ Object
- .user_by_pair(user_id, token, touch_user = false) ⇒ Object
- .user_by_token(input, touch_user = false) ⇒ Object
Instance Method Summary collapse
Methods included from HasOwner
Class Method Details
.creation_parameters ⇒ Object
35 36 37 |
# File 'app/models/token.rb', line 35 def self.creation_parameters entity_parameters + %i(user_id) end |
.entity_parameters ⇒ Object
31 32 33 |
# File 'app/models/token.rb', line 31 def self.entity_parameters %i(active) end |
.from_cookie(input) ⇒ Object
40 41 42 |
# File 'app/models/token.rb', line 40 def self.(input) find_by(token: input.to_s.split(':')[1].to_s) end |
.page_for_administration(page, filter = {}) ⇒ Object
21 22 23 |
# File 'app/models/token.rb', line 21 def self.page_for_administration(page, filter = {}) filtered(filter).recent.page(page).per(PER_PAGE) end |
.page_for_owner(user, page) ⇒ Object
27 28 29 |
# File 'app/models/token.rb', line 27 def self.page_for_owner(user, page) owned_by(user).recent.page(page).per(PER_PAGE) end |
.user_by_pair(user_id, token, touch_user = false) ⇒ Object
55 56 57 58 59 60 61 |
# File 'app/models/token.rb', line 55 def self.user_by_pair(user_id, token, touch_user = false) instance = find_by(user_id: user_id, token: token, active: true) return if instance.nil? instance.update_columns(last_used: Time.now) instance.user.update_columns(last_seen: Time.now) if touch_user instance.user end |
.user_by_token(input, touch_user = false) ⇒ Object
46 47 48 49 50 |
# File 'app/models/token.rb', line 46 def self.user_by_token(input, touch_user = false) return if input.blank? pair = input.split(':') user_by_pair(pair[0], pair[1], touch_user) end |
Instance Method Details
#cookie_pair ⇒ Object
72 73 74 |
# File 'app/models/token.rb', line 72 def "#{user_id}:#{token}" end |
#editable_by?(user) ⇒ Boolean
68 69 70 |
# File 'app/models/token.rb', line 68 def editable_by?(user) owned_by?(user) || UserPrivilege.user_has_privilege?(user, :administrator) end |
#name ⇒ Object
63 64 65 |
# File 'app/models/token.rb', line 63 def name "[#{id}] #{user.profile_name}" end |