Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made All JS Unobtrusive and added option to enabled / disable it on a…
… per-site basis.
- Loading branch information
Showing
12 changed files
with
105 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,32 @@ | ||
class StarsController < ApplicationController | ||
|
||
skip_before_filter :verify_authenticity_token | ||
|
||
def new | ||
@star = Star.new | ||
@star.user_id = params[:user_id] | ||
@star.item_id = params[:item_id] | ||
|
||
if(@star.save) | ||
@star.reload | ||
plural = @star.item.stars.size == 1 ? "star" : "stars" | ||
render :text => "#{@star.item.stars.size} #{plural}" | ||
end | ||
end | ||
before_filter :login_required | ||
|
||
def destroy | ||
@star = Star.find_by_user_id_and_item_id(params[:user_id],params[:item_id]) | ||
@count = @star.item.stars.size - 1 | ||
if(@star.destroy) | ||
plural = @count == 1 ? "star" : "stars" | ||
render :text => "#{@count} #{plural}" | ||
end | ||
def add | ||
@item = Item.find(params[:item_id]) | ||
@star = Star.new(:item => @item, :user => current_user) | ||
saved = @star.save | ||
respond_to do |wants| | ||
wants.html { redirect_to @item } | ||
wants.js do | ||
stars_count = @item.stars.size + 1 | ||
saved ? render(:text => "#{stars_count} #{stars_count == 1 ? "star" : "stars"}") : head(:unprocessable_entity) | ||
end | ||
end | ||
end | ||
|
||
def remove | ||
@item = Item.find(params[:item_id]) | ||
@star = @item.stars.find(:first, :conditions => ["user_id = ?", current_user.id]) | ||
destroyed = @star ? @star.destroy : false # If the star doesn't exist, add it | ||
respond_to do |wants| | ||
wants.html { redirect_to @item } | ||
wants.js do | ||
stars_count = @item.stars.size - 1 | ||
destroyed ? render(:text => "#{stars_count} #{stars_count == 1 ? "star" : "stars"}") : head(:unprocessable_entity) | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,69 @@ | ||
// Place your application-specific JavaScript functions and classes here | ||
// This file is automatically included by javascript_include_tag :defaults | ||
var id_pattern = /item_wrapper_for_(\d+)/i; | ||
|
||
function star (item_id, user_id, is_starred) { | ||
function star(item_id, is_starred) { | ||
if(is_starred) { | ||
var url = "/stars/destroy"; | ||
var url = "/items/" + item_id + "/star/remove"; | ||
var jsOptions = "false"; | ||
} else { | ||
var url = "/stars/new" | ||
var url = "/items/" + item_id + "/star/add" | ||
var jsOptions = "true"; | ||
} | ||
|
||
// Send the data to the server | ||
|
||
$.post(url, {item_id: item_id, user_id: user_id}, function(data) { | ||
$.get(url, {}, function(data) { | ||
$("#item_wrapper_for_" + item_id + " .star a").toggleClass("starred").html((jsOptions == "false" ? "" : "un") + "star this post"); | ||
$("#item_" + item_id + "-star-count").html(data); | ||
$("#item_wrapper_for_" + item_id + " .star a").toggleClass("starred"); | ||
$("#item_wrapper_for_" + item_id + " .star a").attr("onClick", "star(" + item_id + ", "+ user_id +", " + jsOptions +")"); | ||
$("#item_wrapper_for_" + item_id + " .star a").html((jsOptions == "false" ? "" : "un") + "star this post"); | ||
}, "text"); | ||
|
||
} | ||
|
||
function setupAdminAreaToggles() { | ||
$(".admin-actions").each(function(){ | ||
var current = $(this); | ||
var parent = current.parent(); | ||
parent.mouseover(function() { | ||
current.show(); | ||
}); | ||
parent.mouseout(function() { | ||
current.hide(); | ||
}); | ||
}).hide(); | ||
} | ||
|
||
// Someone might want to refactor / rewrite this to make it nicer. | ||
function setupItemAjaxSubmit() { | ||
var posts = $(".post"); | ||
posts.each(function() { | ||
var post = $(this); | ||
setStarFor(post); | ||
}); | ||
} | ||
|
||
function setStarFor(post) { | ||
var match = id_pattern.exec(post.attr("id")); | ||
// Check if we can extract the id. | ||
if(match != null) { | ||
var item_id = parseInt(match[1], 10); | ||
var star_link = post.find(".star a"); | ||
star_link.click(function(){ | ||
star(item_id, !(star_link.html().match(/un/) == null)); | ||
return false; | ||
}); | ||
} | ||
} | ||
|
||
// Set jQuery to automatically use the correct | ||
// accepts header so that we can use respond_to | ||
// with js. | ||
jQuery.ajaxSetup({ | ||
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", | ||
"text/javascript")} | ||
}) | ||
|
||
|
||
// Make all JS behaviour unobtrusive. | ||
$(function() { | ||
setupAdminAreaToggles(); | ||
setupItemAjaxSubmit(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -378,7 +378,6 @@ H1 A { color: #c00; } | |
position: absolute; | ||
top: 12px; | ||
right: 12px; | ||
display: none; | ||
} | ||
|
||
/* @end */ | ||
|