Permalink
Browse files

Make it possible to override the way we get the href, add test for it.

  • Loading branch information...
1 parent 6186a85 commit e78f71be266690b07c3d7b9206bcb8bb34b1b38a @webmat committed Mar 7, 2012
Showing with 45 additions and 3 deletions.
  1. +7 −2 src/rails.js
  2. +37 −0 test/public/test/override.js
  3. +1 −1 test/views/index.erb
View
9 src/rails.js
@@ -99,6 +99,11 @@
return $.ajax(options);
},
+ // Default way to get an element's href. May be overridden at $.rails.href.
+ href: function(element) {
+ return element.attr('href');
+ },
+
// Submits "remote" forms and links with ajax
handleRemote: function(element) {
var method, url, data,
@@ -125,7 +130,7 @@
if (element.data('params')) data = data + "&" + element.data('params');
} else {
method = element.data('method');
- url = element.attr('href');
+ url = rails.href(element);
data = element.data('params') || null;
}
@@ -160,7 +165,7 @@
// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
handleMethod: function(link) {
- var href = link.attr('href'),
+ var href = rails.href(link),
method = link.data('method'),
target = link.attr('target'),
csrf_token = $('meta[name=csrf-token]').attr('content'),
View
37 test/public/test/override.js
@@ -0,0 +1,37 @@
+(function(){
+
+module('override', {
+ setup: function() {
+ window.realHref = $.rails.href;
+ $('#qunit-fixture').append($('<a />', {
+ href: '/real/href', 'data-method': 'delete', 'data-href': '/data/href'
+ }));
+ },
+ teardown: function() {
+ $.rails.href = window.realHref;
+ }
+});
+
+asyncTest("the getter for an element's href is publicly accessible", 1, function() {
+ ok($.rails.href);
+ start();
+});
+
+asyncTest("the getter for an element's href is overridable", 1, function() {
+ $.rails.href = function(element) { return element.data('href'); }
+ $.rails.ajax = function(options) {
+ equal('/data/href', options.url);
+ }
+ $.rails.handleRemote($('#qunit-fixture').find('a'));
+ start();
+});
+
+asyncTest("the getter for an element's href works normally if not overridden", 1, function() {
+ $.rails.ajax = function(options) {
+ equal('/real/href', options.url);
+ }
+ $.rails.handleRemote($('#qunit-fixture').find('a'));
+ start();
+});
+
+})();
View
2 test/views/index.erb
@@ -1,6 +1,6 @@
<% @title = "jquery-ujs test" %>
-<%= test 'data-confirm', 'data-remote', 'data-disable', 'call-remote', 'call-remote-callbacks', 'data-method' %>
+<%= test 'data-confirm', 'data-remote', 'data-disable', 'call-remote', 'call-remote-callbacks', 'data-method', 'override' %>
<h1 id="qunit-header"><%= @title %></h1>
<div id="jquery-cdn">

0 comments on commit e78f71b

Please sign in to comment.