rails3-jquery-autocomplete 0.6.1 → 0.6.2

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,163 +1,137 @@
1
- /*
2
- * jquery-ujs
3
- *
4
- * http://github.com/rails/jquery-ujs/blob/master/src/rails.js
5
- *
6
- * This rails.js file supports jQuery 1.4.3 and 1.4.4 .
7
- *
8
- */
9
-
10
- jQuery(function ($) {
11
- var csrf_token = $('meta[name=csrf-token]').attr('content'),
12
- csrf_param = $('meta[name=csrf-param]').attr('content');
13
-
14
- $.fn.extend({
15
- /**
16
- * Triggers a custom event on an element and returns the event result
17
- * this is used to get around not being able to ensure callbacks are placed
18
- * at the end of the chain.
19
- *
20
- * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our
21
- * own events and placing ourselves at the end of the chain.
22
- */
23
- triggerAndReturn: function (name, data) {
24
- var event = new $.Event(name);
25
- this.trigger(event, data);
26
-
27
- return event.result !== false;
28
- },
29
-
30
- /**
31
- * Handles execution of remote calls. Provides following callbacks:
32
- *
33
- * - ajax:before - is execute before the whole thing begings
34
- * - ajax:loading - is executed before firing ajax call
35
- * - ajax:success - is executed when status is success
36
- * - ajax:complete - is execute when status is complete
37
- * - ajax:failure - is execute in case of error
38
- * - ajax:after - is execute every single time at the end of ajax call
39
- */
40
- callRemote: function () {
41
- var el = this,
42
- method = el.attr('method') || el.attr('data-method') || 'GET',
43
- url = el.attr('action') || el.attr('href'),
44
- dataType = el.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType);
45
-
46
- if (url === undefined) {
47
- throw "No URL specified for remote call (action or href must be present).";
48
- } else {
49
- if (el.triggerAndReturn('ajax:before')) {
50
- var data = el.is('form') ? el.serializeArray() : [];
51
- $.ajax({
52
- url: url,
53
- data: data,
54
- dataType: dataType,
55
- type: method.toUpperCase(),
56
- beforeSend: function (xhr) {
57
- xhr.setRequestHeader("Accept", "text/javascript");
58
- el.trigger('ajax:loading', xhr);
59
- },
60
- success: function (data, status, xhr) {
61
- el.trigger('ajax:success', [data, status, xhr]);
62
- },
63
- complete: function (xhr) {
64
- el.trigger('ajax:complete', xhr);
65
- },
66
- error: function (xhr, status, error) {
67
- el.trigger('ajax:failure', [xhr, status, error]);
68
- }
69
- });
70
- }
71
-
72
- el.trigger('ajax:after');
73
- }
74
- }
75
- });
76
-
77
- /**
78
- * confirmation handler
79
- */
80
-
81
- $('body').delegate('a[data-confirm], button[data-confirm], input[data-confirm]', 'click.rails', function () {
82
- var el = $(this);
83
- if (el.triggerAndReturn('confirm')) {
84
- if (!confirm(el.attr('data-confirm'))) {
85
- return false;
86
- }
87
- }
88
- });
89
-
90
-
91
-
92
- /**
93
- * remote handlers
94
- */
95
- $('form[data-remote]').live('submit.rails', function (e) {
96
- $(this).callRemote();
97
- e.preventDefault();
98
- });
99
-
100
- $('a[data-remote],input[data-remote]').live('click.rails', function (e) {
101
- $(this).callRemote();
102
- e.preventDefault();
103
- });
104
-
105
- /**
106
- * <%= link_to "Delete", user_path(@user), :method => :delete, :confirm => "Are you sure?" %>
107
- *
108
- * <a href="/users/5" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>
109
- */
110
- $('a[data-method]:not([data-remote])').live('click.rails', function (e){
111
- var link = $(this),
112
- href = link.attr('href'),
113
- method = link.attr('data-method'),
114
- form = $('<form method="post" action="'+href+'"></form>'),
115
- metadata_input = '<input name="_method" value="'+method+'" type="hidden" />';
116
-
117
- if (csrf_param !== undefined && csrf_token !== undefined) {
118
- metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />';
119
- }
120
-
121
- form.hide()
122
- .append(metadata_input)
123
- .appendTo('body');
124
-
125
- e.preventDefault();
126
- form.submit();
127
- });
128
-
129
- /**
130
- * disable-with handlers
131
- */
132
- var disable_with_input_selector = 'input[data-disable-with]',
133
- disable_with_form_remote_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')',
134
- disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')';
135
-
136
- var disable_with_input_function = function () {
137
- $(this).find(disable_with_input_selector).each(function () {
138
- var input = $(this);
139
- input.data('enable-with', input.val())
140
- .attr('value', input.attr('data-disable-with'))
141
- .attr('disabled', 'disabled');
142
- });
143
- };
144
-
145
- $(disable_with_form_remote_selector).live('ajax:before.rails', disable_with_input_function);
146
- $(disable_with_form_not_remote_selector).live('submit.rails', disable_with_input_function);
147
-
148
- $(disable_with_form_remote_selector).live('ajax:complete.rails', function () {
149
- $(this).find(disable_with_input_selector).each(function () {
150
- var input = $(this);
151
- input.removeAttr('disabled')
152
- .val(input.data('enable-with'));
153
- });
154
- });
155
-
156
- var jqueryVersion = $().jquery;
157
-
158
- if (!( (jqueryVersion === '1.4.3') || (jqueryVersion === '1.4.4'))){
159
- alert('This rails.js does not support the jQuery version you are using. Please read documentation.');
160
- }
161
-
162
-
163
- });
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
+
8
+ (function($) {
9
+ // Triggers an event on an element and returns the event result
10
+ function fire(obj, name, data) {
11
+ var event = new $.Event(name);
12
+ obj.trigger(event, data);
13
+ return event.result !== false;
14
+ }
15
+
16
+ // Submits "remote" forms and links with ajax
17
+ function handleRemote(element) {
18
+ var method, url, data,
19
+ dataType = element.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType);
20
+
21
+ if (element.is('form')) {
22
+ method = element.attr('method');
23
+ url = element.attr('action');
24
+ data = element.serializeArray();
25
+ // memoized value from clicked submit button
26
+ var button = element.data('ujs:submit-button');
27
+ if (button) {
28
+ data.push(button);
29
+ element.data('ujs:submit-button', null);
30
+ }
31
+ } else {
32
+ method = element.attr('data-method');
33
+ url = element.attr('href');
34
+ data = null;
35
+ }
36
+
37
+ $.ajax({
38
+ url: url, type: method || 'GET', data: data, dataType: dataType,
39
+ // stopping the "ajax:beforeSend" event will cancel the ajax request
40
+ beforeSend: function(xhr, settings) {
41
+ if (settings.dataType === undefined) {
42
+ xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
43
+ }
44
+ return fire(element, 'ajax:beforeSend', [xhr, settings]);
45
+ },
46
+ success: function(data, status, xhr) {
47
+ element.trigger('ajax:success', [data, status, xhr]);
48
+ },
49
+ complete: function(xhr, status) {
50
+ element.trigger('ajax:complete', [xhr, status]);
51
+ },
52
+ error: function(xhr, status, error) {
53
+ element.trigger('ajax:error', [xhr, status, error]);
54
+ }
55
+ });
56
+ }
57
+
58
+ // Handles "data-method" on links such as:
59
+ // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
60
+ function handleMethod(link) {
61
+ var href = link.attr('href'),
62
+ method = link.attr('data-method'),
63
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
64
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
65
+ form = $('<form method="post" action="' + href + '"></form>'),
66
+ metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
67
+
68
+ if (csrf_param !== undefined && csrf_token !== undefined) {
69
+ metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
70
+ }
71
+
72
+ form.hide().append(metadata_input).appendTo('body');
73
+ form.submit();
74
+ }
75
+
76
+ function disableFormElements(form) {
77
+ form.find('input[data-disable-with]').each(function() {
78
+ var input = $(this);
79
+ input.data('ujs:enable-with', input.val())
80
+ .val(input.attr('data-disable-with'))
81
+ .attr('disabled', 'disabled');
82
+ });
83
+ }
84
+
85
+ function enableFormElements(form) {
86
+ form.find('input[data-disable-with]').each(function() {
87
+ var input = $(this);
88
+ input.val(input.data('ujs:enable-with')).removeAttr('disabled');
89
+ });
90
+ }
91
+
92
+ function allowAction(element) {
93
+ var message = element.attr('data-confirm');
94
+ return !message || (fire(element, 'confirm') && confirm(message));
95
+ }
96
+
97
+ $('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
98
+ var link = $(this);
99
+ if (!allowAction(link)) return false;
100
+
101
+ if (link.attr('data-remote')) {
102
+ handleRemote(link);
103
+ return false;
104
+ } else if (link.attr('data-method')) {
105
+ handleMethod(link);
106
+ return false;
107
+ }
108
+ });
109
+
110
+ $('form').live('submit.rails', function(e) {
111
+ var form = $(this);
112
+ if (!allowAction(form)) return false;
113
+
114
+ if (form.attr('data-remote')) {
115
+ handleRemote(form);
116
+ return false;
117
+ } else {
118
+ disableFormElements(form);
119
+ }
120
+ });
121
+
122
+ $('form input[type=submit], form button[type=submit], form button:not([type])').live('click.rails', function() {
123
+ var button = $(this);
124
+ if (!allowAction(button)) return false;
125
+ // register the pressed submit button
126
+ var name = button.attr('name'), data = name ? {name:name, value:button.val()} : null;
127
+ button.closest('form').data('ujs:submit-button', data);
128
+ });
129
+
130
+ $('form').live('ajax:beforeSend.rails', function(event) {
131
+ if (this == event.target) disableFormElements($(this));
132
+ });
133
+
134
+ $('form').live('ajax:complete.rails', function(event) {
135
+ if (this == event.target) enableFormElements($(this));
136
+ });
137
+ })( jQuery );
@@ -30,7 +30,7 @@ module Rails3JQueryAutocomplete
30
30
  if term && !term.empty?
