diff --git a/app/assets/javascripts/votes.js.coffee b/app/assets/javascripts/votes.js.coffee index 5005ad5b8..da3d55664 100644 --- a/app/assets/javascripts/votes.js.coffee +++ b/app/assets/javascripts/votes.js.coffee @@ -2,9 +2,11 @@ App.Votes = hoverize: (votes) -> $(votes).hover -> - $("div.not-logged", votes).show() + $("div.anonymous-votes", votes).show(); + $("div.not-logged", votes).show(); , -> - $("div.not-logged", votes).hide() + $("div.anonymous-votes", votes).hide(); + $("div.not-logged", votes).hide(); initialize: -> App.Votes.hoverize votes for votes in $("div.votes") diff --git a/app/assets/stylesheets/debates.scss b/app/assets/stylesheets/debates.scss index 1fded1efa..a480f4870 100644 --- a/app/assets/stylesheets/debates.scss +++ b/app/assets/stylesheets/debates.scss @@ -134,6 +134,31 @@ text-decoration: underline; } } + + .anonymous-votes { + background: $warning-bg; + color: $warning-color; + height: 100%; + left: 0; + line-height: $line-height; + padding-top: rem-calc(12); + position: absolute; + text-align: center; + top: 0; + width: 100%; + + p { + color: $warning-color; + margin: 0 rem-calc(12); + text-align: left; + } + + a { + color: $warning-color; + font-weight: bold; + text-decoration: underline; + } + } } // 02. Index @@ -354,6 +379,10 @@ line-height: $line-height; padding-top: rem-calc(36); } + + .anonymous-votes { + padding-top: rem-calc(24); + } } } diff --git a/app/assets/stylesheets/participacion.scss b/app/assets/stylesheets/participacion.scss index 6c498adcc..0b027001b 100644 --- a/app/assets/stylesheets/participacion.scss +++ b/app/assets/stylesheets/participacion.scss @@ -773,6 +773,12 @@ form { background-color: $warning-bg; border-color: $warning-border; color: $warning-color; + + a { + color: $warning-color; + font-weight: bold; + text-decoration: underline; + } } &.alert { diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 87df13d37..451d8e893 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -53,7 +53,7 @@ class DebatesController < ApplicationController end def vote - @debate.vote_by(voter: current_user, vote: params[:value]) + @debate.register_vote(current_user, params[:value]) set_debate_votes(@debate) end diff --git a/app/models/debate.rb b/app/models/debate.rb index fdf939b2f..e088d8141 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -31,7 +31,6 @@ class Debate < ActiveRecord::Base scope :sort_by_total_votes, -> { reorder(cached_votes_total: :desc) } scope :sort_by_likes , -> { reorder(cached_votes_up: :desc) } scope :sort_by_created_at, -> { reorder(created_at: :desc) } - # Ahoy setup visitable # Ahoy will automatically assign visit_id on create @@ -56,6 +55,10 @@ class Debate < ActiveRecord::Base cached_votes_total end + def total_anonymous_votes + cached_anonymous_votes_total + end + def editable? total_votes == 0 end @@ -64,6 +67,24 @@ class Debate < ActiveRecord::Base editable? && author == user end + def register_vote(user, vote_value) + if votable_by?(user) + Debate.increment_counter(:cached_anonymous_votes_total, id) if (user.unverified? && !user.voted_for?(self)) + vote_by(voter: user, vote: vote_value) + end + end + + def votable_by?(user) + !user.unverified? || + anonymous_votes_ratio < Setting.value_for('max_ratio_anon_votes_on_debates').to_i || + user.voted_for?(self) + end + + def anonymous_votes_ratio + return 0 if cached_votes_total == 0 + (cached_anonymous_votes_total.to_f / cached_votes_total) * 100 + end + def description super.try :html_safe end diff --git a/app/models/setting.rb b/app/models/setting.rb index 1a52ebe60..8e1504bf3 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -1,4 +1,7 @@ class Setting < ActiveRecord::Base + + validates :key, presence: true, uniqueness: true + default_scope { order(key: :desc) } def self.value_for(key) diff --git a/app/views/admin/moderators/search.js.erb b/app/views/admin/moderators/search.js.erb index 5b8a61207..a6ac48206 100644 --- a/app/views/admin/moderators/search.js.erb +++ b/app/views/admin/moderators/search.js.erb @@ -1 +1 @@ -$("#search-result").html("
<%= j render 'moderator', moderator: @moderator %>
"); +$("#search-result").html("
<%= j render 'moderator', moderator: @moderator %>
"); diff --git a/app/views/debates/_votes.html.erb b/app/views/debates/_votes.html.erb index 462353ff9..5ce64d784 100644 --- a/app/views/debates/_votes.html.erb +++ b/app/views/debates/_votes.html.erb @@ -21,7 +21,14 @@ <%= t("debates.debate.votes", count: debate.total_votes) %> - <% unless user_signed_in? %> + <% if user_signed_in? && current_user.unverified? %> + + <% elsif !user_signed_in? %>