following button on site

This commit is contained in:
Kyle Drake 2014-05-05 15:25:25 -07:00
parent 51a85a0b03
commit 0a29a496d7
5 changed files with 62 additions and 18 deletions

7
app.rb
View file

@ -205,6 +205,13 @@ get '/site/:username/tip' do |username|
erb :'tip'
end
post '/site/:site_id/toggle_follow' do |site_id|
require_login
content_type :json
site = Site[id: site_id]
{result: (current_site.toggle_follow(site) ? 'followed' : 'unfollowed')}.to_json
end
get '/browse' do
@current_page = params[:current_page]
@current_page = @current_page.to_i

View file

@ -65,7 +65,10 @@ class Site < Sequel::Model
one_to_many :profile_comments
one_to_many :profile_commentings, key: :actioning_site_id, class: :ProfileComment
# Who is following this site
one_to_many :follows
# Who this site is following
one_to_many :followings, key: :actioning_site_id, class: :Follow
one_to_many :tips
@ -96,6 +99,20 @@ class Site < Sequel::Model
end
end
def is_following?(site)
followings_dataset.select(:id).filter(site_id: site.id).first ? true : false
end
def toggle_follow(site)
if is_following? site
followings_dataset.filter(site_id: site.id).delete
false
else
add_following site_id: site.id
true
end
end
def tip_amount
return '0.00' if tips_dataset.count == 0
'31.337'

View file

@ -0,0 +1,16 @@
var Site = {
toggleFollow: function(siteId, csrfToken) {
var link = $('a#followLink')
var span = $('a#followLink span')
$.post('/site/'+siteId+'/toggle_follow', {csrf_token: csrfToken}, function(res) {
console.log(res)
if(res.result == "followed") {
span.text('Unfollow')
link.removeClass('follow')
} else if(res.result == 'unfollowed') {
span.text('Follow')
link.addClass('follow')
}
})
}
}

View file

@ -3,6 +3,8 @@
<script src="/assets/scripts/news/comment.js"></script>
<script src="/assets/scripts/news/profile_comment.js"></script>
<script src="/assets/scripts/news/event.js"></script>
<script src="/assets/scripts/news/site.js"></script>
<% events.each do |event| %>
<% if event.profile_comment_id %>
<div class="news-item comment for-me" id="event_<%= event.id %>">

View file

@ -16,9 +16,11 @@
<div class="stat tips"><strong><%= site.tips_dataset.count %></strong> <span>tips</span></div>
</div>
<div class="actions">
<% if site != current_site %>
<a href="" class="btn-Action follow"><span>Follow</span></a>
<a href="" class="btn-Action tip"><span>Tip</span></a>
<% if current_site && current_site != site %>
<a id="followLink" href="#" onclick="Site.toggleFollow(<%= site.id %>, '<%= csrf_token %>')" class="btn-Action <%= current_site.is_following?(site) ? '' : 'follow' %>">
<span><%= current_site.is_following?(site) ? 'Unfollow' : 'Follow' %></span>
</a>
<a href="#" class="btn-Action tip"><span>Tip</span></a>
<% end %>
<a href="" class="btn-Action share"><span>Share</span></a>
</div>