Clean up rating partials

This commit is contained in:
James Mason 2018-02-09 17:19:33 -08:00
parent 73c5c3c97a
commit 9e10678542
10 changed files with 131 additions and 92 deletions

View file

@ -1,19 +1,16 @@
/* Styling for voting on proposals*/
.myrating.bright { background-image: image-url("star-bright.png"); }
.myrating.glow { background-image: image-url("star-glow.png"); }
.othersrating.bright { background-image: image-url("star-bright.png"); }
.avgrating.bright { background-image: image-url("star-bright.png"); }
.avgrating {
.rating {
background: image-url("star.png") 0 0;
margin-right: -2px;
width: 24px;
height: 24px;
display: inline-block;
}
.myrating, .othersrating {
background: image-url("star.png") 0 0;
width: 24px;
height: 24px;
float: left;
}
&.bright {
background-image: image-url("star-bright.png");
}
&.glow {
background-image: image-url("star-glow.png");
}
}

View file

@ -46,7 +46,7 @@ module Admin
@event_types = @program.event_types
@comments = @event.root_comments
@comment_count = @event.comment_threads.count
@ratings = @event.votes.includes(:user)
@votes = @event.votes.includes(:user)
@difficulty_levels = @program.difficulty_levels
@versions = @event.versions |
PaperTrail::Version.where(item_type: 'Commercial').where('object LIKE ?', "%commercialable_id: #{@event.id}\ncommercialable_type: Event%") |
@ -146,7 +146,7 @@ module Admin
end
def vote
@ratings = @event.votes.includes(:user)
@votes = @event.votes.includes(:user)
if (votes = current_user.votes.find_by_event_id(params[:id]))
votes.update_attributes(rating: params[:rating])

View file

@ -10,16 +10,19 @@ module EventsHelper
"Registered: #{event.registrations.count}"
end
##
# Checks if the voting has already started, or if it has already ended
#
def voting_open_or_close(program)
return if program.voting_period?
if program.voting_start_date > Time.current
return 'Voting period has not started yet!'
else # voting_end_date > Date.today because voting_start_date < voting_end_date
return 'Voting period is over!'
end
def rating_stars(rating, max, options = {})
Array.new(max) do |counter|
content_tag(
'label',
'',
class: "rating#{' bright' if rating.to_f > counter}",
**options
)
end.join.html_safe
end
def rating_fraction(rating, max, options = {})
content_tag('span', "#{rating}/#{max}", **options)
end
def replacement_event_notice(event_schedule)
@ -47,4 +50,8 @@ module EventsHelper
include_blank: '(Please select)'
end
end
def rating_tooltip(event, max_rating)
"#{event.average_rating}/#{max_rating}, #{pluralize(event.voters.length, 'vote')}"
end
end

View file

@ -0,0 +1,12 @@
- if show_votes
%div{ data: { toggle: 'tooltip' }, title: rating_tooltip(event, max_rating) }<
= rating_stars(event.average_rating, max_rating, avgrate: true)
.clearfix
- if event.voted?(current_user)
%span.label.label-success
You voted:
= rating_fraction(event.user_rating(current_user), max_rating)
- else
%span.label.label-danger
Not rated

View file

@ -168,8 +168,14 @@
%b Requirements
%td= simple_format(@event.description)
- if @conference.program && @conference.program.rating && @conference.program.rating > 0
= render partial: 'voting'
- if @conference.program.rating_enabled?
= render 'voting',
event: @event,
show_votes: @program.show_voting?,
max_rating: @program.rating,
voting_period: @program.voting_period?,
votes: @votes,
conference_id: @conference.short_title
.row
= link_to "Comments (#{@comment_count})", '#', id: 'event-comment-link'

View file

