Add vote functionality to tracks
This commit is contained in:
parent
8b2f2b538e
commit
92b09c7de4
18 changed files with 237 additions and 19 deletions
|
|
@ -47,8 +47,8 @@ module Admin
|
|||
@versions = @event.versions |
|
||||
PaperTrail::Version.where(item_type: 'Commercial').where('object LIKE ?', "%commercialable_id: #{@event.id}\ncommercialable_type: Event%") |
|
||||
PaperTrail::Version.where(item_type: 'Commercial').where('object_changes LIKE ?', "%commercialable_id:\n- \n- #{@event.id}\ncommercialable_type:\n- \n- Event%") |
|
||||
PaperTrail::Version.where(item_type: 'Vote').where('object_changes LIKE ?', "%\nevent_id:\n- \n- #{@event.id}\n%") |
|
||||
PaperTrail::Version.where(item_type: 'Vote').where('object LIKE ?', "%\nevent_id: #{@event.id}\n%")
|
||||
PaperTrail::Version.where(item_type: 'Vote').where('object_changes LIKE ?', "%votable_type:\n- \n- Event\nvotable_id:\n- \n- #{@event.id}%") |
|
||||
PaperTrail::Version.where(item_type: 'Vote').where('object LIKE ?', "%votable_type: Event\nvotable_id: #{@event.id}%")
|
||||
end
|
||||
|
||||
def edit
|
||||
|
|
@ -141,7 +141,7 @@ module Admin
|
|||
def vote
|
||||
@votes = @event.votes.includes(:user)
|
||||
|
||||
if (votes = current_user.votes.find_by_event_id(params[:id]))
|
||||
if (votes = current_user.votes.find_by(votable: @event))
|
||||
votes.update_attributes(rating: params[:rating])
|
||||
else
|
||||
@myvote = @event.votes.build
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ module Admin
|
|||
end
|
||||
|
||||
def show
|
||||
@votes = @track.votes.includes(:user)
|
||||
respond_to do |format|
|
||||
format.html { render }
|
||||
format.json { render json: @conference.tracks.to_json }
|
||||
|
|
@ -65,6 +66,24 @@ module Admin
|
|||
end
|
||||
end
|
||||
|
||||
def vote
|
||||
@votes = @track.votes.includes(:user)
|
||||
|
||||
if (votes = current_user.votes.find_by(votable: @track))
|
||||
votes.update_attributes(rating: params[:rating])
|
||||
else
|
||||
@myvote = @track.votes.build
|
||||
@myvote.user = current_user
|
||||
@myvote.rating = params[:rating]
|
||||
@myvote.save
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to admin_conference_program_track_path(@conference.short_title, @track) }
|
||||
format.js
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
if @track.destroy
|
||||
redirect_to admin_conference_program_tracks_path(conference_id: @conference.short_title),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
12
app/views/admin/tracks/_datatable_row_rating.haml
Normal file
12
app/views/admin/tracks/_datatable_row_rating.haml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
- if show_votes
|
||||
%div{ data: { toggle: 'tooltip' }, title: rating_tooltip(track, max_rating) }<
|
||||
= rating_stars(track.average_rating, max_rating, avgrate: true)
|
||||
|
||||
.clearfix
|
||||
- if track.voted?(current_user)
|
||||
%span.label.label-success
|
||||
You voted:
|
||||
= rating_fraction(track.user_rating(current_user), max_rating)
|
||||
- else
|
||||
%span.label.label-danger
|
||||
Not rated
|
||||
62
app/views/admin/tracks/_voting.html.haml
Normal file
62
app/views/admin/tracks/_voting.html.haml
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
%table.table#myrating
|
||||
- if show_votes
|
||||
%tr
|
||||
%td.col-md-2
|
||||
%b Rating
|
||||
%td
|
||||
#{track.average_rating}/#{max_rating}
|
||||
= rating_stars(track.average_rating, max_rating, avgrate: true)
|
||||
%tr
|
||||
%td
|
||||
%b Voters
|
||||
%td
|
||||
= votes.length
|
||||
- if votes.present?
|
||||
(
|
||||
= votes.collect(&:name).to_sentence
|
||||
)
|
||||
%tr
|
||||
%td.col-md-2
|
||||
%b Your vote
|
||||
%td
|
||||
- if voting_period
|
||||
- max_rating.times do |counter|
|
||||
- if track.user_rating(current_user) > counter
|
||||
= link_to '', vote_admin_conference_program_track_path(conference_id, track, rating: counter + 1),
|
||||
remote: true,
|
||||
id: "label#{counter + 1}",
|
||||
class: 'rating myrating bright',
|
||||
voted: true
|
||||
- else
|
||||
= link_to '', vote_admin_conference_program_track_path(conference_id, track, rating: counter + 1),
|
||||
remote: true,
|
||||
id: "label#{counter + 1}",
|
||||
class: 'rating myrating'
|
||||
- else
|
||||
= rating_stars(track.user_rating(current_user), max_rating, voted: true)
|
||||
(Voting period is closed)
|
||||
|
||||
- if show_votes
|
||||
- if votes.present?
|
||||
- votes.each do |vote|
|
||||
- unless vote.user_id == current_user.id
|
||||
%tr
|
||||
%td
|
||||
= vote.name
|
||||
%td
|
||||
= rating_stars(vote.rating, max_rating)
|
||||
|
||||
:javascript
|
||||
$(".myrating").hover(
|
||||
function() { // mouseover
|
||||
$(this).prevAll().andSelf().addClass('glow');
|
||||
},
|
||||
function() { // mouseout
|
||||
$(this).siblings().andSelf().removeClass('glow');
|
||||
}
|
||||
);
|
||||
|
||||
$(".myrating").click(function() {
|
||||
$(this).siblings().removeClass("bright");
|
||||
$(this).prevAll().andSelf().addClass("bright");
|
||||
});
|
||||
|
|
@ -37,6 +37,9 @@
|
|||
%thead
|
||||
%th ID
|
||||
%th Name
|
||||
- if @program.rating_enabled?
|
||||
%th
|
||||
%b Rating
|
||||
%th Description
|
||||
%th Room
|
||||
%th Start Date
|
||||
|
|
@ -54,6 +57,12 @@
|
|||
= link_to admin_conference_program_track_path(@conference.short_title, track), class: 'btn' do
|
||||
%span.label{style: "background-color: #{track.color}; color: #{ contrast_color(track.color) }"}
|
||||
= track.name
|
||||
- if @program.rating_enabled?
|
||||
%td.col-md-1{ data: { order: track.average_rating } }
|
||||
= render 'datatable_row_rating',
|
||||
track: track,
|
||||
show_votes: @program.show_voting?,
|
||||
max_rating: @program.rating
|
||||
%td
|
||||
%p
|
||||
= markdown(truncate(track.description))
|
||||
|
|
|
|||
|
|
@ -104,6 +104,14 @@
|
|||
%b Relevance
|
||||
%td
|
||||
= markdown(@track.relevance)
|
||||
- if @conference.program.rating_enabled?
|
||||
= render 'voting',
|
||||
track: @track,
|
||||
show_votes: @program.show_voting?,
|
||||
max_rating: @program.rating,
|
||||
voting_period: @program.voting_period?,
|
||||
votes: @votes,
|
||||
conference_id: @conference.short_title
|
||||
|
||||
.tab-pane#events
|
||||
.col-md-12
|
||||
|
|
|
|||
10
app/views/admin/tracks/vote.js.erb
Normal file
10
app/views/admin/tracks/vote.js.erb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
$('table#myrating').replaceWith(
|
||||
"<%= escape_javascript(render 'voting', \
|
||||
track: @track, \
|
||||
show_votes: @program.show_voting?, \
|
||||
max_rating: @program.rating, \
|
||||
voting_period: @program.voting_period?, \
|
||||
votes: @votes, \
|
||||
conference_id: @conference.short_title \
|
||||
) %>"
|
||||
);
|
||||
|
|
@ -70,7 +70,7 @@
|
|||
|
||||
- when 'EventsRegistration', 'Comment', 'Vote', 'Event'
|
||||
event
|
||||
- event_id = object.try(:event_id) || object.try(:commentable_id) || object.id
|
||||
- event_id = object.try(:votable_id) || object.try(:event_id) || object.try(:commentable_id) || object.id
|
||||
= link_to (current_or_last_object_state('Event', event_id).try(:title) || 'deleted event'),
|
||||
admin_conference_program_event_path(conference_short_title, event_id)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue