ajax 1.0.8 → 1.1.0
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/Gemfile +5 -3
- data/Gemfile.lock +34 -8
- data/README.rdoc +76 -63
- data/Rakefile +3 -1
- data/VERSION +1 -1
- data/app/controllers/ajax_controller.rb +6 -0
- data/init.rb +6 -0
- data/lib/ajax.rb +13 -24
- data/lib/ajax/action_controller.rb +120 -65
- data/lib/ajax/application.rb +62 -0
- data/lib/ajax/helpers/request_helper.rb +2 -2
- data/lib/ajax/helpers/task_helper.rb +2 -2
- data/lib/ajax/railtie.rb +25 -1
- data/lib/ajax/routes.rb +7 -3
- data/lib/ajax/rspec.rb +2 -0
- data/lib/ajax/{spec → rspec}/extension.rb +4 -11
- data/lib/ajax/{spec → rspec}/helpers.rb +10 -10
- data/lib/ajax/rspec/integration.rb +24 -0
- data/lib/rack-ajax.rb +1 -1
- data/lib/rack-ajax/parser.rb +2 -2
- data/public/javascripts/{jquery.address-1.2rc.js → jquery.address-1.3.js} +340 -197
- data/public/javascripts/jquery.address-1.3.min.js +32 -0
- data/rails/init.rb +1 -4
- data/rails/install.rb +1 -1
- data/rails/routes.rb +6 -0
- data/tasks/ajax_tasks.rake +11 -2
- metadata +41 -25
- data/app/views/ajax/_redirect_with_fragment.html.erb +0 -19
- data/public/javascripts/jquery.address-1.1.js +0 -450
- data/public/javascripts/jquery.address-1.1.min.js +0 -11
- data/public/javascripts/jquery.address-1.2.js +0 -528
- data/public/javascripts/jquery.address-1.2.min.js +0 -25
- data/public/javascripts/jquery.address-1.2rc.min.js +0 -27
- data/rails/uninstall.rb +0 -1
- data/spec/ajax/ajax_spec.rb +0 -23
- data/spec/ajax/helpers_spec.rb +0 -121
- data/spec/ajax/request_helper_spec.rb +0 -52
- data/spec/ajax/tasks_spec.rb +0 -48
- data/spec/integration/ajax_spec.rb +0 -146
- data/spec/rack-ajax/parser_spec.rb +0 -62
- data/spec/spec.opts +0 -1
- data/spec/spec_helper.rb +0 -17
- data/spec/support/file_macros.rb +0 -28
- data/spec/support/query_count.rb +0 -0
- data/spec/support/response_helpers.rb +0 -7
data/Gemfile
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
|
-
gem 'activesupport', '=
|
4
|
-
gem 'actionpack', '=
|
3
|
+
gem 'activesupport', '=3.0.4'
|
4
|
+
gem 'actionpack', '=3.0.4', :require => 'action_controller'
|
5
5
|
gem 'jeweler', '=1.4.0'
|
6
|
+
gem 'ajax', :path => "./"
|
7
|
+
|
6
8
|
gem 'ruby-debug', '=0.10.3'
|
7
9
|
gem 'ruby-debug-base', '=0.10.3'
|
8
10
|
gem 'rspec', '~>1.3.1', :require => 'spec'
|
11
|
+
gem 'mocha', '~>0.9.10'
|
9
12
|
gem 'test-unit', '=1.2.3'
|
10
|
-
gem 'ajax', :path => "./"
|
data/Gemfile.lock
CHANGED
@@ -1,27 +1,51 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ./
|
3
3
|
specs:
|
4
|
-
ajax (1.0
|
4
|
+
ajax (1.1.0)
|
5
|
+
json
|
6
|
+
rack
|
5
7
|
|
6
8
|
GEM
|
7
9
|
remote: http://rubygems.org/
|
8
10
|
specs:
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
abstract (1.0.0)
|
12
|
+
actionpack (3.0.4)
|
13
|
+
activemodel (= 3.0.4)
|
14
|
+
activesupport (= 3.0.4)
|
15
|
+
builder (~> 2.1.2)
|
16
|
+
erubis (~> 2.6.6)
|
17
|
+
i18n (~> 0.4)
|
18
|
+
rack (~> 1.2.1)
|
19
|
+
rack-mount (~> 0.6.13)
|
20
|
+
rack-test (~> 0.5.7)
|
21
|
+
tzinfo (~> 0.3.23)
|
22
|
+
activemodel (3.0.4)
|
23
|
+
activesupport (= 3.0.4)
|
24
|
+
builder (~> 2.1.2)
|
25
|
+
i18n (~> 0.4)
|
26
|
+
activesupport (3.0.4)
|
27
|
+
builder (2.1.2)
|
13
28
|
columnize (0.3.2)
|
29
|
+
erubis (2.6.6)
|
30
|
+
abstract (>= 1.0.0)
|
14
31
|
gemcutter (0.7.0)
|
15
32
|
git (1.2.5)
|
16
33
|
hoe (2.9.1)
|
17
34
|
rake (>= 0.8.7)
|
35
|
+
i18n (0.5.0)
|
18
36
|
jeweler (1.4.0)
|
19
37
|
gemcutter (>= 0.1.0)
|
20
38
|
git (>= 1.2.5)
|
21
39
|
rubyforge (>= 2.0.0)
|
40
|
+
json (1.5.1)
|
22
41
|
json_pure (1.5.1)
|
23
42
|
linecache (0.43)
|
24
|
-
|
43
|
+
mocha (0.9.12)
|
44
|
+
rack (1.2.2)
|
45
|
+
rack-mount (0.6.14)
|
46
|
+
rack (>= 1.0.0)
|
47
|
+
rack-test (0.5.7)
|
48
|
+
rack (>= 1.0)
|
25
49
|
rake (0.8.7)
|
26
50
|
rspec (1.3.1)
|
27
51
|
ruby-debug (0.10.3)
|
@@ -33,15 +57,17 @@ GEM
|
|
33
57
|
json_pure (>= 1.1.7)
|
34
58
|
test-unit (1.2.3)
|
35
59
|
hoe (>= 1.5.1)
|
60
|
+
tzinfo (0.3.25)
|
36
61
|
|
37
62
|
PLATFORMS
|
38
63
|
ruby
|
39
64
|
|
40
65
|
DEPENDENCIES
|
41
|
-
actionpack (=
|
42
|
-
activesupport (=
|
66
|
+
actionpack (= 3.0.4)
|
67
|
+
activesupport (= 3.0.4)
|
43
68
|
ajax!
|
44
69
|
jeweler (= 1.4.0)
|
70
|
+
mocha (~> 0.9.10)
|
45
71
|
rspec (~> 1.3.1)
|
46
72
|
ruby-debug (= 0.10.3)
|
47
73
|
ruby-debug-base (= 0.10.3)
|
data/README.rdoc
CHANGED
@@ -8,6 +8,71 @@ The beauty of Ajax is that your Rails application only ever sees traditional req
|
|
8
8
|
|
9
9
|
As of May 2010 Ajax is being used live in production on kazaa.com[http://www.kazaa.com]. Try it out and create some wicked playlists while you're at it!
|
10
10
|
|
11
|
+
== Changelog
|
12
|
+
|
13
|
+
* v1.1.0: Rails 3 supported!
|
14
|
+
|
15
|
+
== Install
|
16
|
+
|
17
|
+
=== Rails 3
|
18
|
+
|
19
|
+
1. Add the gem to your <tt>Gemspec</tt>
|
20
|
+
|
21
|
+
gem 'ajax'
|
22
|
+
|
23
|
+
2. <tt>bundle</tt>
|
24
|
+
3. <tt>rake ajax:install</tt>
|
25
|
+
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:
|
26
|
+
|
27
|
+
YourAppName::Application.routes.draw do
|
28
|
+
Ajax::Routes.draw(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
=== Rails 2
|
32
|
+
|
33
|
+
<b> As a Gem</b>
|
34
|
+
|
35
|
+
1. Add the gem as a dependency in your <tt>config/environment.rb</tt>
|
36
|
+
|
37
|
+
config.gem 'ajax'
|
38
|
+
|
39
|
+
2. <tt>rake gems:install</tt>
|
40
|
+
|
41
|
+
3. Add to your <tt>Rakefile</tt>
|
42
|
+
|
43
|
+
begin
|
44
|
+
require 'ajax/tasks'
|
45
|
+
rescue Exception => e
|
46
|
+
puts "Warning, couldn't load gem tasks: #{e.message}! Skipping..."
|
47
|
+
end
|
48
|
+
|
49
|
+
4. <tt>rake ajax:install</tt> to install required files. This command is safe to run multiple times. It will skip existing files.
|
50
|
+
|
51
|
+
4. Add a route for the framework path to your <tt>config/routes.rb</tt>
|
52
|
+
|
53
|
+
ActionController::Routing::Routes.draw do |map|
|
54
|
+
Ajax::Routes.draw(map)
|
55
|
+
end
|
56
|
+
|
57
|
+
<b> As a Plugin</b>
|
58
|
+
|
59
|
+
./script/plugin install http://github.com/kjvarga/ajax.git
|
60
|
+
|
61
|
+
=== Sample Output
|
62
|
+
|
63
|
+
$ rake ajax:install
|
64
|
+
created: app/controllers/ajax_controller.rb
|
65
|
+
created: app/views/ajax/framework.html.erb
|
66
|
+
created: config/initializers/ajax.rb
|
67
|
+
created: public/javascripts/ajax.js
|
68
|
+
created: public/javascripts/jquery.address-1.3.js
|
69
|
+
created: public/javascripts/jquery.address-1.3.min.js
|
70
|
+
created: public/javascripts/jquery.json-2.2.min.js
|
71
|
+
created: public/images/ajax-loading.gif
|
72
|
+
|
73
|
+
$ rake ajax:install:specs
|
74
|
+
already exists: spec/integration/ajax_spec.rb
|
75
|
+
|
11
76
|
== Getting Started
|
12
77
|
|
13
78
|
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.
|
@@ -66,62 +131,10 @@ As of May 2010 Ajax is being used live in production on kazaa.com[http://www.kaz
|
|
66
131
|
7. Specify paths that should bypass the Ajax framework. I.e. accessing these paths using a full URL (not a hashed URL), will render the page like in a traditional Rails app. If that path was not excepted the Ajax framework would have forced a redirect to the hashed version of the URL before rendering the page contents using Ajax. See <i>Excepted Links</i>.
|
67
132
|
|
68
133
|
For example:
|
69
|
-
|
134
|
+
|
70
135
|
# config/initializers/ajax.rb
|
71
136
|
Ajax.exclude_paths %w[ /login /logout /signup ]
|
72
137
|
|
73
|
-
== Install
|
74
|
-
|
75
|
-
=== Rails 3 (untested)
|
76
|
-
|
77
|
-
1. Add the gem to your <tt>Gemspec</tt>
|
78
|
-
|
79
|
-
gem 'ajax'
|
80
|
-
|
81
|
-
2. <tt>bundle install</tt>
|
82
|
-
3. <tt>rake ajax:install</tt>
|
83
|
-
|
84
|
-
=== Rails 2.x.x
|
85
|
-
|
86
|
-
<b> As a Gem</b>
|
87
|
-
|
88
|
-
1. Add the gem as a dependency in your <tt>config/environment.rb</tt>
|
89
|
-
|
90
|
-
config.gem 'ajax'
|
91
|
-
|
92
|
-
2. <tt>rake gems:install</tt>
|
93
|
-
|
94
|
-
3. Add to your <tt>Rakefile</tt>
|
95
|
-
|
96
|
-
begin
|
97
|
-
require 'ajax/tasks'
|
98
|
-
rescue Exception => e
|
99
|
-
puts "Warning, couldn't load gem tasks: #{e.message}! Skipping..."
|
100
|
-
end
|
101
|
-
|
102
|
-
4. <tt>rake ajax:install</tt> to install required files. This command is safe to run multiple times. It will skip existing files.
|
103
|
-
|
104
|
-
<b> As a Plugin</b>
|
105
|
-
|
106
|
-
./script/plugin install http://github.com/kjvarga/ajax.git
|
107
|
-
|
108
|
-
=== Sample Output
|
109
|
-
|
110
|
-
$ rake ajax:install
|
111
|
-
created: app/controllers/ajax_controller.rb
|
112
|
-
created: app/views/ajax/framework.html.erb
|
113
|
-
created: config/initializers/ajax.rb
|
114
|
-
created: public/javascripts/ajax.js
|
115
|
-
created: public/javascripts/jquery.address-1.2rc.js
|
116
|
-
created: public/javascripts/jquery.address-1.2rc.min.js
|
117
|
-
created: public/javascripts/jquery.json-2.2.min.js
|
118
|
-
created: public/images/ajax-loading.gif
|
119
|
-
|
120
|
-
$ rake ajax:install
|
121
|
-
skipped: app/controllers/ajax_controller.rb exists!
|
122
|
-
skipped: app/views/ajax/framework.html.erb exists!
|
123
|
-
...
|
124
|
-
|
125
138
|
== Introduction
|
126
139
|
|
127
140
|
=== Features and Support
|
@@ -188,28 +201,28 @@ If you need to expose external APIs you can do so using a regular expression tha
|
|
188
201
|
|
189
202
|
== Compatibility
|
190
203
|
|
191
|
-
You must be running <b>jQuery 1.4.2</b> to use this plugin. Sorry, Prototype users.
|
204
|
+
You must be running <b>jQuery >= 1.4.2</b> to use this plugin. Sorry, Prototype users.
|
192
205
|
|
193
206
|
The following JavaScript libraries are required and included in the plugin:
|
194
207
|
|
195
|
-
* {jQuery Address 1.
|
208
|
+
* {jQuery Address 1.3}[http://www.asual.com/jquery/address/]
|
196
209
|
* jQuery JSON 2.2
|
197
210
|
|
198
211
|
=== Ruby and Rails:
|
199
212
|
|
200
|
-
|
201
|
-
|
213
|
+
* Rails 2.3.4, 2.3.5, 3.0.5, 3.0.7
|
214
|
+
* Ruby 1.8.7, 1.9.1, 1.9.2
|
202
215
|
|
203
216
|
=== Browsers:
|
204
217
|
|
205
218
|
(See {jQuery address supported browsers}[http://www.asual.com/jquery/address/docs/].)
|
206
219
|
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
220
|
+
* Internet Explorer 6.0+
|
221
|
+
* Mozilla Firefox 1.0+
|
222
|
+
* Safari 1.3+
|
223
|
+
* Opera 9.5+
|
224
|
+
* Chrome 1.0+
|
225
|
+
* Camino 1.0+
|
213
226
|
|
214
227
|
= Documentation
|
215
228
|
|
data/Rakefile
CHANGED
@@ -10,9 +10,11 @@ begin
|
|
10
10
|
gem.email = "kjvarga@gmail.com"
|
11
11
|
gem.homepage = "http://github.com/kjvarga/ajax"
|
12
12
|
gem.authors = ["Karl Varga"]
|
13
|
-
gem.files = FileList["[A-Z]*", "{app,config,lib,public,rails,
|
13
|
+
gem.files = FileList["[A-Z]*", "init.rb", "{app,config,lib,public,rails,tasks}/**/*"] - FileList['spec/rails*/**/*']
|
14
14
|
gem.test_files = []
|
15
15
|
gem.add_development_dependency "rspec"
|
16
|
+
gem.add_dependency "json"
|
17
|
+
gem.add_dependency "rack"
|
16
18
|
end
|
17
19
|
Jeweler::GemcutterTasks.new
|
18
20
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/init.rb
ADDED
data/lib/ajax.rb
CHANGED
@@ -1,15 +1,22 @@
|
|
1
|
+
require 'rack-ajax'
|
1
2
|
require 'ajax/helpers'
|
3
|
+
require 'ajax/application'
|
4
|
+
require 'ajax/routes'
|
5
|
+
require 'ajax/rspec'
|
6
|
+
require 'pathname'
|
7
|
+
# require railties at the end
|
2
8
|
|
3
9
|
module Ajax
|
4
10
|
include Ajax::Helpers
|
5
11
|
|
6
12
|
class << self
|
13
|
+
attr_accessor :app
|
7
14
|
attr_writer :logger, :framework_path
|
8
15
|
end
|
9
16
|
|
10
17
|
# Return the full path to the root of the Ajax plugin/gem directory.
|
11
18
|
def self.root
|
12
|
-
@root ||=
|
19
|
+
@root ||= Pathname.new(File.expand_path('../../', __FILE__))
|
13
20
|
end
|
14
21
|
|
15
22
|
# Return a logger instance.
|
@@ -88,29 +95,11 @@ module Ajax
|
|
88
95
|
@mocked = !!value
|
89
96
|
end
|
90
97
|
|
91
|
-
# Installs Ajax for Rails.
|
92
|
-
#
|
93
|
-
# This method is called by <tt>init.rb</tt>, which is run by Rails on startup.
|
94
|
-
#
|
95
|
-
# Customize rendering. Include custom headers and don't render the layout for AJAX.
|
96
|
-
# Insert the Rack::Ajax middleware to rewrite and handle requests.
|
97
|
-
# Add custom attributes to outgoing links.
|
98
|
-
def self.install_for_rails
|
99
|
-
if defined?(Rails)
|
100
|
-
Ajax.logger = Rails.logger
|
101
|
-
|
102
|
-
# Customize rendering. Include custom headers and don't render the layout for AJAX.
|
103
|
-
::ActionController::Base.send(:include, Ajax::ActionController)
|
104
|
-
|
105
|
-
# Insert the Rack::Ajax middleware to rewrite and handle requests
|
106
|
-
::ActionController::Dispatcher.middleware.insert_before(Rack::Head, Rack::Ajax)
|
107
|
-
|
108
|
-
# Add custom attributes to outgoing links
|
109
|
-
::ActionView::Base.send(:include, Ajax::ActionView)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
98
|
def self.version
|
114
99
|
@version ||= File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).strip
|
115
100
|
end
|
116
|
-
|
101
|
+
|
102
|
+
self.app = Ajax::Application.new
|
103
|
+
end
|
104
|
+
|
105
|
+
require 'ajax/railtie' if Ajax.app.rails?(3)
|
@@ -2,17 +2,14 @@ module Ajax
|
|
2
2
|
module ActionController
|
3
3
|
def self.included(klass)
|
4
4
|
klass.class_eval do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
append_after_filter :process_response_headers
|
5
|
+
extend MirrorMethods
|
6
|
+
include MirrorMethods
|
7
|
+
after_filter :serialize_ajax_info
|
8
|
+
include InstanceMethods
|
11
9
|
end
|
12
|
-
klass.extend(ClassMethods)
|
13
10
|
end
|
14
11
|
|
15
|
-
module
|
12
|
+
module MirrorMethods
|
16
13
|
|
17
14
|
# Set a custom response header if the request is AJAX.
|
18
15
|
#
|
@@ -46,13 +43,23 @@ module Ajax
|
|
46
43
|
end
|
47
44
|
end
|
48
45
|
|
49
|
-
|
46
|
+
module InstanceMethods
|
47
|
+
protected
|
48
|
+
|
49
|
+
if Ajax.app.rails?(3)
|
50
|
+
# Rails 3 hook. Rails < 3 is handled using redirect_to_full_url. See
|
51
|
+
# those docs for info.
|
52
|
+
def redirect_to(url={}, status={})
|
53
|
+
super
|
54
|
+
self.location = nil if _ajax_redirect(url, status) # clear any location set by super
|
55
|
+
end
|
56
|
+
end
|
50
57
|
|
51
58
|
# Redirect to hashed URLs unless the path is excepted.
|
52
59
|
#
|
53
60
|
# Store the URL that we are redirecting to in the session.
|
54
61
|
# If we then have a request for the root URL we know
|
55
|
-
# to render this URL
|
62
|
+
# to render this URL instead.
|
56
63
|
#
|
57
64
|
# If redirecting back to the referer, use the referer
|
58
65
|
# in the Ajax-Info header because it includes the
|
@@ -61,9 +68,17 @@ module Ajax
|
|
61
68
|
#
|
62
69
|
# For AJAX requests, respond with an AJAX-suitable
|
63
70
|
# redirect.
|
64
|
-
|
65
|
-
|
66
|
-
|
71
|
+
#
|
72
|
+
# This method only applies to Rails < 3
|
73
|
+
def redirect_to_full_url(url, status)
|
74
|
+
super unless _ajax_redirect(url, status) # Only call super if we didn't handle it
|
75
|
+
end
|
76
|
+
|
77
|
+
# Perform special processing on the response if we need to.
|
78
|
+
# Return true if an Ajax "redirect" was performed, and false
|
79
|
+
# otherwise.
|
80
|
+
def _ajax_redirect(url, status)
|
81
|
+
return false unless Ajax.is_enabled?
|
67
82
|
|
68
83
|
special_redirect = false
|
69
84
|
original_url = url
|
@@ -103,7 +118,26 @@ module Ajax
|
|
103
118
|
if special_redirect
|
104
119
|
session[:redirected_to] = nil
|
105
120
|
Ajax.logger.info("[ajax] returning special redirect JS")
|
106
|
-
|
121
|
+
self.response_body = <<END
|
122
|
+
<script type="text/javascript">
|
123
|
+
var url = #{url.to_json};
|
124
|
+
var hash = document.location.hash;
|
125
|
+
|
126
|
+
// Remove leading # from the fragment
|
127
|
+
if (hash.charAt(0) == '#') {
|
128
|
+
hash = hash.substr(1);
|
129
|
+
}
|
130
|
+
|
131
|
+
// Remove leading / from the fragment if the URL already ends in a /
|
132
|
+
// This prevents double-slashes. Note we can't just replace all
|
133
|
+
// double-slashes because the protocol includes //.
|
134
|
+
if (url.charAt(url.length - 1) == '/' && hash.charAt(0) == '/') {
|
135
|
+
hash = hash.substr(1);
|
136
|
+
}
|
137
|
+
|
138
|
+
document.location.href = url + hash;
|
139
|
+
</script>
|
140
|
+
END
|
107
141
|
else
|
108
142
|
session[:redirected_to] = url
|
109
143
|
if request.xhr? && Ajax.is_hashed_url?(url)
|
@@ -111,20 +145,22 @@ module Ajax
|
|
111
145
|
redirect_path = URI.parse(url).select(:fragment).first
|
112
146
|
Ajax.logger.info("[ajax] redirect path is #{redirect_path}")
|
113
147
|
Ajax.set_header(response, :soft_redirect, redirect_path)
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
148
|
+
self.response_body = <<END
|
149
|
+
<script type="text/javascript">
|
150
|
+
window.location.href = #{url.to_json};
|
151
|
+
</script>
|
152
|
+
END
|
119
153
|
else
|
120
154
|
Ajax.logger.info("[ajax] not detecting we are xhr. Hard redirect!")
|
121
|
-
|
155
|
+
return false
|
122
156
|
end
|
123
157
|
end
|
158
|
+
true
|
124
159
|
end
|
125
160
|
|
126
161
|
# Convert the Ajax-Info hash to JSON before the request is sent.
|
127
|
-
|
162
|
+
# Invoked as an after filter.
|
163
|
+
def serialize_ajax_info
|
128
164
|
case response.headers['Ajax-Info']
|
129
165
|
when Hash
|
130
166
|
response.headers['Ajax-Info'] = response.headers['Ajax-Info'].to_json
|
@@ -134,63 +170,82 @@ module Ajax
|
|
134
170
|
#
|
135
171
|
# Intercept rendering to customize the headers and layout handling
|
136
172
|
#
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
options
|
146
|
-
|
147
|
-
options
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
173
|
+
if !Ajax.app.rails?(3)
|
174
|
+
def render(options = nil, extra_options = {}, &block)
|
175
|
+
return super unless Ajax.is_enabled?
|
176
|
+
|
177
|
+
original_args = [options, extra_options]
|
178
|
+
if request.xhr?
|
179
|
+
|
180
|
+
# Options processing taken from ActionController::Base#render
|
181
|
+
if options.nil?
|
182
|
+
options = { :template => default_template, :layout => true }
|
183
|
+
elsif options == :update
|
184
|
+
options = extra_options.merge({ :update => true })
|
185
|
+
elsif options.is_a?(String) || options.is_a?(Symbol)
|
186
|
+
case options.to_s.index('/')
|
187
|
+
when 0
|
188
|
+
extra_options[:file] = options
|
189
|
+
when nil
|
190
|
+
extra_options[:action] = options
|
191
|
+
else
|
192
|
+
extra_options[:template] = options
|
193
|
+
end
|
194
|
+
options = extra_options
|
195
|
+
elsif !options.is_a?(Hash)
|
196
|
+
extra_options[:partial] = options
|
197
|
+
options = extra_options
|
156
198
|
end
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
199
|
+
|
200
|
+
default = pick_layout(options)
|
201
|
+
default = default.path_without_format_and_extension unless default.nil?
|
202
|
+
ajax_layout = _layout_for_ajax(default)
|
203
|
+
ajax_layout = ajax_layout.path_without_format_and_extension unless ajax_layout.nil?
|
204
|
+
options[:layout] = ajax_layout unless ajax_layout.nil?
|
205
|
+
|
206
|
+
# Send the current layout and controller in a custom response header
|
207
|
+
Ajax.set_header(response, :layout, ajax_layout)
|
208
|
+
Ajax.set_header(response, :controller, self.class.controller_name)
|
161
209
|
end
|
210
|
+
super(options, extra_options, &block)
|
211
|
+
end
|
212
|
+
else
|
213
|
+
def render_to_body(options = {})
|
214
|
+
return super if !request.xhr? || !Ajax.is_enabled?
|
215
|
+
_process_options(options)
|
162
216
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
ajax_layout = ajax_layout.path_without_format_and_extension unless ajax_layout.nil?
|
167
|
-
options[:layout] = ajax_layout unless ajax_layout.nil?
|
217
|
+
if ajax_layout = _layout_for_ajax(options[:layout])
|
218
|
+
options[:layout] = ajax_layout.virtual_path
|
219
|
+
end
|
168
220
|
|
169
221
|
# Send the current layout and controller in a custom response header
|
170
|
-
Ajax.set_header(response, :layout,
|
222
|
+
Ajax.set_header(response, :layout, options[:layout])
|
171
223
|
Ajax.set_header(response, :controller, self.class.controller_name)
|
224
|
+
|
225
|
+
_render_template(options)
|
172
226
|
end
|
173
|
-
render_without_ajax(options, extra_options, &block)
|
174
227
|
end
|
175
228
|
|
176
|
-
# Return the layout to use for an AJAX request, or the default
|
177
|
-
# cannot be found. If no default is known, <tt>layouts/ajax/application</tt> is used.
|
229
|
+
# Return the layout to use for an AJAX request, or nil if the default should be used.
|
178
230
|
#
|
179
231
|
# If no ajax_layout is set, look for the default layout in <tt>layouts/ajax</tt>.
|
180
232
|
# If the layout cannot be found, use the default.
|
181
|
-
|
182
|
-
# FIXME: Use hard-coded html layout extension because <tt>default_template_format</tt>
|
183
|
-
# is sometimes :js which means the layout isn't found.
|
184
|
-
def layout_for_ajax(default) #:nodoc:
|
233
|
+
def _layout_for_ajax(default) #:nodoc:
|
185
234
|
ajax_layout = self.class.read_inheritable_attribute(:ajax_layout)
|
186
|
-
if ajax_layout.nil?
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
235
|
+
ajax_layout = if ajax_layout.nil? && default.nil?
|
236
|
+
nil
|
237
|
+
elsif ajax_layout.nil? && !default.nil? # look for one with the default name in layouts/ajax
|
238
|
+
"layouts/ajax/#{default.sub(/layouts(\/)?/, '')}"
|
239
|
+
elsif ajax_layout && !(ajax_layout =~ /^layouts\/ajax/) # look for it in layouts/ajax
|
240
|
+
"layouts/ajax/#{ajax_layout}"
|
241
|
+
else # look as is
|
242
|
+
ajax_layout
|
243
|
+
end
|
244
|
+
Ajax.app.rails?(3) ? find_template(ajax_layout) : find_layout(ajax_layout, 'html') if !ajax_layout.nil?
|
191
245
|
rescue ::ActionView::MissingTemplate
|
192
|
-
Ajax.logger.info("[ajax] no layout found in layouts/ajax
|
193
|
-
|
246
|
+
Ajax.logger.info("[ajax] no layout found in layouts/ajax. Using #{default}.")
|
247
|
+
nil
|
194
248
|
end
|
249
|
+
end
|
195
250
|
end
|
196
|
-
end
|
251
|
+
end
|