jquery-rails 0.2.7 → 1.0.rc

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of jquery-rails might be problematic. Click here for more details.

Files changed (39) hide show
  1. data/CHANGELOG.md +6 -0
  2. data/Gemfile +5 -1
  3. data/Gemfile.lock +92 -62
  4. data/README.md +21 -6
  5. data/jquery-rails.gemspec +2 -4
  6. data/lib/generators/jquery/install/install_generator.rb +7 -5
  7. data/lib/jquery-rails.rb +5 -14
  8. data/lib/jquery-rails/engine.rb +12 -0
  9. data/lib/jquery-rails/railtie.rb +21 -0
  10. data/lib/jquery-rails/version.rb +1 -1
  11. data/spec/lib/jquery-rails_spec.rb +1 -48
  12. data/vendor/assets/javascripts/jquery.js +8865 -0
  13. data/vendor/assets/javascripts/jquery_ujs.js +289 -0
  14. metadata +13 -74
  15. data/spec/lib/generators/jquery/install_generator_spec.rb +0 -50
  16. data/spec/support/custom_app/.gitignore +0 -4
  17. data/spec/support/custom_app/Gemfile +0 -31
  18. data/spec/support/custom_app/config.ru +0 -4
  19. data/spec/support/custom_app/config/application.rb +0 -42
  20. data/spec/support/custom_app/config/boot.rb +0 -13
  21. data/spec/support/custom_app/config/database.yml +0 -22
  22. data/spec/support/custom_app/config/environment.rb +0 -5
  23. data/spec/support/custom_app/config/environments/development.rb +0 -26
  24. data/spec/support/custom_app/config/environments/production.rb +0 -49
  25. data/spec/support/custom_app/config/environments/test.rb +0 -35
  26. data/spec/support/custom_app/config/routes.rb +0 -58
  27. data/spec/support/custom_app/script/rails +0 -6
  28. data/spec/support/default_app/.gitignore +0 -4
  29. data/spec/support/default_app/Gemfile +0 -31
  30. data/spec/support/default_app/config.ru +0 -4
  31. data/spec/support/default_app/config/application.rb +0 -42
  32. data/spec/support/default_app/config/boot.rb +0 -13
  33. data/spec/support/default_app/config/database.yml +0 -22
  34. data/spec/support/default_app/config/environment.rb +0 -5
  35. data/spec/support/default_app/config/environments/development.rb +0 -26
  36. data/spec/support/default_app/config/environments/production.rb +0 -49
  37. data/spec/support/default_app/config/environments/test.rb +0 -35
  38. data/spec/support/default_app/config/routes.rb +0 -58
  39. data/spec/support/default_app/script/rails +0 -6
