Class: Vote
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Vote
- Includes:
- HasOwner, HasUuid
- Defined in:
- app/models/vote.rb
Overview
Vote
Attributes:
agent_id [Agent], optional
created_at [DateTime]
delta [Integer]
ip [Inet], optional
slug [String]
updated_at [DateTime]
user_id [User], optional
uuid [UUID]
votable_id [Integer]
votable_type [String]
Class Method Summary collapse
- .count_vote(user, votable, delta = 1) ⇒ Object
- .creation_parameters ⇒ Object
- .page_for_administration(page = 1) ⇒ Object
- .slug_string(user, ip, agent_id) ⇒ Object
- .voted?(slug, votable) ⇒ Boolean
Instance Method Summary collapse
Class Method Details
.count_vote(user, votable, delta = 1) ⇒ Object
59 60 61 62 63 |
# File 'app/models/vote.rb', line 59 def self.count_vote(user, votable, delta = 1) create(user: user, votable: votable, delta: delta) unless voted? votable.reload votable.vote_result end |
.creation_parameters ⇒ Object
65 66 67 |
# File 'app/models/vote.rb', line 65 def self.creation_parameters %i[delta votable_id votable_type] end |
.page_for_administration(page = 1) ⇒ Object
35 36 37 |
# File 'app/models/vote.rb', line 35 def self.page_for_administration(page = 1) list_for_administration.page(page) end |
.slug_string(user, ip, agent_id) ⇒ Object
42 43 44 45 46 47 48 |
# File 'app/models/vote.rb', line 42 def self.slug_string(user, ip, agent_id) if user.nil? "#{ip}:#{agent_id}" else user.id.to_s end end |
.voted?(slug, votable) ⇒ Boolean
52 53 54 |
# File 'app/models/vote.rb', line 52 def self.voted?(slug, votable) exists?(slug: slug, votable: votable) end |
Instance Method Details
#current_slug ⇒ Object
82 83 84 |
# File 'app/models/vote.rb', line 82 def current_slug self.class.slug_string(user, ip, agent_id) end |
#downvote? ⇒ Boolean
73 74 75 |
# File 'app/models/vote.rb', line 73 def downvote? delta < 0 end |
#editable_by?(user) ⇒ Boolean
78 79 80 |
# File 'app/models/vote.rb', line 78 def editable_by?(user) owned_by?(user) || user&.super_user? end |
#upvote? ⇒ Boolean
69 70 71 |
# File 'app/models/vote.rb', line 69 def upvote? delta >= 0 end |