Add vote functionality to tracks

This commit is contained in:
Rishabh Singh 2019-08-09 17:33:42 +05:30
parent 8b2f2b538e
commit 92b09c7de4
18 changed files with 237 additions and 19 deletions

View file

@ -18,7 +18,7 @@ class Event < ApplicationRecord
has_one :submitter_event_user, -> { where(event_role: 'submitter') }, class_name: 'EventUser'
has_one :submitter, through: :submitter_event_user, source: :user
has_many :votes, dependent: :destroy
has_many :votes, as: :votable
has_many :voters, through: :votes, source: :user
has_many :commercials, as: :commercialable, dependent: :destroy
has_many :surveys, as: :surveyable, dependent: :destroy

View file

@ -15,6 +15,8 @@ class Track < ApplicationRecord
has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id }
has_many :votes, as: :votable
has_many :voters, through: :votes, source: :user
before_create :generate_guid
validates :name, presence: true
validates :color, format: /\A#[0-9A-F]{6}\z/
@ -100,6 +102,25 @@ class Track < ApplicationRecord
short_name
end
def user_rating(user)
(vote = votes.find_by(user: user)) ? vote.rating : 0
end
def voted?(user = nil)
return votes.where(user: user).any? if user
votes.any?
end
def average_rating
@total_rating = 0
votes.each do |vote|
@total_rating += vote.rating
end
@total = votes.size
@total_rating.positive? ? number_with_precision(@total_rating / @total.to_f, precision: 2, strip_insignificant_zeros: true) : 0
end
def transition_possible?(transition)
self.class.state_machine.events_for(current_state).include?(transition)
end

View file

@ -2,7 +2,7 @@
class Vote < ApplicationRecord
belongs_to :user
belongs_to :event
belongs_to :votable, polymorphic: true
has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id }
@ -11,6 +11,6 @@ class Vote < ApplicationRecord
private
def conference_id
event.program.conference_id
votable.program.conference_id
end
end