@@ -0,0 +1,289 @@
1
+ /**
2
+ * Unobtrusive scripting adapter for jQuery
3
+ *
4
+ * Requires jQuery 1.4.3 or later.
5
+ * https://github.com/rails/jquery-ujs
6
+
7
+ * Uploading file using rails.js
8
+ * =============================
9
+ *
10
+ * By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
11
+ * in the remote form, this adapter aborts the AJAX submission and allows the form to submit through standard means.
12
+ *
13
+ * The `ajax:aborted:file` event allows you to bind your own handler to process the form submission however you wish.
14
+ *
15
+ * Ex:
16
+ * $('form').live('ajax:aborted:file', function(event, elements){
17
+ * // Implement own remote file-transfer handler here for non-blank file inputs passed in `elements`.
18
+ * // Returning false in this handler tells rails.js to disallow standard form submission
19
+ * return false;
20
+ * });
21
+ *
22
+ * The `ajax:aborted:file` event is fired when a file-type input is detected with a non-blank value.
23
+ *
24
+ * Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use
25
+ * techniques like the iframe method to upload the file instead.
26
+ *
27
+ * Required fields in rails.js
28
+ * ===========================
29
+ *
30
+ * If any blank required inputs (required="required") are detected in the remote form, the whole form submission
31
+ * is canceled. Note that this is unlike file inputs, which still allow standard (non-AJAX) form submission.
32
+ *
33
+ * The `ajax:aborted:required` event allows you to bind your own handler to inform the user of blank required inputs.
34
+ *
35
+ * !! Note that Opera does not fire the form's submit event if there are blank required inputs, so this event may never
36
+ * get fired in Opera. This event is what causes other browsers to exhibit the same submit-aborting behavior.
37
+ *
38
+ * Ex:
39
+ * $('form').live('ajax:aborted:required', function(event, elements){
40
+ * // Returning false in this handler tells rails.js to submit the form anyway.
41
+ * // The blank required inputs are passed to this function in `elements`.
42
+ * return ! confirm("Would you like to submit the form with missing info?");
43
+ * });
44
+ */
45
+
46
+ (function($) {
47
+ // Shorthand to make it a little easier to call public rails functions from within rails.js
48
+ var rails;
49
+
50
+ $.rails = rails = {
51
+ // Link elements bound by jquery-ujs
52
+ linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote]',
53
+
54
+ // Form elements bound by jquery-ujs
55
+ formSubmitSelector: 'form',
56
+
57
+ // Form input elements bound by jquery-ujs
58
+ formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])',
59
+
60
+ // Form input elements disabled during form submission
61
+ disableSelector: 'input[data-disable-with], button[data-disable-with], textarea[data-disable-with]',
62
+
63
+ // Form input elements re-enabled after form submission
64
+ enableSelector: 'input[data-disable-with]:disabled, button[data-disable-with]:disabled, textarea[data-disable-with]:disabled',
65
+
66
+ // Form required input elements
67
+ requiredInputSelector: 'input[name][required],textarea[name][required]',
68
+
69
+ // Form file input elements
70
+ fileInputSelector: 'input:file',
71
+
72
+ // Make sure that every Ajax request sends the CSRF token
73
+ CSRFProtection: function(xhr) {
74
+ var token = $('meta[name="csrf-token"]').attr('content');
75
+ if (token) xhr.setRequestHeader('X-CSRF-Token', token);
76
+ },
77
+
78
+ // Triggers an event on an element and returns false if the event result is false
79
+ fire: function(obj, name, data) {
80
+ var event = $.Event(name);
81
+ obj.trigger(event, data);
82
+ return event.result !== false;
83
+ },
84
+
85
+ // Submits "remote" forms and links with ajax
86
+ handleRemote: function(element) {
87
+ var method, url, data,
88
+ dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
89
+
90
+ if (rails.fire(element, 'ajax:before')) {
91
+
92
+ if (element.is('form')) {
93
+ method = element.attr('method');
94
+ url = element.attr('action');
95
+ data = element.serializeArray();
96
+ // memoized value from clicked submit button
97
+ var button = element.data('ujs:submit-button');
98
+ if (button) {
99
+ data.push(button);
100
+ element.data('ujs:submit-button', null);
101
+ }
102
+ } else {
103
+ method = element.data('method');
104
+ url = element.attr('href');
105
+ data = null;
106
+ }
107
+
108
+ $.ajax({
109
+ url: url, type: method || 'GET', data: data, dataType: dataType,
110
+ // stopping the "ajax:beforeSend" event will cancel the ajax request
111
+ beforeSend: function(xhr, settings) {
112
+ if (settings.dataType === undefined) {
113
+ xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
114
+ }
115
+ return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
116
+ },
117
+ success: function(data, status, xhr) {
118
+ element.trigger('ajax:success', [data, status, xhr]);
119
+ },
120
+ complete: function(xhr, status) {
121
+ element.trigger('ajax:complete', [xhr, status]);
122
+ },
123
+ error: function(xhr, status, error) {
124
+ element.trigger('ajax:error', [xhr, status, error]);
125
+ }
126
+ });
127
+ }
128
+ },
129
+
130
+ // Handles "data-method" on links such as:
131
+ // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
132
+ handleMethod: function(link) {
133
+ var href = link.attr('href'),
134
+ method = link.data('method'),
135
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
136
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
137
+ form = $('<form method="post" action="' + href + '"></form>'),
138
+ metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
139
+
140
+ if (csrf_param !== undefined && csrf_token !== undefined) {
141
+ metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
142
+ }
143
+
144
+ form.hide().append(metadata_input).appendTo('body');
145
+ form.submit();
146
+ },
147
+
148
+ /* Disables form elements:
149
+ - Caches element value in 'ujs:enable-with' data store
150
+ - Replaces element text with value of 'data-disable-with' attribute
151
+ - Adds disabled=disabled attribute
152
+ */
153
+ disableFormElements: function(form) {
154
+ form.find(rails.disableSelector).each(function() {
155
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
156
+ element.data('ujs:enable-with', element[method]());
157
+ element[method](element.data('disable-with'));
158
+ element.attr('disabled', 'disabled');
159
+ });
160
+ },
161
+
162
+ /* Re-enables disabled form elements:
163
+ - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
164
+ - Removes disabled attribute
165
+ */
166
+ enableFormElements: function(form) {
167
+ form.find(rails.enableSelector).each(function() {
168
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
169
+ if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
170
+ element.removeAttr('disabled');
171
+ });
172
+ },
173
+
174
+ // If message provided in 'data-confirm' attribute, fires `confirm` event and returns result of confirm dialog.
175
+ // Attaching a handler to the element's `confirm` event that returns false cancels the confirm dialog.
176
+ allowAction: function(element) {
177
+ var message = element.data('confirm');
178
+ return !message || (rails.fire(element, 'confirm') && confirm(message));
179
+ },
180
+
181
+ // Helper function which checks for blank inputs in a form that match the specified CSS selector
182
+ blankInputs: function(form, specifiedSelector, nonBlank) {
183
+ var inputs = $(), input,
184
+ selector = specifiedSelector || 'input,textarea';
185
+ form.find(selector).each(function() {
186
+ input = $(this);
187
+ // Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
188
+ if (nonBlank ? input.val() : !input.val()) {
189
+ inputs = inputs.add(input);
190
+ }
191
+ });
192
+ return inputs.length ? inputs : false;
193
+ },
194
+
195
+ // Helper function which checks for non-blank inputs in a form that match the specified CSS selector
196
+ nonBlankInputs: function(form, specifiedSelector) {
197
+ return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank
198
+ },
199
+
200
+ // Helper function, needed to provide consistent behavior in IE
201
+ stopEverything: function(e) {
202
+ e.stopImmediatePropagation();
203
+ return false;
204
+ },
205
+
206
+ // find all the submit events directly bound to the form and
207
+ // manually invoke them. If anyone returns false then stop the loop
208
+ callFormSubmitBindings: function(form) {
209
+ var events = form.data('events'), continuePropagation = true;
210
+ if (events !== undefined && events['submit'] !== undefined) {
211
+ $.each(events['submit'], function(i, obj){
212
+ if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
213
+ });
214
+ }
215
+ return continuePropagation;
216
+ }
217
+ };
218
+
219
+ // ajaxPrefilter is a jQuery 1.5 feature
220
+ if ('ajaxPrefilter' in $) {
221
+ $.ajaxPrefilter(function(options, originalOptions, xhr){ rails.CSRFProtection(xhr); });
222
+ } else {
223
+ $(document).ajaxSend(function(e, xhr){ rails.CSRFProtection(xhr); });
224
+ }
225
+
226
+ $(rails.linkClickSelector).live('click.rails', function(e) {
227
+ var link = $(this);
228
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
229
+
230
+ if (link.data('remote') !== undefined) {
231
+ rails.handleRemote(link);
232
+ return false;
233
+ } else if (link.data('method')) {
234
+ rails.handleMethod(link);
235
+ return false;
236
+ }
237
+ });
238
+
239
+ $(rails.formSubmitSelector).live('submit.rails', function(e) {
240
+ var form = $(this),
241
+ remote = form.data('remote') !== undefined,
242
+ blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
243
+ nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
244
+
245
+ if (!rails.allowAction(form)) return rails.stopEverything(e);
246
+
247
+ // skip other logic when required values are missing or file upload is present
248
+ if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
249
+ return !remote;
250
+ }
251
+
252
+ if (remote) {
253
+ if (nonBlankFileInputs) {
254
+ return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
255
+ }
256
+
257
+ // If browser does not support submit bubbling, then this live-binding will be called before direct
258
+ // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
259
+ if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) === false) return rails.stopEverything(e);
260
+
261
+ rails.handleRemote(form);
262
+ return false;
263
+ } else {
264
+ // slight timeout so that the submit button gets properly serialized
265
+ setTimeout(function(){ rails.disableFormElements(form); }, 13);
266
+ }
267
+ });
268
+
269
+ $(rails.formInputClickSelector).live('click.rails', function(event) {
270
+ var button = $(this);
271
+
272
+ if (!rails.allowAction(button)) return rails.stopEverything(event);
273
+
274
+ // register the pressed submit button
275
+ var name = button.attr('name'),
276
+ data = name ? {name:name, value:button.val()} : null;
277
+
278
+ button.closest('form').data('ujs:submit-button', data);
279
+ });
280
+
281
+ $(rails.formSubmitSelector).live('ajax:beforeSend.rails', function(event) {
282
+ if (this == event.target) rails.disableFormElements($(this));
283
+ });
284
+
285
+ $(rails.formSubmitSelector).live('ajax:complete.rails', function(event) {
286
+ if (this == event.target) rails.enableFormElements($(this));
287
+ });
288
+
289
+ })( jQuery );
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jquery-rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
4
+ hash: 7712066
5
+ prerelease: true
6
6
  segments:
