ajax 0.1.7 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -48,10 +48,18 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
48
48
  rdoc.rdoc_files.include('app/**/*.rb')
49
49
  end
50
50
 
51
- desc "Release a new patch version"
52
- task :release_new_version do
53
- Rake::Task['version:bump:patch'].invoke
54
- Rake::Task['github:release'].invoke
55
- Rake::Task['git:release'].invoke
56
- Rake::Task['gemcutter:release'].invoke
51
+ namespace :release do
52
+
53
+ desc "Release a new patch version"
54
+ task :patch do
55
+ Rake::Task['version:bump:patch'].invoke
56
+ Rake::Task['release:current'].invoke
57
+ end
58
+
59
+ desc "Release the current version (e.g. after a version bump). This rebuilds the gemspec, pushes the updated code, tags it and releases to RubyGems"
60
+ task :current do
61
+ Rake::Task['github:release'].invoke
62
+ Rake::Task['git:release'].invoke
63
+ Rake::Task['gemcutter:release'].invoke
64
+ end
57
65
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.7
1
+ 1.0.1
@@ -95,7 +95,7 @@ module Ajax
95
95
  url = Ajax.hashed_url_from_traditional(url)
96
96
  end
97
97
  end
98
- Ajax.logger.info("[ajax] rewrote redirect from #{original_url} to #{url}")
98
+ Ajax.logger.info("[ajax] rewrote redirect from #{original_url} to #{url}") unless original_url == url
99
99
 
100
100
  # Don't store session[:redirected_to] if doing a special redirect otherwise
101
101
  # when the next request for root comes in it will think we really want
@@ -107,8 +107,17 @@ module Ajax
107
107
  else
108
108
  session[:redirected_to] = url
109
109
  if request.xhr?
110
- render(:update) { |page| page.redirect_to(url) }
110
+ Ajax.logger.info("[ajax] detecting we are xhr. soft redirect")
111
+ redirect_path = URI.parse(url).select(:fragment).first
112
+ Ajax.logger.info("[ajax] redirect path is #{redirect_path}")
113
+ Ajax.set_header(response, :soft_redirect, redirect_path)
114
+ render :text => <<-END
115
+ <script type="text/javascript">
116
+ window.location.href = #{url.to_json};
117
+ </script>
118
+ END
111
119
  else
120
+ Ajax.logger.info("[ajax] not detecting we are xhr. Hard redirect!")
112
121
  redirect_to_full_url_without_ajax(url, status)
113
122
  end
114
123
  end
@@ -85,16 +85,25 @@ module Ajax
85
85
  #
86
86
  # The string and regex paths are modified to match full URLs by prepending
87
87
  # them with the appropriate regular expression.
88
- def exclude_paths(paths=nil)
88
+ def exclude_paths(paths=nil, expand = true)
89
89
  if !instance_variable_defined?(:@exclude_paths)
90
90
  @exclude_paths = []
91
91
  end
92
92
  (paths || []).each do |path|
93
- @exclude_paths << /^(\w+\:\/\/[^\/]+\/?)?#{path.to_s}$/
93
+ if expand
94
+ @exclude_paths << /^(\w+\:\/\/[^\/]+\/?)?#{path.to_s}$/
95
+ else
96
+ @exclude_paths << path
97
+ end
94
98
  end
95
99
  @exclude_paths
96
100
  end
97
101
 
102
+ # Directly set regexes for one or more paths that can be accessed directly without the AJAX framework.
103
+ def exclude_regex(exclude_regex=nil)
104
+ exclude_paths(exclude_regex, false)
105
+ end
106
+
98
107
  # Return a boolean indicating whether or not to exclude a path from the
99
108
  # AJAX redirect.
100
109
  def exclude_path?(path)
@@ -36,9 +36,18 @@ module Ajax
36
36
  protected
37
37
 
38
38
  def encode_and_parse_url(url)
39
- URI.parse(URI.encode(url).gsub("%23", "#")) rescue URI.parse('/')
39
+ if already_encoded?(url)
40
+ res = URI.parse(url.gsub("%23", "#")) rescue URI.parse('/')
41
+ else
42
+ res = URI.parse(URI.encode(url).gsub("%23", "#")) rescue URI.parse('/')
43
+ end
44
+ res
40
45
  end
41
-
46
+
47
+ def already_encoded?(url)
48
+ URI.decode(url) != url rescue true
49
+ end
50
+
42
51
  def url_host(url)
43
52
  if url.match(/^(\w+\:\/\/[^\/]+)\/?/)
44
53
  $1
@@ -1,6 +1,8 @@
1
1
  require 'rack-ajax/decision_tree'
2
2
  require 'rack-ajax/parser'
3
3
  require 'json'
4
+ require 'yaml'
5
+ require 'yaml/encoding' unless RUBY_VERSION.to_f == 1.9
4
6
 
5
7
  module Rack
6
8
  class Ajax
@@ -34,6 +36,7 @@ module Rack
34
36
  @parser = Parser.new(env)
35
37
  rack_response = @parser.instance_eval(&@decision_tree)
36
38
 
39
+
37
40
  # Clear the value of session[:redirected_to]
38
41
  unless env['rack.session'].nil?
39
42
  env['rack.session']['redirected_to'] = env['rack.session'][:redirected_to] = nil
@@ -49,7 +52,7 @@ module Rack
49
52
  # The Ajax::Spec::Helpers module includes a helper
50
53
  # method to test the result of a rewrite.
51
54
  if ::Ajax.is_mocked?
52
- rack_response.nil? ? Rack::Ajax::Parser.rack_response(env.to_yaml) : rack_response
55
+ rack_response.nil? ? Rack::Ajax::Parser.rack_response(env.to_yaml(:Encoding => :Utf8)) : rack_response
53
56
  elsif !rack_response.nil?
54
57
  rack_response
55
58
  else
@@ -15,7 +15,6 @@ module Rack
15
15
  # completeness.
16
16
  def default_decision_tree
17
17
  @@default_decision_tree ||= Proc.new do
18
- ::Ajax.logger.debug("[ajax] rack session #{@env['rack.session'].inspect}")
19
18
  ::Ajax.logger.debug("[ajax] Ajax-Info #{@env['Ajax-Info'].inspect}")
20
19
 
21
20
  if !::Ajax.exclude_path?(@env['PATH_INFO'] || @env['REQUEST_URI'])