31
31
  #allow specifying fully qualified class name for model object
32
32
  class_name = options[:class_name] || object
33
- items = get_items(:model => get_object(class_name), \
33
+ items = get_autocomplete_items(:model => get_object(class_name), \
34
34
  :options => options, :term => term, :method => method)
35
35
  else
36
36
  items = {}
@@ -51,6 +51,6 @@ end
51
51
 
52
52
  class ActionView::Helpers::FormBuilder #:nodoc:
53
53
  def autocomplete_field(method, source, options = {})
54
- @template.autocomplete_field(@object_name, method, source, options)
54
+ @template.autocomplete_field(@object_name, method, source, objectify_options(options))
55
55
  end
56
56
  end
@@ -3,7 +3,10 @@ module Rails3JQueryAutocomplete
3
3
  # Contains utility methods used by autocomplete
4
4
  module Helpers
5
5
 
6
- # Returns a three keys hash
6
+ #
7
+ # Returns a three keys hash actually used by the Autocomplete jQuery-ui
8
+ # Can be overriden to show whatever you like
9
+ #
7
10
  def json_for_autocomplete(items, method)
8
11
  items.collect {|item| {"id" => item.id, "label" => item.send(method), "value" => item.send(method)}}
9
12
  end
@@ -12,6 +15,7 @@ module Rails3JQueryAutocomplete
12
15
  #
13
16
  # get_object(:actor)
14
17
  # # returns a Actor constant supposing it is already defined
18
+ #
15
19
  def get_object(model_sym)
16
20
  object = model_sym.to_s.camelize.constantize
17
21
  end
@@ -29,14 +33,20 @@ module Rails3JQueryAutocomplete
29
33
  end
30
34
  end
31
35
 
32
- # Returns the order parameter to be used in the query created by get_items
36
+ #DEPRECATED
33
37
  def get_order(implementation, method, options)
38
+ warn 'Rails3JQueryAutocomplete#get_order is has been DEPRECATED, please use #get_autocomplete_order instead'
39
+ get_autocomplete_order(implementation, method, options)
40
+ end
41
+
42
+ # Returns the order parameter to be used in the query created by get_items
43
+ def get_autocomplete_order(implementation, method, options)
34
44
  order = options[:order]
35
45
 
36
46
  case implementation
37
47
  when :mongoid then
38
48
  if order
39
- order.split(',').collect do |fields|
49
+ order.split(',').collect do |fields|
40
50
  sfields = fields.split
41
51
  [sfields[0].downcase.to_sym, sfields[1].downcase.to_sym]
42
52
  end
@@ -48,25 +58,39 @@ module Rails3JQueryAutocomplete
48
58
  end
49
59
  end
50
60
 
51
- # Returns a limit that will be used on the query
61
+ # DEPRECATED
52
62
  def get_limit(options)
63
+ warn 'Rails3JQueryAutocomplete#get_limit is has been DEPRECATED, please use #get_autocomplete_limit instead'
64
+ get_autocomplete_limit(options)
65
+ end
66
+
67
+ # Returns a limit that will be used on the query
68
+ def get_autocomplete_limit(options)
53
69
  options[:limit] ||= 10
54
70
  end
55
-
56
- # Queries selected database
57
- #
58
- # items = get_items(:model => get_object(object), :options => options, :term => term, :method => method)
71
+
72
+ # DEPRECATED
59
73
  def get_items(parameters)
74
+ warn 'Rails3JQueryAutocomplete#get_items is has been DEPRECATED, you should use #get_autocomplete_items instead'
75
+ get_autocomplete_items(parameters)
76
+ end
60
77
 
78
+ #
79
+ # Can be overriden to return or filter however you like
80
+ # the objects to be shown by autocomplete
81
+ #
82
+ # items = get_autocomplete_items(:model => get_object(object), :options => options, :term => term, :method => method)
83
+ #
84
+ def get_autocomplete_items(parameters)
61
85
  model = parameters[:model]
62
86
  method = parameters[:method]
63
87
  options = parameters[:options]
64
88
  term = parameters[:term]
65
89
  is_full_search = options[:full]
66
90
 
67
- limit = get_limit(options)
91
+ limit = get_autocomplete_limit(options)
68
92
  implementation = get_implementation(model)
69
- order = get_order(implementation, method, options)
93
+ order = get_autocomplete_order(implementation, method, options)
70
94
 
71
95
  case implementation
72
96
  when :mongoid
@@ -1,3 +1,3 @@
1
1
  module Rails3JQueryAutocomplete
2
- VERSION = '0.6.1'
2
+ VERSION = '0.6.2'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails3-jquery-autocomplete
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
5
- prerelease: false
4
+ hash: 3
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 1
10
- version: 0.6.1
9
+ - 2
10
+ version: 0.6.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Padilla
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-21 00:00:00 -07:00
18
+ date: 2011-02-12 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -135,6 +135,7 @@ files:
135
135
  - integration/app/controllers/autocomplete_controller.rb
136
136
  - integration/app/controllers/id_elements_controller.rb
137
137
  - integration/app/controllers/multiple_selections_controller.rb
138
+ - integration/app/controllers/nested_models_controller.rb
138
139
  - integration/app/controllers/sub_classes_controller.rb
139
140
  - integration/app/helpers/application_helper.rb
140
141
  - integration/app/helpers/autocomplete_helper.rb
@@ -143,12 +144,14 @@ files:
143
144
  - integration/app/helpers/layout_helper.rb
144
145
  - integration/app/helpers/sub_classes_helper.rb
145
146
  - integration/app/models/brand.rb
147
+ - integration/app/models/feature.rb
146
148
  - integration/app/models/foreign_brand.rb
147
149
  - integration/app/models/product.rb
148
150
  - integration/app/views/autocomplete/new.html.haml
149
151
  - integration/app/views/id_elements/new.html.haml
150
152
  - integration/app/views/layouts/application.html.haml
151
153
  - integration/app/views/multiple_selections/new.html.haml
154
+ - integration/app/views/nested_models/new.html.haml
152
155
  - integration/app/views/sub_classes/new.html.haml
153
156
  - integration/config.ru
154
157
  - integration/config/application.rb
@@ -169,6 +172,7 @@ files:
169
172
  - integration/db/migrate/20101209014338_create_brands.rb
170
173
  - integration/db/migrate/20101209014355_create_products.rb
171
174
  - integration/db/migrate/20101209053936_add_type_to_brand.rb
175
+ - integration/db/migrate/20110209020136_create_features.rb
172
176
  - integration/db/schema.rb
173
177
  - integration/db/seeds.rb
174
178
  - integration/doc/README_FOR_APP
@@ -187,7 +191,6 @@ files:
187
191
  - integration/public/images/rails.png
188
192
  - integration/public/javascripts/application.js
189
193
  - integration/public/javascripts/autocomplete-rails.js
190
- - integration/public/javascripts/jquery-ui.js
191
194
  - integration/public/javascripts/jquery-ui.min.js
192
195
  - integration/public/javascripts/jquery.js
193
196
  - integration/public/javascripts/jquery.min.js
@@ -246,7 +249,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
246
249
  requirements: []
247
250
 
248
251
  rubyforge_project:
249
- rubygems_version: 1.3.7
252
+ rubygems_version: 1.4.1
250
253
  signing_key:
251
254
  specification_version: 3
252
255
  summary: Use jQuery's autocomplete plugin with Rails 3.