7
+ - 1
7
8
  - 0
8
- - 2
9
- - 7
10
- version: 0.2.7
9
+ - rc
10
+ version: 1.0.rc
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Andr\xC3\xA9 Arko"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-05 00:00:00 -08:00
18
+ date: 2011-05-03 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -41,12 +41,11 @@ dependencies:
41
41
  requirements:
42
42
  - - ~>
43
43
  - !ruby/object:Gem::Version
44
- hash: 47
44
+ hash: 23
45
45
  segments:
46
46
  - 0
47
47
  - 14
48
- - 4
49
- version: 0.14.4
48
+ version: "0.14"
50
49
  type: :runtime
51
50
  version_requirements: *id002
52
51
  - !ruby/object:Gem::Dependency
@@ -65,38 +64,7 @@ dependencies:
65
64
  version: 1.0.0
66
65
  type: :development
67
66
  version_requirements: *id003
68
- - !ruby/object:Gem::Dependency
69
- name: rspec
70
- prerelease: false
71
- requirement: &id004 !ruby/object:Gem::Requirement
72
- none: false
73
- requirements:
74
- - - ~>
75
- - !ruby/object:Gem::Version
76
- hash: 9
77
- segments:
78
- - 1
79
- - 3
80
- version: "1.3"
81
- type: :development
82
- version_requirements: *id004
83
- - !ruby/object:Gem::Dependency
84
- name: webmock
85
- prerelease: false
86
- requirement: &id005 !ruby/object:Gem::Requirement
87
- none: false
88
- requirements:
89
- - - ~>
90
- - !ruby/object:Gem::Version
91
- hash: 7
92
- segments:
93
- - 1
94
- - 4
95
- - 0
96
- version: 1.4.0
97
- type: :development
98
- version_requirements: *id005
99
- description: This gem provides a Rails generator to install jQuery and the jQuery-ujs driver into your Rails 3 application, and then have them included automatically instead of Prototype.
67
+ description: This gem provides jQuery and the jQuery-ujs driver for your Rails 3 application.
100
68
  email:
101
69
  - andre@arko.net
102
70
  executables: []
@@ -117,42 +85,13 @@ files:
117
85
  - lib/generators/jquery/install/install_generator.rb
118
86
  - lib/jquery-rails.rb
119
87
  - lib/jquery-rails/assert_select_jquery.rb
88
+ - lib/jquery-rails/engine.rb
89
+ - lib/jquery-rails/railtie.rb
120
90
  - lib/jquery-rails/version.rb
121
- - spec/lib/generators/jquery/install_generator_spec.rb
122
91
  - spec/lib/jquery-rails_spec.rb
123
92
  - spec/spec_helper.rb
124
- - spec/support/custom_app/.gitignore
125
- - spec/support/custom_app/Gemfile
126
- - spec/support/custom_app/config.ru
127
- - spec/support/custom_app/config/application.rb
128
- - spec/support/custom_app/config/boot.rb
129
- - spec/support/custom_app/config/database.yml
130
- - spec/support/custom_app/config/environment.rb
131
- - spec/support/custom_app/config/environments/development.rb
132
- - spec/support/custom_app/config/environments/production.rb
133
- - spec/support/custom_app/config/environments/test.rb
134
- - spec/support/custom_app/config/routes.rb
135
- - spec/support/custom_app/script/rails
136
- - spec/support/default_app/.gitignore
137
- - spec/support/default_app/Gemfile
138
- - spec/support/default_app/config.ru
139
- - spec/support/default_app/config/application.rb
140
- - spec/support/default_app/config/boot.rb
141
- - spec/support/default_app/config/database.yml
142
- - spec/support/default_app/config/environment.rb
143
- - spec/support/default_app/config/environments/development.rb
144
- - spec/support/default_app/config/environments/production.rb
145
- - spec/support/default_app/config/environments/test.rb
146
- - spec/support/default_app/config/routes.rb
147
- - spec/support/default_app/public/javascripts/application.js
148
- - spec/support/default_app/public/javascripts/controls.js
149
- - spec/support/default_app/public/javascripts/dragdrop.js
150
- - spec/support/default_app/public/javascripts/effects.js
151
- - spec/support/default_app/public/javascripts/jquery.js
152
- - spec/support/default_app/public/javascripts/jquery.min.js
153
- - spec/support/default_app/public/javascripts/prototype.js
154
- - spec/support/default_app/public/javascripts/rails.js
155
- - spec/support/default_app/script/rails
93
+ - vendor/assets/javascripts/jquery.js
94
+ - vendor/assets/javascripts/jquery_ujs.js
156
95
  has_rdoc: true
