Class: Vote

Inherits:
ApplicationRecord show all
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

Instance Method Summary collapse

Class Method Details

.count_vote(user, votable, delta = 1) ⇒ Object

Parameters:



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_parametersObject



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

Parameters:

  • page (Integer) (defaults to: 1)


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

Parameters:

  • user (User)
  • ip (String)
  • agent_id (Integer)


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

Parameters:

Returns:

  • (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_slugObject



82
83
84
# File 'app/models/vote.rb', line 82

def current_slug
  self.class.slug_string(user, ip, agent_id)
end

#downvote?Boolean

Returns:

  • (Boolean)


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

def downvote?
  delta < 0
end

#editable_by?(user) ⇒ Boolean

Parameters:

  • user (User)

Returns:

  • (Boolean)


78
79
80
# File 'app/models/vote.rb', line 78

def editable_by?(user)
  owned_by?(user) || user&.super_user?
end

#upvote?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/models/vote.rb', line 69

def upvote?
  delta >= 0
end