From 9e10678542c789e8a82e78311afdd62e3fc3c7e5 Mon Sep 17 00:00:00 2001 From: James Mason Date: Fri, 9 Feb 2018 17:19:33 -0800 Subject: [PATCH] Clean up rating partials --- app/assets/stylesheets/osem-rating.scss | 21 +++--- app/controllers/admin/events_controller.rb | 4 +- app/helpers/events_helper.rb | 27 ++++--- .../admin/events/_datatable_row_rating.haml | 12 +++ app/views/admin/events/_proposal.html.haml | 10 ++- app/views/admin/events/_voting.html.haml | 74 ++++++++----------- .../admin/events/_voting_index.html.haml | 19 ----- app/views/admin/events/index.html.haml | 7 +- app/views/admin/events/vote.js.erb | 11 ++- spec/helpers/events_helper_spec.rb | 38 ++++++++++ 10 files changed, 131 insertions(+), 92 deletions(-) create mode 100644 app/views/admin/events/_datatable_row_rating.haml delete mode 100644 app/views/admin/events/_voting_index.html.haml diff --git a/app/assets/stylesheets/osem-rating.scss b/app/assets/stylesheets/osem-rating.scss index 7312ac45..f686fbe2 100644 --- a/app/assets/stylesheets/osem-rating.scss +++ b/app/assets/stylesheets/osem-rating.scss @@ -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"); + } +} diff --git a/app/controllers/admin/events_controller.rb b/app/controllers/admin/events_controller.rb index 164e0407..87c78d15 100644 --- a/app/controllers/admin/events_controller.rb +++ b/app/controllers/admin/events_controller.rb @@ -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]) diff --git a/app/helpers/events_helper.rb b/app/helpers/events_helper.rb index 8f620e62..019fef99 100644 --- a/app/helpers/events_helper.rb +++ b/app/helpers/events_helper.rb @@ -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 diff --git a/app/views/admin/events/_datatable_row_rating.haml b/app/views/admin/events/_datatable_row_rating.haml new file mode 100644 index 00000000..8e3e9c29 --- /dev/null +++ b/app/views/admin/events/_datatable_row_rating.haml @@ -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 diff --git a/app/views/admin/events/_proposal.html.haml b/app/views/admin/events/_proposal.html.haml index 063bb20a..f5107b47 100644 --- a/app/views/admin/events/_proposal.html.haml +++ b/app/views/admin/events/_proposal.html.haml @@ -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' diff --git a/app/views/admin/events/_voting.html.haml b/app/views/admin/events/_voting.html.haml index b7ae8ee2..8a8fa4ed 100644 --- a/app/views/admin/events/_voting.html.haml +++ b/app/views/admin/events/_voting.html.haml @@ -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'); diff --git a/app/views/admin/events/_voting_index.html.haml b/app/views/admin/events/_voting_index.html.haml deleted file mode 100644 index 18a9e73b..00000000 --- a/app/views/admin/events/_voting_index.html.haml +++ /dev/null @@ -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 diff --git a/app/views/admin/events/index.html.haml b/app/views/admin/events/index.html.haml index be7abe05..f78a8e9d 100644 --- a/app/views/admin/events/index.html.haml +++ b/app/views/admin/events/index.html.haml @@ -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' diff --git a/app/views/admin/events/vote.js.erb b/app/views/admin/events/vote.js.erb index dc677901..46849859 100644 --- a/app/views/admin/events/vote.js.erb +++ b/app/views/admin/events/vote.js.erb @@ -1 +1,10 @@ -$('table#myrating').replaceWith("<%= escape_javascript(render :partial => 'voting') %>"); \ No newline at end of file +$('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 \ + ) %>" +); diff --git a/spec/helpers/events_helper_spec.rb b/spec/helpers/events_helper_spec.rb index 4d92ce0b..99f6d8e3 100644 --- a/spec/helpers/events_helper_spec.rb +++ b/spec/helpers/events_helper_spec.rb @@ -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('