@ -1,70 +1,56 @@
%table.table#myrating
- if @program.show_voting?
- if show_votes
%tr
%td.col-md-2
%b Rating
%td
- if @event.average_rating.to_f > 0
#{@event.average_rating}/#{@program.rating}
- else
Rating: 0/#{@program.rating}
- @program.rating.times do |counter|
- if @event.average_rating.to_f.round == counter + 1
= label_tag 'label_rating', '', class: 'avgrating', avgrate: true
= javascript_tag "$('label[avgrate=true]').prevAll().andSelf().addClass('bright');"
- else
= label_tag 'label_rating', '', class: 'avgrating'
#{event.average_rating}/#{max_rating}
= rating_stars(event.average_rating, max_rating, avgrate: true )
%tr
%td
%b Voters
%td
= @event.voters.length
- if @event.voters.length > 0
= votes.length
- unless votes.blank?
(
= @ratings.map {|x| "#{x.name}"}.join ', '
= votes.collect(&:name).to_sentence
)
%tr
%td.col-md-2
%b Your vote
%td
- if @program.voting_period?
- @program.rating.times do |counter|
- if @event.voted?(current_user) && @event.user_rating(current_user) == counter + 1
= link_to '', vote_admin_conference_program_event_path(@conference.short_title, @event, rating: counter + 1), remote: true, id: "label#{counter + 1}", class: 'myrating', voted: true
- if voting_period
- max_rating.times do |counter|
- if event.user_rating(current_user) > counter
= link_to '',
vote_admin_conference_program_event_path(conference_id,
event, rating: counter + 1),
remote: true,
id: "label#{counter + 1}",
class: 'rating myrating bright',
voted: true
- else
= link_to '', vote_admin_conference_program_event_path(@conference.short_title, @event, rating: counter + 1), remote: true, id: "label#{counter + 1}", class: 'myrating'
%br
= link_to '',
vote_admin_conference_program_event_path(conference_id,
event, rating: counter + 1),
remote: true,
id: "label#{counter + 1}",
class: 'rating myrating'
- else
- @conference.program.rating.times do |counter|
- if @event.voted?(current_user) && @event.user_rating(current_user) == counter + 1
= label_tag "label#{counter + 1}", '', class: 'othersrating', voted: true
= javascript_tag "$('label[voted=true]').prevAll().andSelf().addClass('bright');"
- else
= label_tag "label#{counter + 1}", '', class: 'othersrating'
(#{voting_open_or_close(@program)})
= rating_stars(event.user_rating(current_user), max_rating, voted: true)
= (Voting period is closed)
- if @program.show_voting?
- if @ratings.length > 0
- @ratings.each do |rate|
- unless rate.user_id == current_user.id
- if show_votes
- unless votes.blank?
- votes.each do |vote|
- unless vote.user_id == current_user.id
%tr
%td
= rate.name
= vote.name
%td
- @conference.program.rating.times do |counter|
= rating_stars(vote, max_rating )
- if @event.voted?(rate.user) && @event.user_rating(rate.user) == counter + 1
= label_tag "label#{counter + 1}", "", class: 'othersrating', voted: true
= javascript_tag "$('label[voted=true]').prevAll().andSelf().addClass('bright');"
- else
= label_tag "label#{counter + 1}", "", class: 'othersrating'
:javascript
$(function () {
var checkedId = $("a[voted='true']").attr('id');
$('a[id=' + checkedId + ']').prevAll().andSelf().addClass('bright');
});
$(".myrating").hover(
function() { // mouseover
$(this).prevAll().andSelf().addClass('glow');

View file

@ -1,19 +0,0 @@
- if @program.show_voting?
#{event.average_rating}/#{@program.rating}
%br
#{pluralize(event.voters.length, 'voter')}
%br
- @program.rating.times do |counter|
- if event.average_rating.to_f.round == counter + 1
= label_tag 'label_rating', '', class: 'avgrating', avgrate: true
= javascript_tag "$('label[avgrate=true]').prevAll().andSelf().addClass('bright');"
- else
= label_tag 'label_rating', '', class: 'avgrating'
%br
- if event.voted?(current_user)
%span.label.label-success
Your rating: #{ event.user_rating(current_user) }
- else
%span.label.label-danger
Not rated

View file

@ -106,8 +106,11 @@
= link_to event.title, admin_conference_program_event_path(@conference.short_title, event)
- if @program.rating_enabled?
%td.col-md-1{ 'data-order' => "#{event.average_rating}" }
= render partial: 'voting_index', locals: { event: event }
%td.col-md-1{ data: { order: event.average_rating } }
= render 'datatable_row_rating',
event: event,
show_votes: @program.show_voting?,
max_rating: @program.rating
- if event.submitter && event.submitter.registrations && event.submitter.registrations.count < 1
- bgcolor = '#F7819F'

View file

@ -1 +1,10 @@
$('table#myrating').replaceWith("<%= escape_javascript(render :partial => 'voting') %>");
$('table#myrating').replaceWith(
"<%= escape_javascript(render 'voting', \
event: @event, \
show_votes: @program.show_voting?, \
max_rating: @program.rating, \
voting_period: @program.voting_period?, \
votes: @votes, \
conference_id: @conference.short_title \
) %>"
);

View file

@ -3,6 +3,13 @@ require 'spec_helper'
describe EventsHelper, type: :helper do
let(:conference) { create(:conference) }
let(:event) { create(:event, program: conference.program) }
let(:my_vote) { 3 }
let(:max_rating) { 5 }
let(:fraction) { my_vote.to_s + '/' + max_rating.to_s }
setup do
allow(event).to receive(:average_rating) { my_vote }
end
describe '#registered_text' do
describe 'returns correct string' do
@ -18,4 +25,35 @@ describe EventsHelper, type: :helper do
end
end
end
describe '#rating_tooltip' do
let(:vote_count) { pluralize(event.voters.length, 'vote') }
it 'includes the average rating' do
expect(rating_tooltip(event, max_rating)).to match(fraction)
end
it 'includes the vote count' do
expect(rating_tooltip(event, max_rating)).to match(vote_count)
end
end
describe '#rating_fraction' do
it 'represents the rating as a fraction of the max' do
expect(rating_fraction(my_vote, max_rating)).to match(fraction)
end
describe 'rating_stars' do
it 'renders labels for each value of max_rating' do
expect(
rating_stars(my_vote, max_rating).scan('<label class="rating').size
).to eq(max_rating)
end
it 'renders bright labels for each value of vote' do
expect(
rating_stars(my_vote, max_rating).scan('<label class="rating bright').size
).to eq(my_vote)
end
end
end
end