@@ -330,6 +330,7 @@ var Ajax = function(options) {
330
330
  self.addressChanged = function() {
331
331
  if (document.location.pathname != '/') { return false; }
332
332
  if (self.disable_next_address_intercept) {
333
+ console.log('skipping address intercept & resetting disable_next_address_intercept')
333
334
  self.disable_next_address_intercept = false;
334
335
  return false;
335
336
  }
@@ -341,18 +342,21 @@ var Ajax = function(options) {
341
342
  // Ensure that the URL ends with '#' if we are on root. This
342
343
  // will not trigger addressChanged().
343
344
  if (document.location.pathname == '/'
344
- && document.location.href.indexOf('#') == -1) {
345
+ && document.location.href.indexOf('#') == -1) {
345
346
  document.location.href = document.location.href + '#';
346
347
  }
347
348
 
348
349
  // Clean up the URL before making the request. If the URL changes
349
350
  // as a result of this, update it, which will trigger this
350
351
  // callback again.
351
- var url = encodeURI($.address.value()).replace(/\/\//, '/');
352
+ console.log('cleaning up the url');
353
+ var url = ($.address.value()).replace(/\/\//, '/');
352
354
  if (url != $.address.value()) {
355
+ console.log('reloading because encoded uri ' + url + ' differs from current uri ' + $.address.value());
353
356
  $.address.value(url);
354
357
  return false;
355
358
  } else {
359
+ console.log('going ahead with load');
356
360
  self.loadPage({
357
361
  url: url
358
362
  });
@@ -380,21 +384,29 @@ var Ajax = function(options) {
380
384
  document.location.href = options.url;
381
385
  return true;
382
386
  }
387
+
388
+ if (options.url === undefined) {
389
+ console.log('[ajax] no url supplied ');
390
+ return false;
391
+ };
392
+
383
393
  self.loaded = false;
384
394
  self.showLoadingImage();
395
+ console.log('[ajax] loadPage ' + options.url);
385
396
 
386
397
  if (self.current_request !== undefined) {
387
- try {
388
- self.current_request.abort();
389
- console.log('[ajax] aborting current request');
390
- } catch(e) {
391
- console.log('[ajax] abort failed!', e);
392
- }
398
+ self.abortCurrentRequest();
393
399
  }
394
400
 
401
+ if ($.browser.msie) {
402
+ safe_url = encodeURI(options.url);
403
+ } else {
404
+ safe_url = options.url;
405
+ };
406
+
395
407
  self.current_request = jQuery.ajax({
396
408
  cache: false,
397
- url: options.url,
409
+ url: safe_url,
398
410
  method: options.method || 'GET',
399
411
  beforeSend: self.setRequestHeaders,
400
412
  success: self.responseHandler,
@@ -413,6 +425,21 @@ var Ajax = function(options) {
413
425
  });
414
426
  };
415
427
 
428
+
429
+ /**
430
+ * abortCurrentRequest
431
+ *
432
+ * Abort the current ajax request
433
+ */
434
+ self.abortCurrentRequest = function() {
435
+ try {
436
+ console.log('[ajax] aborting current request for url ' + self.current_request.url);
437
+ self.current_request.abort();
438
+ } catch(e) {
439
+ console.log('[ajax] abort failed!', e);
440
+ };
441
+ };
442
+
416
443
  /**
417
444
  * setRequestHeaders
418
445
  *
@@ -447,16 +474,42 @@ var Ajax = function(options) {
447
474
  *
448
475
  */
449
476
  self.linkClicked = function(event) {
477
+ // The deep link must be a path. A full URL shouldn't have
478
+ // made it through, but sometimes it can happen. In this
479
+ // case, strip off the host and protocol.
480
+ if ($(this).attr('href') == '#') return false;
481
+ ajax_url = $(this).attr('data-deep-link');
482
+ if (ajax_url.match(/^https?:\/\//)) {
483
+ ajax_url = ajax_url.replace(/(https?:\/\/[^\/]*\/(.*))/, '$2');
484
+ }
450
485
  if (document.location.pathname != '/') {
451
- var url = ('/#/' + $(this).attr('data-deep-link')).replace(/\/\//, '/');
486
+ var url = ('/#/' + ajax_url).replace(/\/\//, '/');
487
+ console.log('linkClicked 1: going to ' + url);
452
488
  document.location.href = url;
453
489
  } else {
454
490
  self.last_click_coords = { pageX: event.pageX, pageY: event.pageY };
455
- $.address.value($(this).attr('data-deep-link'));
491
+ encoded_url = ajax.safeURL(ajax_url);
492
+ console.log('linkClicked 2: going to ' + ajax_url);
493
+ console.log('untouched url is ' + ajax_url + ', encoded is ' + encoded_url);
494
+ if ($.browser.msie) {
495
+ $.address.value(encoded_url);
496
+ }
497
+ else {
498
+ $.address.value(ajax_url);
499
+ }
456
500
  }
457
501
  return false;
458
502
  };
459
503
 
504
+ self.safeURL = function(url) {
505
+ if (decodeURI(url)==url) {
506
+ encoded_url = encodeURI(url);
507
+ } else {
508
+ encoded_url = url;
509
+ };
510
+ return encoded_url;
511
+ };
512
+
460
513
  /**
461
514
  * responseHandler
462
515
  *
@@ -466,17 +519,19 @@ var Ajax = function(options) {
466
519
  */
467
520
  self.responseHandler = function(responseText, textStatus, XMLHttpRequest) {
468
521
  var data = self.processResponseHeaders(XMLHttpRequest);
469
- var container = data.container === undefined ? $(self.default_container) : $(data.container);
470
522
 
471
- // Redirect? Let the JS execute. It will set the new window location.
472
- if (responseText && responseText.match(/try\s{\swindow\.location\.href/)) {
473
- jQuery.globalEval(responseText);
474
- return true;
475
- }
523
+ if (data.soft_redirect !== undefined) {
524
+ console.log('**** data.soft_redirect is ' + data.soft_redirect)
525
+ $.address.value(data.soft_redirect);
526
+ return false;
527
+ };
476
528
 
529
+ var container = data.container === undefined ? $(self.default_container) : $(data.container);
477
530
  /**
478
- * Extract the body
479
- */
531
+ * Full page response. The best we can do is to extract the body
532
+ * and display that. Additionally, if the container to update
533
+ * is present in the response, just use that.
534
+ */
480
535
  if (responseText.search(/<\s*body[^>]*>/) != -1) {
481
536
  var start = responseText.search(/<\s*body[^>]*>/);
482
537
  start += responseText.match(/<\s*body[^>]*>/)[0].length;
@@ -484,6 +539,11 @@ var Ajax = function(options) {
484
539
 
485
540
  console.log('Extracting body ['+start+'..'+end+'] chars');
486
541
  responseText = responseText.substr(start, end - start);
542
+
543
+ var body = $(responseText);
544
+ if (body.size() > 0 && body.find(container.selector).size() > 0) {
545
+ responseText = body.find(container.selector).html();
546
+ }
487
547
  }
488
548
 
489
549
  // Handle special header instructions
@@ -724,7 +784,7 @@ var Ajax = function(options) {
724
784
  };
725
785
 
726
786
  self.teaser = function(callback) {
727
- return new String(callback).slice(0,50);
787
+ return new String(callback).slice(0,200).replace(/\n/g, ' ');
728
788
  };
729
789
 
730
790
  /**
metadata CHANGED
@@ -3,10 +3,10 @@ name: ajax
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
+ - 1
6
7
  - 0
7
8
  - 1
8
- - 7
9
- version: 0.1.7
9
+ version: 1.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Karl Varga
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-28 00:00:00 -07:00
17
+ date: 2010-07-15 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency