Skip to content

Commit

Permalink
Faster request and response.
Browse files Browse the repository at this point in the history
  • Loading branch information
jongha committed Jun 24, 2014
1 parent f175aa5 commit 4934900
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 146 deletions.
12 changes: 8 additions & 4 deletions app/controllers/vote_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ class VoteController < ApplicationController
def add
find_board_and_topic

if ['-1', '1'].include? params[:point] then
if ['-1', '1', '0'].include? params[:point] then
@point = @votes.add_vote(@message.id, @user.id, params[:point])
else
@point = @votes.get_point(@message.id)
end

get
end

def get
find_board_and_topic

@point = @votes.get_point(@message.id)
# @point = @votes.get_point(@message.id)
result = @votes.get_points(@user.id, @message.id)
result['point'] = result['plus'] + result['minus']
render :json => result

end

def result
Expand Down
9 changes: 9 additions & 0 deletions app/models/votes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ def add_vote(message_id, user_id, point = 0)
def get_point(message_id)
return Votes.sum(:point, :conditions => ['message_id = ?', message_id])
end

def get_points(user_id, message_id)
return result = {
"plus" => Votes.sum(:point, :conditions => ['message_id = ? and point > 0', message_id]),
"minus" => Votes.sum(:point, :conditions => ['message_id = ? and point < 0', message_id]),
"zero" => Votes.sum(:point, :conditions => ['message_id = ? and point = 0', message_id]),
"vote" => Votes.count(:point, :conditions => ['message_id = ? and user_id = ?', message_id, user_id]),
}
end
end
15 changes: 6 additions & 9 deletions app/views/vote/_view_layouts_base_content.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<% unless User.current.login == '' %>
<% unless @board.nil? || @board[:id].nil? || @topic.nil? || @topic.id.nil? %>
<div id="vote" class="vote-area" data-board="<%=@board[:id] %>" data-topic="<%=@topic.id %>">
<div><a class="vote-button" href="#" data-point="1"></a></div>
<div class="vote-point">-</div>
<div><a class="vote-button" href="#" data-point="-1"></a></div>
</div>
<label id="label_vote_count" style="display:none"><%=l(:label_vote_count)%></label>
<% end %>
<div id="vote" class="vote-area" data-board="<%=@board.id%>" data-topic="<%=@topic.id%>">
<div><a class="vote-button" href="#" data-point="1"></a></div>
<div class="vote-point">-</div>
<div><a class="vote-button" href="#" data-point="-1"></a></div>
</div>
<label id="label_vote_count" style="display:none"><%=l(:label_vote_count)%></label>
<% end %>
<input type="hidden" id="vote-base-url" value="<%=url_for :controller => 'welcome' %>" />
1 change: 0 additions & 1 deletion app/views/vote/add.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion app/views/vote/get.html.erb

This file was deleted.

125 changes: 123 additions & 2 deletions assets/javascripts/vote.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,123 @@
/* vote.uncompressed.js Compressed file. using UglifyJS. */
$(document).ready(function(){var e=$("#vote-base-url");var t="";if(e.length>0){t=e.val()}if($(".controller-messages").length&&$("#vote").length){var n=$("<div></div>").css({clear:"right"});var r=[];$(".message").each(function(){r.push($(this))});var i=function(){if(r.length){s(r.shift())}};var s=function(e){var n=$.Deferred();var r=e.attr("id");var s=$("#vote").clone().show().attr({id:null});if(r){s.data({topic:parseInt(String(r).replace(/message-/gi,""))})}e.css({clear:"both"}).prepend(s);var o=s.data("board");var u=s.data("topic");var a=s.find(".vote-point:first");$.ajax({type:"GET",url:t+"boards/"+o+"/topics/"+u+"/vote",cache:false,error:function(e,t,n){a.html("-")},success:function(e,t,n){a.html($(e).find("#vote-point").html())}}).always(function(){n.always();s.find(".vote-button").bind("click",function(e){e.preventDefault();var n=$(this).data("point");$.ajax({type:"POST",url:t+"boards/"+o+"/topics/"+u+"/vote",data:{point:n},cache:false,success:function(e,t,n){a.html($(e).find("#vote-point").html())}})});i()});return n.promise()};i()}var o=/https?:\/\/.*\/projects\/.*\/boards\/([0-9]*)/;var u=document.URL;var a=u.match(o);var f="#vote-result";if(a){$("<div></div>").attr("id","vote-result-box").insertAfter($("#content").find("p.breadcrumb"));$.ajax({type:"GET",url:t+"boards/"+a[1]+"/vote/result",cache:false,success:function(e,t,n){if($(f).length===0){var r=$(e).find(f).html();if(r){$("#vote-result-box").addClass("vote-result").html(r)}else{$("#vote-result-box").remove()}}}});var l=$("table.list.messages");$("<th></th>").html($("#label_vote_count").text()).insertAfter(l.find("thead > tr > th:nth-child(3)"));$("<td></td>").addClass("vote-td-trigger").insertAfter(l.find("tbody > tr > td:nth-child(3)"));$("td.vote-td-trigger").each(function(){var e=/\/boards\/([0-9]*)\/topics\/([0-9]*)/;var n=$(this).parent().find("td.subject > a").attr("href");var r=n.match(e);if(r){var i=r[1];var s=r[2];var o=$(this);$.ajax({type:"GET",url:t+"boards/"+i+"/topics/"+s+"/vote",cache:false,success:function(e,t,n){o.html($(e).find("#vote-point").html())}})}})}})
$(document).ready(function() {
if($(".controller-messages").length && $("#vote").length) {
var clr = $("<div></div>").css({ clear: "right" });
var queue = [];
$(".message").each(
function() {
queue.push($(this));
}
);

var execQueue = function() {
if(queue.length) {
queueStep(queue.shift());
}
};

var queueStep = function(that) {
var deferred = $.Deferred();
var messageId = that.attr("id");
var vote = $("#vote").clone().show().attr({ id: null });
if(messageId) {
vote.data({ topic: parseInt(String(messageId).replace(/message-/gi, "")) });
}
that.css({ "clear": "both" }).prepend(vote);

var board = vote.data("board");
var topic = vote.data("topic");
var votePoint = vote.find(".vote-point:first");
var voteCheck = vote.find(".vote-check:first");

$.ajax({
type: "GET",
url: "/boards/" + board + "/topics/" + topic + "/vote",
cache: false,
error: function(jqXHR, textStatus, errorThrown) {
votePoint.html("-");
},
success: function(data, textStatus, jqXHR) {
votePoint.html(data.point);
voteCheck.html(data.vote ? "☑" : "✅");
}

}).always(function() {
deferred.always();

vote.find(".vote-button").bind("click", function(event) {
event.preventDefault();
var point = $(this).data("point");
$.ajax({
type: "POST",
url: "/boards/" + board + "/topics/" + topic + "/vote",
data: { point: point },
cache: false,
success: function(data, textStatus, jqXHR) {
votePoint.html(data.point);
voteCheck.html(data.vote ? "☑" : "✅");
}
});
});

execQueue();
});
return deferred.promise();
};
execQueue();
};

var re = /https?:\/\/.*\/projects\/.*\/boards\/([0-9]*)/;
var url = document.URL;
var match = url.match(re);
var result = "#vote-result";

if(match) {
$("<div></div>")
.attr("id", "vote-result-box")
.insertAfter($("#content").find("p.breadcrumb"));

$.ajax({
type: "GET",
url: "/boards/" + match[1] + "/vote/result",
cache: false,
success: function(data, textStatus, jqXHR) {
if($(result).length === 0) {
var html = $(data).find(result).html();
if(html) {
$("#vote-result-box").addClass("vote-result").html(html);
}else {
$("#vote-result-box").remove();
}
}
}
});

var table = $("table.list.messages");

$("<th></th>")
.html($("#label_vote_count").text())
.insertAfter(table.find("thead > tr > th:nth-child(3)"));

$("<td></td>")
.addClass("vote-td-trigger")
.insertAfter(table.find("tbody > tr > td:nth-child(3)"));

$("td.vote-td-trigger").each(function() {
var _re = /\/boards\/([0-9]*)\/topics\/([0-9]*)/;
var _href = $(this).parent().find("td.subject > a").attr("href");
var _match = _href.match(_re);
if(_match) {
var _board = _match[1];
var _topic = _match[2];
var _this = $(this);
$.ajax({
type: "GET",
url: "/boards/" + _board + "/topics/" + _topic + "/vote",
cache: false,
success: function(data, textStatus, jqXHR) {
_this.html(data.point);
}
});
}
});
}
});
128 changes: 0 additions & 128 deletions assets/javascripts/vote.uncompressed.js

This file was deleted.

2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
en:
label_vote_result: "Vote Result"
label_vote_count: "Votes"
label_vote_count: "Votes"

0 comments on commit 4934900

Please sign in to comment.