ajax 1.1.0 → 1.1.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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ./
3
3
  specs:
4
- ajax (1.1.0)
4
+ ajax (1.1.1)
5
5
  json
6
6
  rack
7
7
 
@@ -10,6 +10,7 @@ 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.1: Backwards compatibility fix for Rails < 3
13
14
  * v1.1.0: Rails 3 supported!
14
15
 
15
16
  == Install
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.1.1
@@ -5,7 +5,7 @@ module Ajax
5
5
  extend MirrorMethods
6
6
  include MirrorMethods
7
7
  after_filter :serialize_ajax_info
8
- include InstanceMethods
8
+ include(Ajax.app.rails?(3) ? Rails3 : Rails2)
9
9
  end
10
10
  end
11
11
 
@@ -43,15 +43,11 @@ module Ajax
43
43
  end
44
44
  end
45
45
 
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
46
+ module Rails2
47
+ def self.included(klass)
48
+ klass.class_eval do
49
+ alias_method_chain :redirect_to_full_url, :ajax
50
+ alias_method_chain :render, :ajax
55
51
  end
56
52
  end
57
53
 
@@ -70,182 +66,197 @@ module Ajax
70
66
  # redirect.
71
67
  #
72
68
  # 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
69
+ def redirect_to_full_url_with_ajax(url, status)
70
+ if !_ajax_redirect(url, status)
71
+ redirect_to_full_url_without_ajax(url, status)
72
+ end
75
73
  end
76
74
 
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?
82
-
83
- special_redirect = false
84
- original_url = url
75
+ #
76
+ # Intercept rendering to customize the headers and layout handling
77
+ #
78
+ def render_with_ajax(options = nil, extra_options = {}, &block)
79
+ return render_without_ajax(options, extra_options, &block) unless Ajax.is_enabled?
85
80
 
86
- # If we have the full referrer in Ajax-Info, use that because it
87
- # includes the fragment.
88
- if url == request.headers["Referer"] && !request.headers['Ajax-Info'].blank?
89
- url = request.headers['Ajax-Info']['referer']
90
- Ajax.logger.debug("[ajax] using referer #{url} from Ajax-Info")
91
- end
81
+ original_args = [options, extra_options]
82
+ if request.xhr?
92
83
 
