Permalink
Browse files

add vote count

  • Loading branch information...
elentok committed Apr 21, 2012
1 parent 4bc3fa6 commit 657809680f70f375d29fcfd57c6d93565f1878c5
@@ -22,13 +22,14 @@ addEntry = (entry) ->
<div class="desc">#{entry.desc}</div>
</div>
<div class="secondary">
- ??? points,
+ <span class="points">#{entry.votes.length} points</span>,
added by #{entry.user.name}
about #{created_at}
</div>
"""
$('#entries').append(html)
+
$(document).ready ->
$('input[placeholder]').placeholder()
@@ -66,3 +67,23 @@ $(document).ready ->
for entry in entries
addEntry(entry)
+ $('#entries').on '.vote click', (event) ->
+ event.preventDefault()
+ a = $(event.target)
+ return if a.hasClass('icon-refresh')
+ entry = a.closest('.entry')
+ entryId = entry.data('id')
+ console.log "voting for #{entryId}"
+ a.removeClass('icon-plus-sign').addClass('icon-refresh')
+ $.ajax
+ url: "/vote/#{entryId}"
+ success: (response) ->
+ entry.find('.points').text "#{response.total_votes} points"
+ a.addClass('icon-plus-sign').removeClass('icon-refresh')
+
+
+
+
+
+
+
@@ -9,11 +9,11 @@ def index
@user = User.find(session[:user_id])
end
- @entries = Entry.includes(:user).all
+ @entries = Entry.includes(:user).includes(:votes).all
respond_to do |format|
format.html # index.html.erb
- format.json { render json: @entries.to_json(:include => :user) }
+ format.json { render json: @entries.to_json(:include => [:user, :votes]) }
end
end
View
@@ -1,6 +1,7 @@
class Entry < ActiveRecord::Base
before_save :validate_url, :get_info_from_github
belongs_to :user
+ has_many :votes
private
def validate_url
@@ -11,7 +11,8 @@
%a{:href => entry.url}= entry.name
.desc= entry.desc
.secondary
- ??? points,
+ %span.points #{entry.votes.length} points
+ ,
added by #{entry.user.name}
#{time_ago_in_words(entry.created_at)} ago

0 comments on commit 6578096

Please sign in to comment.