157
96
  homepage: http://rubygems.org/gems/jquery-rails
158
97
  licenses: []
@@ -1,50 +0,0 @@
1
- require 'spec/test/unit'
2
- require 'spec_helper'
3
- require 'generators/jquery/install/install_generator'
4
-
5
- class Jquery::Generators::InstallGeneratorTest < Rails::Generators::TestCase
6
- describe "The jQuery generator"
7
-
8
- destination File.join(Rails.root)
9
- tests Jquery::Generators::InstallGenerator
10
- arguments []
11
-
12
- before(:each) do
13
- prepare_destination
14
- @response = {:body => "abc", :status => 200}
15
- stub_request(:get, /ajax.googleapis.com|github.com/).to_return(@response)
16
- end
17
-
18
- it "should remove prototype" do
19
- run_generator
20
- %w(controls.js dragdrop.js effects.js prototype.js).each { |js| assert_no_file "public/javascripts/#{js}" }
21
- end
22
-
23
- it "should install the rails ujs shim" do
24
- run_generator
25
- assert_file "public/javascripts/rails.js"
26
- end
27
-
28
- it "should install jquery" do
29
- run_generator
30
- %w(jquery.min.js jquery.js).each { |js| assert_file "public/javascripts/#{js}" }
31
- end
32
-
33
- it "should install old jquery versions" do
34
- run_generator %w(--version 1.4.2)
35
- %w(jquery.min.js jquery.js).each { |js| assert_file "public/javascripts/#{js}" }
36
- end
37
-
38
- it "should try to install unknown jquery versions with fallback" do
39
- stub_request(:get, /ajax.googleapis.com/).
40
- to_return(:status => 404, :body => "No").
41
- to_return(@response)
42
- run_generator %w(--version 100.0)
43
- %w(jquery.min.js jquery.js).each { |js| assert_file "public/javascripts/#{js}" }
44
- end
45
-
46
- it "should install jquery-ui when asked" do
47
- run_generator %w(--ui)
48
- %w(jquery-ui.min.js jquery-ui.js).each { |js| assert_file "public/javascripts/#{js}" }
49
- end
50
- end