93
- if !Ajax.exclude_path?(url)
94
- # Never redirect to the Ajax framework path, redirect to /
95
- if url =~ %r[#{Ajax.framework_path}]
96
- url = url.sub(%r[#{Ajax.framework_path}], '/')
97
-
98
- # Special case:
99
- #
100
- # Changing protocol forces a redirect from root to root.
101
- # The full request URL (including the hashed part) is
102
- # in the browser. So return JS to do the redirect and
103
- # have it include the hashed part in the redirect URL.
104
- if !request.xhr? && URI.parse(url).scheme != URI.parse(request.url).scheme
105
- special_redirect = true
84
+ # Options processing taken from ActionController::Base#render
85
+ if options.nil?
86
+ options = { :template => default_template, :layout => true }
87
+ elsif options == :update
88
+ options = extra_options.merge({ :update => true })
89
+ elsif options.is_a?(String) || options.is_a?(Symbol)
90
+ case options.to_s.index('/')
91
+ when 0
92
+ extra_options[:file] = options
93
+ when nil
94
+ extra_options[:action] = options
95
+ else
96
+ extra_options[:template] = options
106
97
  end
98
+ options = extra_options
99
+ elsif !options.is_a?(Hash)
100
+ extra_options[:partial] = options
101
+ options = extra_options
107
102
  end
108
103
 
109
- if !Ajax.is_hashed_url?(url) and !Ajax.is_robot?(request.user_agent)
110
- url = Ajax.hashed_url_from_traditional(url)
111
- end
112
- end
113
- Ajax.logger.info("[ajax] rewrote redirect from #{original_url} to #{url}") unless original_url == url
114
-
115
- # Don't store session[:redirected_to] if doing a special redirect otherwise
116
- # when the next request for root comes in it will think we really want
117
- # to display the home page.
118
- if special_redirect
119
- session[:redirected_to] = nil
120
- Ajax.logger.info("[ajax] returning special redirect JS")
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
141
- else
142
- session[:redirected_to] = url
143
- if request.xhr? && Ajax.is_hashed_url?(url)
144
- Ajax.logger.info("[ajax] detecting we are xhr. soft redirect")
145
- redirect_path = URI.parse(url).select(:fragment).first
146
- Ajax.logger.info("[ajax] redirect path is #{redirect_path}")
147
- Ajax.set_header(response, :soft_redirect, redirect_path)
148
- self.response_body = <<END
149
- <script type="text/javascript">
150
- window.location.href = #{url.to_json};
151
- </script>
152
- END
153
- else
154
- Ajax.logger.info("[ajax] not detecting we are xhr. Hard redirect!")
155
- return false
156
- end
104
+ default = pick_layout(options)
105
+ default = default.path_without_format_and_extension unless default.nil?
106
+ ajax_layout = _layout_for_ajax(default)
107
+ ajax_layout = ajax_layout.path_without_format_and_extension unless ajax_layout.nil?
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)
157
113
  end
158
- true
114
+ render_without_ajax(options, extra_options, &block)
159
115
  end
160
116
 
161
- # Convert the Ajax-Info hash to JSON before the request is sent.
162
- # Invoked as an after filter.
163
- def serialize_ajax_info
164
- case response.headers['Ajax-Info']
165
- when Hash
166
- response.headers['Ajax-Info'] = response.headers['Ajax-Info'].to_json
117
+ end
118
+
119
+ module Rails3
120
+ # Rails 3 hook. Rails < 3 is handled using redirect_to_full_url. See
121
+ # those docs for info.
122
+ def redirect_to(url={}, status={})
123
+ if !_ajax_redirect(url, status)
124
+ super
167
125
  end
168
126
  end
169
127
 
170
128
  #
171
129
  # Intercept rendering to customize the headers and layout handling
172
130
  #
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
198
- end
131
+ def render_to_body(options = {})
132
+ return super if !request.xhr? || !Ajax.is_enabled?
133
+ _process_options(options)
134
+
135
+ if ajax_layout = _layout_for_ajax(options[:layout])
136
+ options[:layout] = ajax_layout.virtual_path
137
+ end
199
138
 
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?
139
+ # Send the current layout and controller in a custom response header
140
+ Ajax.set_header(response, :layout, options[:layout])
141
+ Ajax.set_header(response, :controller, self.class.controller_name)
142
+
143
+ _render_template(options)
144
+ end
145
+ end
205
146
 
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)
147
+ protected
148
+
149
+ # Convert the Ajax-Info hash to JSON before the request is sent.
150
+ # Invoked as an after filter.
151
+ def serialize_ajax_info
152
+ case response.headers['Ajax-Info']
153
+ when Hash
154
+ response.headers['Ajax-Info'] = response.headers['Ajax-Info'].to_json
155
+ end
156
+ end
157
+
158
+ # Perform special processing on the response if we need to.
159
+ # Return true if an Ajax "redirect" was performed, and false
160
+ # otherwise.
161
+ def _ajax_redirect(url, status)
162
+ return false unless Ajax.is_enabled?
163
+ special_redirect = false
164
+ original_url = url
165
+
166
+ # If we have the full referrer in Ajax-Info, use that because it
167
+ # includes the fragment.
168
+ if url == request.headers["Referer"] && !request.headers['Ajax-Info'].blank?
169
+ url = request.headers['Ajax-Info']['referer']
170
+ Ajax.logger.debug("[ajax] using referer #{url} from Ajax-Info")
171
+ end
172
+
173
+ if !Ajax.exclude_path?(url)
174
+ # Never redirect to the Ajax framework path, redirect to /
175
+ if url =~ %r[#{Ajax.framework_path}]
176
+ url = url.sub(%r[#{Ajax.framework_path}], '/')
177
+
178
+ # Special case:
179
+ #
180
+ # Changing protocol forces a redirect from root to root.
181
+ # The full request URL (including the hashed part) is
182
+ # in the browser. So return JS to do the redirect and
183
+ # have it include the hashed part in the redirect URL.
184
+ if !request.xhr? && URI.parse(url).scheme != URI.parse(request.url).scheme
185
+ special_redirect = true
209
186
  end
210
- super(options, extra_options, &block)
211
187
  end
212
- else
213
- def render_to_body(options = {})
214
- return super if !request.xhr? || !Ajax.is_enabled?
215
- _process_options(options)
216
188
 
217
- if ajax_layout = _layout_for_ajax(options[:layout])
218
- options[:layout] = ajax_layout.virtual_path
219
- end
189
+ if !Ajax.is_hashed_url?(url) and !Ajax.is_robot?(request.user_agent)
190
+ url = Ajax.hashed_url_from_traditional(url)
191
+ end
192
+ end
193
+ Ajax.logger.info("[ajax] rewrote redirect from #{original_url} to #{url}") unless original_url == url
220
194
 
221
- # Send the current layout and controller in a custom response header
222
- Ajax.set_header(response, :layout, options[:layout])
223
- Ajax.set_header(response, :controller, self.class.controller_name)
195
+ # Don't store session[:redirected_to] if doing a special redirect otherwise
196
+ # when the next request for root comes in it will think we really want
197
+ # to display the home page.
198
+ if special_redirect
199
+ session[:redirected_to] = nil
200
+ Ajax.logger.info("[ajax] returning special redirect JS")
201
+ render :layout => false, :text => <<-END
202
+ <script type="text/javascript">
203
+ var url = #{url.to_json};
204
+ var hash = document.location.hash;
224
205
 
225
- _render_template(options)
206
+ // Remove leading # from the fragment
207
+ if (hash.charAt(0) == '#') {
208
+ hash = hash.substr(1);
209
+ }
210
+
211
+ // Remove leading / from the fragment if the URL already ends in a /
212
+ // This prevents double-slashes. Note we can't just replace all
213
+ // double-slashes because the protocol includes //.
214
+ if (url.charAt(url.length - 1) == '/' && hash.charAt(0) == '/') {
215
+ hash = hash.substr(1);
216
+ }
217
+
218
+ document.location.href = url + hash;
219
+ </script>
220
+ END
221
+ else
222
+ session[:redirected_to] = url
223
+ if request.xhr? && Ajax.is_hashed_url?(url)
224
+ Ajax.logger.info("[ajax] detecting we are xhr. soft redirect")
225
+ redirect_path = URI.parse(url).select(:fragment).first
226
+ Ajax.logger.info("[ajax] redirect path is #{redirect_path}")
227
+ Ajax.set_header(response, :soft_redirect, redirect_path)
228
+ render :layout => false, :text => <<-END
229
+ <script type="text/javascript">
230
+ window.location.href = #{url.to_json};
231
+ </script>
232
+ END
233
+ else
234
+ Ajax.logger.info("[ajax] not detecting we are xhr. Hard redirect!")
235
+ return false
226
236
  end
227
237
  end
238
+ true
239
+ end
228
240
 
229
- # Return the layout to use for an AJAX request, or nil if the default should be used.
230
- #
231
- # If no ajax_layout is set, look for the default layout in <tt>layouts/ajax</tt>.
232
- # If the layout cannot be found, use the default.
233
- def _layout_for_ajax(default) #:nodoc:
234
- ajax_layout = self.class.read_inheritable_attribute(:ajax_layout)
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?
245
- rescue ::ActionView::MissingTemplate
246
- Ajax.logger.info("[ajax] no layout found in layouts/ajax. Using #{default}.")
247
- nil
248
- end
241
+ # Return the layout to use for an AJAX request, or nil if the default should be used.
242
+ #
243
+ # If no ajax_layout is set, look for the default layout in <tt>layouts/ajax</tt>.
244
+ # If the layout cannot be found, use the default.
245
+ def _layout_for_ajax(default) #:nodoc:
246
+ ajax_layout = self.class.read_inheritable_attribute(:ajax_layout)
247
+ ajax_layout = if ajax_layout.nil? && default.nil?
248
+ nil
249
+ elsif ajax_layout.nil? && !default.nil? # look for one with the default name in layouts/ajax
250
+ "layouts/ajax/#{default.sub(/layouts(\/)?/, '')}"
251
+ elsif ajax_layout && !(ajax_layout =~ /^layouts\/ajax/) # look for it in layouts/ajax
252
+ "layouts/ajax/#{ajax_layout}"
253
+ else # look as is
254
+ ajax_layout
255
+ end
256
+ Ajax.app.rails?(3) ? find_template(ajax_layout) : find_layout(ajax_layout, 'html') if !ajax_layout.nil?
257
+ rescue ::ActionView::MissingTemplate
258
+ Ajax.logger.info("[ajax] no layout found in layouts/ajax. Using #{default}.")
259
+ nil
249
260
  end
250
261
  end
251
262
  end
@@ -61,4 +61,4 @@ module Ajax
61
61
  link_to_without_ajax(*args, &block)
62
62
  end
63
63
  end
64
- end
64
+ end
@@ -448,7 +448,9 @@ var Ajax = function(options) {
448
448
  */
449
449
  self.abortCurrentRequest = function() {
450
450
  try {
451
- console.log('[ajax] aborting current request for url ' + self.current_request.url);
451
+ if (self.current_request.url) {
452
+ console.log('[ajax] aborting current request for url ' + self.current_request.url);
453
+ }
452
454
  self.current_request.abort();
453
455
  } catch(e) {
454
456
  console.log('[ajax] abort failed!', e);
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: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 0
10
- version: 1.1.0
9
+ - 1
10
+ version: 1.1.1
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-04-26 00:00:00 -07:00
18
+ date: 2011-04-27 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency