ajax 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -10,11 +10,12 @@ As of May 2010 Ajax is being used live in production on kazaa.com[http://www.kaz
10
10
 
11
11
  == Changelog
12
12
 
13
+ * v1.1.5: Fix inclusion of +controller+ and +layout+ in <tt>Ajax-Info</tt> response header. Improve RSpec 1.* integration
13
14
  * v1.1.4: Fix RSpec 2 integration
14
15
  * v1.1.3: Guard against possible nil values for the redirect_to url and the referrers
15
16
  * v1.1.2: Fix Rails 3 render hook so it doesn't break rendering with no layout e.g. <tt>render :json => ...</tt>
16
17
  * v1.1.1: Backwards compatibility fix for Rails < 3
17
- * v1.1.0: Rails 3 supported!
18
+ * <b>v1.1.0: Rails 3 supported!</b>
18
19
 
19
20
  == Install
20
21
 
@@ -26,7 +27,7 @@ As of May 2010 Ajax is being used live in production on kazaa.com[http://www.kaz
26
27
 
27
28
  2. <tt>bundle</tt>
28
29
  3. <tt>rake ajax:install</tt>
29
- 4. The framework route will automatically be added to your application but if you have catch-all routes they will take precedence. If that is the case, you can add the route to <tt>config/routes.rb</tt> like so:
30
+ 4. Run <tt>rake routes</tt> to verify that the <tt>/ajax/framework</tt> path is being routed correctly. The route will automatically be added to your application but if you have catch-all routes they will take precedence, so if that is the case, you can add the route to <tt>config/routes.rb</tt> manually like so:
30
31
 
31
32
  YourAppName::Application.routes.draw do
32
33
  Ajax::Routes.draw(self)
@@ -58,11 +59,19 @@ As of May 2010 Ajax is being used live in production on kazaa.com[http://www.kaz
58
59
  Ajax::Routes.draw(map)
59
60
  end
60
61
 
62
+ 5. Run <tt>rake routes</tt> to verify that the <tt>/ajax/framework</tt> path is being routed correctly.
63
+
61
64
  <b> As a Plugin</b>
62
65
 
63
- ./script/plugin install http://github.com/kjvarga/ajax.git
66
+ 1. ./script/plugin install http://github.com/kjvarga/ajax.git
67
+ 2. Run <tt>rake routes</tt> to verify that the <tt>/ajax/framework</tt> path is being routed correctly.
68
+
69
+ === Rake Tasks
64
70
 
65
- === Sample Output
71
+ $ rake -T ajax
72
+ rake ajax:install # Install required Ajax files.
73
+ rake ajax:install:specs # Copy Ajax integration spec into spec/integration/ajax_spec.rb.
74
+ rake ajax:update:javascript # Overwrite public/javascripts/ajax.js with the latest version.
66
75
 
67
76
  $ rake ajax:install
68
77
  created: app/controllers/ajax_controller.rb
@@ -77,8 +86,15 @@ As of May 2010 Ajax is being used live in production on kazaa.com[http://www.kaz
77
86
  $ rake ajax:install:specs
78
87
  already exists: spec/integration/ajax_spec.rb
79
88
 
89
+ $ rake ajax:update:javascript
90
+ created: public/javascripts/ajax.js
91
+
80
92
  == Getting Started
81
93
 
94
+ 1. Include the JavaScript assets in your <tt>app/views/layouts/application.html.erb</tt> file. For example:
95
+
96
+ <%= javascript_include_tag 'jquery', 'jquery.address-1.3', 'ajax', 'application' %>
97
+
82
98
  1. Ajax looks for a layout to use for an Ajax request in <tt>app/views/layouts/ajax/</tt>. It looks for a layout with the same name as the default Rails layout for that action. So copy your existing layouts into <tt>layouts/ajax/</tt> and get them ready for Ajax by removing any excess HTML, leaving just the content that will be inserted into the container.
83
99
 
84
100
  Your main layout should contain a container element to receive page content. Typically this would be a container below your page header. If you don't have a static header, you can make the whole BODY element the container.
@@ -98,7 +114,7 @@ As of May 2010 Ajax is being used live in production on kazaa.com[http://www.kaz
98
114
 
99
115
  3. Add a <tt>data-deep-link</tt> attribute to links that you want to load using the Ajax framework. A jQuery live event automatically intercepts all clicks on links with this attribute and loads their content using the framework.
100
116
 
101
- By default all links that use the Rails link helpers will include this attribute. When a link with this attribute is clicked, content is requested using AJAX and goes through the Ajax framework. Rails receives a request for content at the <tt>data-deep-link</tt> location. The content is rendered using the default layout for the action but from the <tt>app/views/layouts/ajax</tt> directory. The rendered content is received client-side and special Ajax headers are processed. The content is then inserted into the specified container, or the <tt>default_container</tt> defined in your Ajax JS class above.
117
+ By default all links that use the Rails link helpers will include this attribute, so you won't need to do anything. When a link with this attribute is clicked, content is requested using AJAX and goes through the Ajax framework. Rails receives a request for content at the <tt>data-deep-link</tt> location. The content is rendered using the default layout for the action but from the <tt>app/views/layouts/ajax</tt> directory. The rendered content is received client-side and special Ajax headers are processed. The content is then inserted into the specified container, or the <tt>default_container</tt> defined in your Ajax JS class above.
102
118
 
103
119
  4. To submit forms using the Ajax framework, or to manually request content using various request methods, you can call <tt>window.ajax.loadPage</tt>, passing in options like _url_, _method_ and _data_.
104
120
 
@@ -451,9 +467,25 @@ To ease some of the pain, observe some of the following advice:
451
467
 
452
468
  == Testing
453
469
 
454
- * We use RSpec
455
- * See <tt>Ajax::Spec::Helpers</tt> and <tt>Ajax::Spec::Extension</tt> {in the rdocs}[http://rdoc.info/projects/kjvarga/ajax]
456
- * Copy <tt>ajax/spec/integration/ajax_spec.rb</tt> into your project to ensure that the Ajax integration always works.
470
+ Ajax comes with RSpec integration tests for RSpec 1 and 2 and <tt>ActiveSupport::TestCase</tt>. You don't have to add anything to your <tt>spec/spec_helper.rb</tt> file. Just run the rake task to copy
471
+ the integration spec into <tt>spec/integration/ajax_spec.rb</tt>:
472
+
473
+ rake ajax:install:specs
474
+
475
+ If you want to do add your own specs, you can make use of helper methods defined
476
+ in the <tt>Ajax::RSpec::Extension</tt> module. To make these methods available
477
+ to all your specs just add a require to your <tt>spec/spec_helper.rb</tt>:
478
+
479
+ # spec/spec_helper.rb
480
+ require 'ajax/rspec'
481
+
482
+ If <tt>ajax/rspec</tt> has been required before the testing framework has
483
+ been defined (Spec, RSpec or ActiveSupport::TestCase) the extension methods will
484
+ not have been integrated. In this case you can force re-integration by calling:
485
+
486
+ Ajax::RSpec.setup
487
+
488
+ * See <tt>Ajax::RSpec::Helpers</tt> and <tt>Ajax::RSpec::Extension</tt> {in the rdocs}[http://rdoc.info/projects/kjvarga/ajax]
457
489
 
458
490
  == Contributions
459
491
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.4
1
+ 1.1.5
data/lib/ajax.rb CHANGED
@@ -2,7 +2,6 @@ require 'rack-ajax'
2
2
  require 'ajax/helpers'
3
3
  require 'ajax/application'
4
4
  require 'ajax/routes'
5
- require 'ajax/rspec'
6
5
  require 'pathname'
7
6
  # require railties at the end
8
7
 
@@ -106,14 +106,9 @@ module Ajax
106
106
  ajax_layout = _layout_for_ajax(default)
107
107
  ajax_layout = ajax_layout.path_without_format_and_extension unless ajax_layout.nil?
108
108
  options[:layout] = ajax_layout unless ajax_layout.nil?
109
-
110
- # Send the current layout and controller in a custom response header
111
- Ajax.set_header(response, :layout, ajax_layout)
112
- Ajax.set_header(response, :controller, self.class.controller_name)
113
109
  end
114
110
  render_without_ajax(options, extra_options, &block)
115
111
  end
116
-
117
112
  end
118
113
 
119
114
  module Rails3
@@ -145,11 +140,16 @@ module Ajax
145
140
 
146
141
  # Convert the Ajax-Info hash to JSON before the request is sent.
147
142
  # Invoked as an after filter.
143
+ #
144
+ # Adds the current +layout+ and +controller+ to the hash.
145
+ # These values will be sent in future requests using the Ajax-Info header.
146
+ #
147
+ # +controller+ is the result of calling ActionController#controller_name, so
148
+ # if your controller is ApplicationController the value will be <tt>'application'</tt>.
148
149
  def serialize_ajax_info
149
- case response.headers['Ajax-Info']
150
- when Hash
151
- response.headers['Ajax-Info'] = response.headers['Ajax-Info'].to_json
152
- end
150
+ Ajax.set_header(response, :layout, Ajax.app.rails?(3) ? _layout : active_layout)
151
+ Ajax.set_header(response, :controller, self.class.controller_name)
152
+ response.headers['Ajax-Info'] = Ajax.send(:serialize_hash, response.headers['Ajax-Info'])
153
153
  end
154
154
 
155
155
  # Perform special processing on the response if we need to.
@@ -27,32 +27,25 @@ module Ajax
27
27
  headers = object.is_a?(Hash) ? object : object.headers # ::ActionController::Response
28
28
  key = key.to_s
29
29
 
30
- info = case headers["Ajax-Info"]
31
- when String
32
- JSON.parse(headers["Ajax-Info"]) rescue {}
33
- when Hash
34
- headers["Ajax-Info"]
35
- else
36
- {}
37
- end
30
+ headers["Ajax-Info"] = serialize_hash(headers["Ajax-Info"]) do |info|
31
+ # Deep merge hashes
32
+ if info.has_key?(key) &&
33
+ value.is_a?(Hash) &&
34
+ info[key].is_a?(Hash)
35
+ value = value.stringify_keys!
36
+ value = info[key].merge(value, &DEEP_MERGE)
37
+ end
38
38
 
39
- # Deep merge hashes
40
- if info.has_key?(key) &&
41
- value.is_a?(Hash) &&
42
- info[key].is_a?(Hash)
43
- value = value.stringify_keys!
44
- value = info[key].merge(value, &DEEP_MERGE)
45
- end
39
+ # Concat arrays
40
+ if info.has_key?(key) &&
41
+ value.is_a?(Array) &&
42
+ info[key].is_a?(Array)
43
+ value = info[key].concat(value)
44
+ end
46
45
 
47
- # Concat arrays
48
- if info.has_key?(key) &&
49
- value.is_a?(Array) &&
50
- info[key].is_a?(Array)
51
- value = info[key].concat(value)
46
+ # Set the value for this key
47
+ info[key] = value
52
48
  end
53
-
54
- info[key] = value
55
- headers["Ajax-Info"] = info.to_json
56
49
  end
57
50
 
58
51
  # Return the value at key <tt>key</tt> from the <tt>Ajax-Info</tt> header
@@ -62,17 +55,7 @@ module Ajax
62
55
  # <tt>key</tt> Symbol or String hash key, converted to String
63
56
  def get_header(object, key)
64
57
  headers = object.is_a?(Hash) ? object : object.headers # ::ActionController::Request
65
- key = key.to_s
66
-
67
- info = case headers["Ajax-Info"]
68
- when String
69
- JSON.parse(headers["Ajax-Info"]) rescue {}
70
- when Hash
71
- headers["Ajax-Info"]
72
- else
73
- {}
74
- end
75
- info[key]
58
+ unserialize_hash(headers["Ajax-Info"])[key.to_s]
76
59
  end
77
60
 
78
61
  # Set one or more paths that can be accessed directly without the AJAX framework.
@@ -111,6 +94,27 @@ module Ajax
111
94
  !!excluded.match(path)
112
95
  end)
113
96
  end
97
+
98
+ # Return JSON given a Hash or JSON string. If a block is given, yields
99
+ # the Hash to the block so that the block can modify it before it is
100
+ # converted to JSON.
101
+ def serialize_hash(hash, &block)
102
+ info = unserialize_hash(hash)
103
+ yield info if block_given?
104
+ info.to_json
105
+ end
106
+
107
+ # Return a Hash given JSON or a Hash.
108
+ def unserialize_hash(hash)
109
+ case hash
110
+ when String
111
+ JSON.parse(hash) rescue {}
112
+ when Hash
113
+ hash
114
+ else
115
+ {}
116
+ end
117
+ end
114
118
  end
115
119
  end
116
- end
120
+ end
data/lib/ajax/rspec.rb CHANGED
@@ -1,3 +1,31 @@
1
1
  require 'ajax/rspec/extension'
2
2
  require 'ajax/rspec/helpers'
3
- require 'ajax/rspec/integration'
3
+
4
+ # RSpec integration. Add a before filter to disable Ajax before all tests.
5
+ # Make the methods in Ajax::RSpec::Extension available in your example groups.
6
+ #
7
+ # Call <tt>Ajax::RSpec.setup</tt> to retry adding the test integration.
8
+ # This can be useful if RSpec is not defined at the time that Ajax is initialized.
9
+ module Ajax::RSpec
10
+ def self.setup
11
+ if defined?(::RSpec)
12
+ ::RSpec.configure do |c|
13
+ c.include(Ajax::RSpec::Extension)
14
+ c.before :all do
15
+ Ajax.enabled = false
16
+ end
17
+ end
18
+ elsif defined?(::Spec::Runner)
19
+ ::Spec::Runner.configure do |c|
20
+ c.include(Ajax::RSpec::Extension)
21
+ c.before :all do
22
+ Ajax.enabled = false
23
+ end
24
+ end
25
+ elsif defined?(ActiveSupport::TestCase)
26
+ ActiveSupport::TestCase.send(:include, Ajax::RSpec::Extension) unless ActiveSupport::TestCase.include?(Ajax::RSpec::Extension)
27
+ end
28
+ end
29
+ end
30
+
31
+ Ajax::RSpec.setup
@@ -1,25 +0,0 @@
1
- # Only require integration module when extensions are included.
2
- # RSpec integration. Add a before filter to disable Ajax before all tests.
3
- module Ajax::RSpec::Integration
4
- if defined?(::RSpec)
5
- ::RSpec.configure do |c|
6
- c.include(Ajax::RSpec::Extension)
7
- c.before :all do
8
- disable_ajax
9
- end
10
- end
11
- elsif defined?(::Spec)
12
- ::Spec::Runner.configure do |c|
13
- c.before :all do
14
- disable_ajax
15
- end
16
- end
17
- end
18
- end
19
-
20
- # ActiveSupport::TestCase integration
21
- module ActiveSupport
22
- class TestCase
23
- include Ajax::RSpec::Extension
24
- end
25
- end
data/lib/rack-ajax.rb CHANGED
@@ -27,16 +27,10 @@ module Rack
27
27
  return @app.call(env) unless ::Ajax.is_enabled?
28
28
 
29
29
  # Parse the Ajax-Info header
30
- if env["HTTP_AJAX_INFO"].nil?
31
- env["Ajax-Info"] = {}
32
- elsif env["HTTP_AJAX_INFO"].is_a?(String)
33
- env["Ajax-Info"] = (JSON.parse(env['HTTP_AJAX_INFO']) rescue {})
34
- end
35
-
30
+ env["Ajax-Info"] = ::Ajax.unserialize_hash(env['HTTP_AJAX_INFO'])
36
31
  @parser = Parser.new(env)
37
32
  rack_response = @parser.instance_eval(&@decision_tree)
38
33
 
39
-
40
34
  # Clear the value of session[:redirected_to]
41
35
  unless env['rack.session'].nil?
42
36
  env['rack.session']['redirected_to'] = env['rack.session'][:redirected_to] = nil
@@ -69,4 +63,4 @@ module Rack
69
63
  env.to_yaml(:Encoding => :Utf8)
70
64
  end
71
65
  end
72
- end
66
+ end
@@ -40,7 +40,7 @@ END
40
40
  end
41
41
 
42
42
  namespace :install do
43
- desc "Copy Ajax integration spec tests into spec/integration."
43
+ desc "Copy Ajax integration spec into spec/integration/ajax_spec.rb."
44
44
  task :specs do
45
45
  file = (Ajax.app.root + 'spec/integration/ajax_spec.rb').to_s
46
46
  if File.exist?(file)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ajax
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 4
10
- version: 1.1.4
9
+ - 5
10
+ version: 1.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Karl Varga
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-06 00:00:00 -07:00
18
+ date: 2011-05-08 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency