fixed comment vote functional test
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
class CommentVotesController < ApplicationController
|
||||
rescue_from CommentVote::Error, :with => :error
|
||||
|
||||
def create
|
||||
@comment = Comment.find(params[:comment_id])
|
||||
@comment.vote!(params[:score])
|
||||
rescue CommentVote::Error => x
|
||||
@error = x
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
private
|
||||
def error(exception)
|
||||
@exception = exception
|
||||
render :action => "error", :status => 500
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,2 +1,14 @@
|
||||
module PostVotesHelper
|
||||
def pool_version_diff(current)
|
||||
prev = PoolVersion.where(["pool_id = ? and id < ?", current.pool_id, current.id]).order("id desc").first
|
||||
|
||||
if prev.nil?
|
||||
return current.post_id_array.map {|x| content_tag("ins", "+#{x}")}.join(" ").html_safe
|
||||
end
|
||||
|
||||
added = current.post_id_array - prev.post_id_array
|
||||
removed = prev.post_id_array - current.post_id_array
|
||||
|
||||
added.map {|x| '<ins>+<a href="/posts/' + x + '">' + x + '</a></ins>'}.join(" ") + removed.map {|x| '<del>–<a href="/posts/' + x + '">' + x + '</a></del>'}.join(" ")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -37,23 +37,14 @@ class Comment < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def vote!(score)
|
||||
if !CurrentUser.user.can_comment_vote?
|
||||
raise CommentVote::Error.new("You can only vote ten times an hour on comments")
|
||||
|
||||
elsif score == "down" && creator.is_janitor?
|
||||
raise CommentVote::Error.new("You cannot downvote janitor/moderator/admin comments")
|
||||
|
||||
elsif votes.find_by_user_id(CurrentUser.user.id).nil?
|
||||
if score == "up"
|
||||
increment!(:score)
|
||||
elsif score == "down"
|
||||
decrement!(:score)
|
||||
end
|
||||
|
||||
votes.create
|
||||
|
||||
else
|
||||
raise CommentVote::Error.new("You have already voted for this comment")
|
||||
vote = votes.create(:score => score)
|
||||
|
||||
if vote.errors.any?
|
||||
raise CommentVote::Error.new(vote.errors.full_messages.join("; "))
|
||||
elsif vote.is_positive?
|
||||
increment!(:score)
|
||||
elsif vote.is_negative?
|
||||
decrement!(:score)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,12 +4,42 @@ class CommentVote < ActiveRecord::Base
|
||||
belongs_to :comment
|
||||
belongs_to :user
|
||||
before_validation :initialize_user, :on => :create
|
||||
validates_presence_of :user_id, :comment_id
|
||||
validates_presence_of :user_id, :comment_id, :score
|
||||
validates_uniqueness_of :user_id, :scope => :comment_id
|
||||
validate :validate_user_can_vote
|
||||
validate :validate_comment_can_be_down_voted
|
||||
validates_inclusion_of :score, :in => [-1, 1], :message => "must be 1 or -1"
|
||||
|
||||
def self.prune!
|
||||
destroy_all(["created_at < ?", 14.days.ago])
|
||||
end
|
||||
|
||||
def validate_user_can_vote
|
||||
if !user.can_comment_vote?
|
||||
errors.add :user, "can not comment vote"
|
||||
false
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def validate_comment_can_be_down_voted
|
||||
if is_negative? && comment.creator.is_janitor?
|
||||
errors.add :user, "can not downvote a janitor comment"
|
||||
false
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def is_positive?
|
||||
score == 1
|
||||
end
|
||||
|
||||
def is_negative?
|
||||
score == -1
|
||||
end
|
||||
|
||||
def initialize_user
|
||||
self.user_id = CurrentUser.user.id
|
||||
end
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class NoteVersion < ActiveRecord::Base
|
||||
before_validation :initialize_updater
|
||||
belongs_to :updater, :class_name => "User"
|
||||
|
||||
def initialize_updater
|
||||
self.updater_id = CurrentUser.id
|
||||
|
||||
@@ -9,4 +9,8 @@ class PoolVersion < ActiveRecord::Base
|
||||
self.updater_id = CurrentUser.id
|
||||
self.updater_ip_addr = CurrentUser.ip_addr
|
||||
end
|
||||
|
||||
def post_id_array
|
||||
@post_id_array ||= post_ids.scan(/\d+/).map(&:to_i)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
<% if @error %>
|
||||
alert("<%= escape_javascript(@error.to_s) %>");
|
||||
<% end %>
|
||||
@@ -1,4 +1,4 @@
|
||||
<table width="100%" class="row-highlight">
|
||||
<table width="100%">
|
||||
<thead>
|
||||
<tr>
|
||||
<th></th>
|
||||
@@ -12,27 +12,27 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @notes.each do |note| %>
|
||||
<tr class="<%= cycle 'even', 'odd' %>">
|
||||
<td style="background: <%= id_to_color(note.post_id) %>;"></td>
|
||||
<td><%= link_to note.post_id, :controller => "post", :action => "show", :id => note.post_id %></td>
|
||||
<td><%= link_to "#{note.note_id}.#{note.version}", :controller => "note", :action => "history", :id => note.note_id %></td>
|
||||
<td><%= h(note.body) %> <% unless note.is_active? %>(deleted)<% end %></td>
|
||||
<% @note_versions.each do |note_version| %>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td><%= link_to note.post_id, post_path(note_version.post_id) %></td>
|
||||
<td><%= link_to "#{note_version.note_id}", note_versions_path(:search => {:note_id_eq => note_version.note_id}) %></td>
|
||||
<td><%= h(note_version.body) %> <% unless note_version.is_active? %>(deleted)<% end %></td>
|
||||
<td>
|
||||
<% if @current_user.is_admin? %>
|
||||
<%= note.ip_addr %>
|
||||
<% if CurrentUser.is_janitor? %>
|
||||
<%= note_version.ip_addr %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td><%= link_to h(note.author), :controller => "user", :action => "show", :id => note.user_id %></td>
|
||||
<td><%= note.updated_at.strftime("%Y-%m-%d %H:%M") %></td>
|
||||
<td><%= link_to "Revert", {:controller => "note", :action => "revert", :id => note.note_id, :version => note.version}, :method => :post, :confirm => "Do you really wish to revert to this note?" %></td>
|
||||
<td><%= link_to note_version.updater.name, user_path(note_version.updater) %></td>
|
||||
<td><%= note_version.updated_at.strftime("%Y-%m-%d %H:%M") %></td>
|
||||
<td><%= link_to "Revert", revert_note_path(note_version.note_id, :version => note_version.id), :remote => true, :confirm => "Do you really want to revert to this version?" %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div id="paginator">
|
||||
<%= will_paginate(@notes) %>
|
||||
<%= will_paginate(@note_versions) %>
|
||||
</div>
|
||||
|
||||
<%= render :partial => "footer" %>
|
||||
|
||||
@@ -3,13 +3,3 @@
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="note-list">
|
||||
<%= render :partial => "post/posts", :locals => {:posts => @posts} %>
|
||||
|
||||
<div id="paginator">
|
||||
<%= will_paginate(@posts) %>
|
||||
</div>
|
||||
|
||||
<%= render :partial => "footer" %>
|
||||
</div>
|
||||
|
||||
@@ -1,47 +1,40 @@
|
||||
<div id="pool-history">
|
||||
<h4>Pool History: <%= h(@pool.pretty_name) %></h4>
|
||||
<table width="100%" class="highlightable">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Post Count</th>
|
||||
<th>Changes</th>
|
||||
<th>Updater</th>
|
||||
<th>IP Address</th>
|
||||
<th>Date</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @updates.each_with_index do |update, i| %>
|
||||
<tr class="<%= cycle 'even', 'odd' %>">
|
||||
<td><%= link_to update.post_count, :action => "show_historical", :id => update.id %></td>
|
||||
<td><%= pool_update_diff(@updates, i) %></td>
|
||||
<td><%= link_to update.updater_name, :controller => "user", :action => "show", :id => update.user_id %></td>
|
||||
<td>
|
||||
<% if @current_user.is_admin? %>
|
||||
<%= h update.ip_addr %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td><%= update.created_at.strftime("%Y-%m-%d %H:%M") %></td>
|
||||
<td><%= link_to "Revert", :action => "revert", :id => update.id %></td>
|
||||
<div id="c-pools">
|
||||
<div id="a-index">
|
||||
<h3>Pool History: <%= @pool_version.name %></h3>
|
||||
|
||||
<table width="100%">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Post Count</th>
|
||||
<th>Changes</th>
|
||||
<th>Updater</th>
|
||||
<th>IP Address</th>
|
||||
<th>Date</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @pool_versions.each do |pool_version| %>
|
||||
<tr>
|
||||
<td><%= link_to pool_version.post_id_array.size, pool_version_path(pool_version) %></td>
|
||||
<td><%= pool_version_diff(pool_version) %></td>
|
||||
<td><%= link_to pool_version.updater.name, user_path(pool_version.updater) %></td>
|
||||
<td>
|
||||
<% if CurrentUser.is_janitor? %>
|
||||
<%= pool_version.updater_ip_addr %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td><%= time_ago_in_words pool_version.updated_at %></td>
|
||||
<td><%= link_to "Revert", revert_pool_path(pool_version.pool_id, :version => pool_version.id) %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="paginator">
|
||||
<%= will_paginate(@updates) %>
|
||||
<%= will_paginate(@pool_versions) %>
|
||||
</div>
|
||||
|
||||
<% content_for("footer") do %>
|
||||
<li>|</li>
|
||||
<li><%= link_to "Show", :action => "show", :id => params[:id] %></li>
|
||||
<li><%= link_to "Edit", :action => "update", :id => params[:id] %></li>
|
||||
<li><%= link_to "Delete", :action => "destroy", :id => params[:id] %></li>
|
||||
<li><%= link_to "Order", :action => "order", :id => params[:id] %></li>
|
||||
<li><%= link_to "Import", :action => "import", :id => params[:id] %></li>
|
||||
<li><%= link_to "History", :action => "history", :id => params[:id] %></li>
|
||||
<% end %>
|
||||
|
||||
<%= render :partial => "footer" %>
|
||||
<%= render :partial => "pools/secondary_links" %>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<label for="user_name">User</label>
|
||||
</div>
|
||||
<div>
|
||||
<% form_tag({:action => "index"}, :method => :get) do %>
|
||||
<%= form_tag(post_versions_path, :method => :get) do %>
|
||||
<%= text_field_tag "user_name", params[:user_name], :id => "user_name", :size => 20 %> <%= submit_tag "Search" %>
|
||||
<% end %>
|
||||
</div>
|
||||
@@ -15,7 +15,7 @@
|
||||
<label for="post_id">Post ID</label>
|
||||
</div>
|
||||
<div>
|
||||
<% form_tag({:action => "index"}, :method => :get) do %>
|
||||
<%= form_tag(post_versions_path, :method => :get) do %>
|
||||
<%= text_field_tag "post_id", params[:post_id], :id => "post_id", :size => 10 %> <%= submit_tag "Search" %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<div id="c-tag-aliases">
|
||||
<div id="a-index">
|
||||
<div class="search">
|
||||
<% form_tag(tag_aliases_path, :method => :get) do %>
|
||||
<%= form_tag(tag_aliases_path, :method => :get) do %>
|
||||
<%= text_field_tag "query", params[:query] %>
|
||||
<%= submit_tag "Search Aliases" %>
|
||||
<%= submit_tag "Search Implications" %>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<div id="c-tag-implications">
|
||||
<div id="a-index">
|
||||
<div class="search">
|
||||
<% form_tag(tag_implications_path, :method => :get) do %>
|
||||
<%= form_tag(tag_implications_path, :method => :get) do %>
|
||||
<%= text_field_tag "query", params[:query] %>
|
||||
<%= submit_tag "Search Aliases" %>
|
||||
<%= submit_tag "Search Implications" %>
|
||||
|
||||
Reference in New Issue
Block a user