Class: Vote
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Vote
- Includes:
- HasOwner
- 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
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
57 58 59 60 61 |
# File 'app/models/vote.rb', line 57 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
63 64 65 |
# File 'app/models/vote.rb', line 63 def self.creation_parameters %i[delta votable_id votable_type] end |
.page_for_administration(page = 1) ⇒ Object
33 34 35 |
# File 'app/models/vote.rb', line 33 def self.page_for_administration(page = 1) list_for_administration.page(page) end |
.slug_string(user, ip, agent_id) ⇒ Object
40 41 42 43 44 45 46 |
# File 'app/models/vote.rb', line 40 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
50 51 52 |
# File 'app/models/vote.rb', line 50 def self.voted?(slug, votable) exists?(slug: slug, votable: votable) end |
Instance Method Details
#current_slug ⇒ Object
80 81 82 |
# File 'app/models/vote.rb', line 80 def current_slug self.class.slug_string(user, ip, agent_id) end |
#downvote? ⇒ Boolean
71 72 73 |
# File 'app/models/vote.rb', line 71 def downvote? delta < 0 end |
#editable_by?(user) ⇒ Boolean
76 77 78 |
# File 'app/models/vote.rb', line 76 def editable_by?(user) owned_by?(user) || UserPrivilege.user_has_privilege?(:user, :moderator) end |
#upvote? ⇒ Boolean
67 68 69 |
# File 'app/models/vote.rb', line 67 def upvote? delta >= 0 end |