autosuggest-rb 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/.gitignore +5 -0
  2. data/Gemfile +4 -0
  3. data/README.markdown +130 -0
  4. data/Rakefile +2 -0
  5. data/autosuggest-rb.gemspec +27 -0
  6. data/integration/.gitignore +4 -0
  7. data/integration/Gemfile +43 -0
  8. data/integration/README +256 -0
  9. data/integration/Rakefile +7 -0
  10. data/integration/app/controllers/application_controller.rb +3 -0
  11. data/integration/app/helpers/application_helper.rb +2 -0
  12. data/integration/app/views/layouts/application.html.erb +14 -0
  13. data/integration/config/application.rb +46 -0
  14. data/integration/config/boot.rb +6 -0
  15. data/integration/config/database.yml +22 -0
  16. data/integration/config/environment.rb +5 -0
  17. data/integration/config/environments/development.rb +26 -0
  18. data/integration/config/environments/production.rb +49 -0
  19. data/integration/config/environments/test.rb +35 -0
  20. data/integration/config/initializers/backtrace_silencers.rb +7 -0
  21. data/integration/config/initializers/inflections.rb +10 -0
  22. data/integration/config/initializers/mime_types.rb +5 -0
  23. data/integration/config/initializers/secret_token.rb +7 -0
  24. data/integration/config/initializers/session_store.rb +8 -0
  25. data/integration/config/locales/en.yml +5 -0
  26. data/integration/config/routes.rb +58 -0
  27. data/integration/config.ru +4 -0
  28. data/integration/db/seeds.rb +7 -0
  29. data/integration/doc/README_FOR_APP +2 -0
  30. data/integration/lib/tasks/.gitkeep +0 -0
  31. data/integration/public/404.html +26 -0
  32. data/integration/public/422.html +26 -0
  33. data/integration/public/500.html +26 -0
  34. data/integration/public/favicon.ico +0 -0
  35. data/integration/public/images/rails.png +0 -0
  36. data/integration/public/index.html +239 -0
  37. data/integration/public/javascripts/application.js +2 -0
  38. data/integration/public/javascripts/controls.js +965 -0
  39. data/integration/public/javascripts/dragdrop.js +974 -0
  40. data/integration/public/javascripts/effects.js +1123 -0
  41. data/integration/public/javascripts/prototype.js +6001 -0
  42. data/integration/public/javascripts/rails.js +191 -0
  43. data/integration/public/robots.txt +5 -0
  44. data/integration/public/stylesheets/.gitkeep +0 -0
  45. data/integration/script/rails +6 -0
  46. data/integration/test/performance/browsing_test.rb +9 -0
  47. data/integration/test/test_helper.rb +13 -0
  48. data/integration/vendor/plugins/.gitkeep +0 -0
  49. data/lib/autosuggest/controller_macros.rb +15 -0
  50. data/lib/autosuggest/form_helper.rb +35 -0
  51. data/lib/autosuggest/helpers.rb +12 -0
  52. data/lib/autosuggest/version.rb +3 -0
  53. data/lib/autosuggest-rb.rb +8 -0
  54. data/lib/generators/assets/autoSuggest.css +217 -0
  55. data/lib/generators/assets/jquery.autoSuggest.js +399 -0
  56. data/lib/generators/autosuggest_generator.rb +14 -0
  57. data/spec/lib/class_methods_spec.rb +9 -0
  58. data/spec/spec_helper.rb +17 -0
  59. metadata +169 -0
@@ -0,0 +1,191 @@
1
+ (function() {
2
+ // Technique from Juriy Zaytsev
3
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
4
+ function isEventSupported(eventName) {
5
+ var el = document.createElement('div');
6
+ eventName = 'on' + eventName;
7
+ var isSupported = (eventName in el);
8
+ if (!isSupported) {
9
+ el.setAttribute(eventName, 'return;');
10
+ isSupported = typeof el[eventName] == 'function';
11
+ }
12
+ el = null;
13
+ return isSupported;
14
+ }
15
+
16
+ function isForm(element) {
17
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
18
+ }
19
+
20
+ function isInput(element) {
21
+ if (Object.isElement(element)) {
22
+ var name = element.nodeName.toUpperCase()
23
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
24
+ }
25
+ else return false
26
+ }
27
+
28
+ var submitBubbles = isEventSupported('submit'),
29
+ changeBubbles = isEventSupported('change')
30
+
31
+ if (!submitBubbles || !changeBubbles) {
32
+ // augment the Event.Handler class to observe custom events when needed
33
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
34
+ function(init, element, eventName, selector, callback) {
35
+ init(element, eventName, selector, callback)
36
+ // is the handler being attached to an element that doesn't support this event?
37
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
38
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
39
+ // "submit" => "emulated:submit"
40
+ this.eventName = 'emulated:' + this.eventName
41
+ }
42
+ }
43
+ )
44
+ }
45
+
46
+ if (!submitBubbles) {
47
+ // discover forms on the page by observing focus events which always bubble
48
+ document.on('focusin', 'form', function(focusEvent, form) {
49
+ // special handler for the real "submit" event (one-time operation)
50
+ if (!form.retrieve('emulated:submit')) {
51
+ form.on('submit', function(submitEvent) {
52
+ var emulated = form.fire('emulated:submit', submitEvent, true)
53
+ // if custom event received preventDefault, cancel the real one too
54
+ if (emulated.returnValue === false) submitEvent.preventDefault()
55
+ })
56
+ form.store('emulated:submit', true)
57
+ }
58
+ })
59
+ }
60
+
61
+ if (!changeBubbles) {
62
+ // discover form inputs on the page
63
+ document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
64
+ // special handler for real "change" events
65
+ if (!input.retrieve('emulated:change')) {
66
+ input.on('change', function(changeEvent) {
67
+ input.fire('emulated:change', changeEvent, true)
68
+ })
69
+ input.store('emulated:change', true)
70
+ }
71
+ })
72
+ }
73
+
74
+ function handleRemote(element) {
75
+ var method, url, params;
76
+
77
+ var event = element.fire("ajax:before");
78
+ if (event.stopped) return false;
79
+
80
+ if (element.tagName.toLowerCase() === 'form') {
81
+ method = element.readAttribute('method') || 'post';
82
+ url = element.readAttribute('action');
83
+ params = element.serialize();
84
+ } else {
85
+ method = element.readAttribute('data-method') || 'get';
86
+ url = element.readAttribute('href');
87
+ params = {};
88
+ }
89
+
90
+ new Ajax.Request(url, {
91
+ method: method,
92
+ parameters: params,
93
+ evalScripts: true,
94
+
95
+ onComplete: function(request) { element.fire("ajax:complete", request); },
96
+ onSuccess: function(request) { element.fire("ajax:success", request); },
97
+ onFailure: function(request) { element.fire("ajax:failure", request); }
98
+ });
99
+
100
+ element.fire("ajax:after");
101
+ }
102
+
103
+ function handleMethod(element) {
104
+ var method = element.readAttribute('data-method'),
105
+ url = element.readAttribute('href'),
106
+ csrf_param = $$('meta[name=csrf-param]')[0],
107
+ csrf_token = $$('meta[name=csrf-token]')[0];
108
+
109
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
110
+ element.parentNode.insert(form);
111
+
112
+ if (method !== 'post') {
113
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
114
+ form.insert(field);
115
+ }
116
+
117
+ if (csrf_param) {
118
+ var param = csrf_param.readAttribute('content'),
119
+ token = csrf_token.readAttribute('content'),
120
+ field = new Element('input', { type: 'hidden', name: param, value: token });
121
+ form.insert(field);
122
+ }
123
+
124
+ form.submit();
125
+ }
126
+
127
+
128
+ document.on("click", "*[data-confirm]", function(event, element) {
129
+ var message = element.readAttribute('data-confirm');
130
+ if (!confirm(message)) event.stop();
131
+ });
132
+
133
+ document.on("click", "a[data-remote]", function(event, element) {
134
+ if (event.stopped) return;
135
+ handleRemote(element);
136
+ event.stop();
137
+ });
138
+
139
+ document.on("click", "a[data-method]", function(event, element) {
140
+ if (event.stopped) return;
141
+ handleMethod(element);
142
+ event.stop();
143
+ });
144
+
145
+ document.on("submit", function(event) {
146
+ var element = event.findElement(),
147
+ message = element.readAttribute('data-confirm');
148
+ if (message && !confirm(message)) {
149
+ event.stop();
150
+ return false;
151
+ }
152
+
153
+ var inputs = element.select("input[type=submit][data-disable-with]");
154
+ inputs.each(function(input) {
155
+ input.disabled = true;
156
+ input.writeAttribute('data-original-value', input.value);
157
+ input.value = input.readAttribute('data-disable-with');
158
+ });
159
+
160
+ var element = event.findElement("form[data-remote]");
161
+ if (element) {
162
+ handleRemote(element);
163
+ event.stop();
164
+ }
165
+ });
166
+
167
+ document.on("ajax:after", "form", function(event, element) {
168
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
169
+ inputs.each(function(input) {
170
+ input.value = input.readAttribute('data-original-value');
171
+ input.removeAttribute('data-original-value');
172
+ input.disabled = false;
173
+ });
174
+ });
175
+
176
+ Ajax.Responders.register({
177
+ onCreate: function(request) {
178
+ var csrf_meta_tag = $$('meta[name=csrf-token]')[0];
179
+
180
+ if (csrf_meta_tag) {
181
+ var header = 'X-CSRF-Token',
182
+ token = csrf_meta_tag.readAttribute('content');
183
+
184
+ if (!request.options.requestHeaders) {
185
+ request.options.requestHeaders = {};
186
+ }
187
+ request.options.requestHeaders[header] = token;
188
+ }
189
+ }
190
+ });
191
+ })();
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+ require 'rails/performance_test_help'
3
+
4
+ # Profiling results for each test method are written to tmp/performance.
5
+ class BrowsingTest < ActionDispatch::PerformanceTest
6
+ def test_homepage
7
+ get '/'
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.expand_path('../../config/environment', __FILE__)
3
+ require 'rails/test_help'
4
+
5
+ class ActiveSupport::TestCase
6
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
7
+ #
8
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
9
+ # -- they do not yet inherit this setting
10
+ fixtures :all
11
+
12
+ # Add more helper methods to be used by all tests here...
13
+ end
File without changes
@@ -0,0 +1,15 @@
1
+ module Autosuggest
2
+ module ControllerMacros
3
+ # when called, you must add a custom route for action like this:
4
+ # resources :products do
5
+ # get :autosuggest_brand_name, :on => :collection
6
+ # end
7
+ def autosuggest(object, name)
8
+ define_method "autosuggest_#{object}_#{name}" do
9
+ # assuming an ActiveRecord mysql backed model for right now
10
+ results = objectify(object).where("#{name} LIKE ?", "%#{params[:q]}%")
11
+ render :json => results.map{|r| {:name => r.send(:name), :value => r.id}}
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ module ActionView
2
+ module Helpers
3
+ module FormHelper
4
+ def autosuggest_field(object_name, method, source, options={})
5
+ text_field_class = "autosuggest_#{object_name}_#{method}"
6
+ options[:class] = "#{options[:class].to_s} #{text_field_class}"
7
+ autosuggest_options = options.delete(:autosuggest_options) || {}
8
+ autosuggest_options.reverse_merge!("selectedItemProp" => "name", "searchObjProps" => "name", "neverSubmit" => "true", "asHtmlName" => "#{object_name}[set_#{method}]")
9
+
10
+ _out = text_field(object_name, method, options)
11
+ _out << raw(%{
12
+ <script type="text/javascript">
13
+ $(document).ready(function(){
14
+ $('.#{text_field_class}').autoSuggest('#{source}', #{autosuggest_options.to_json});
15
+ });
16
+ </script>
17
+ })
18
+ _out
19
+ end
20
+ end
21
+
22
+ module FormTagHelper
23
+ def autosuggest_field_tag(name, value, source, options={})
24
+ raise "todo"
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ class ActionView::Helpers::FormBuilder #:nodoc:
31
+ def autosuggest_field(method, source, options = {})
32
+ @template.autosuggest_field(@object_name, method, source, objectify_options(options))
33
+ end
34
+ end
35
+
@@ -0,0 +1,12 @@
1
+ module Autosuggest
2
+ module Helpers
3
+ # Returns parameter object_sym as a constant
4
+ #
5
+ # objectify(:ingredient)
6
+ # # returns a Ingredient constant supposing it is already defined
7
+ #
8
+ def objectify(object_sym)
9
+ object_sym.to_s.camelize.constantize
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Autosuggest
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'autosuggest/form_helper'
2
+ require 'autosuggest/controller_macros'
3
+ require 'autosuggest/helpers'
4
+
5
+ class ActionController::Base
6
+ extend Autosuggest::ControllerMacros
7
+ include Autosuggest::Helpers
8
+ end
@@ -0,0 +1,217 @@
1
+ /* AutoSuggest CSS - Version 1.2 */
2
+
3
+ ul.as-selections {
4
+ list-style-type: none;
5
+ border-top: 1px solid #888;
6
+ border-bottom: 1px solid #b6b6b6;
7
+ border-left: 1px solid #aaa;
8
+ border-right: 1px solid #aaa;
9
+ padding: 4px 0 4px 4px;
10
+ margin: 0;
11
+ overflow: auto;
12
+ background-color: #fff;
13
+ box-shadow:inset 0 1px 2px #888;
14
+ -webkit-box-shadow:inset 0 1px 2px #888;
15
+ -moz-box-shadow:inset 0 1px 2px #888;
16
+ }
17
+
18
+ ul.as-selections.loading {
19
+ background-color: #eee;
20
+ }
21
+
22
+ ul.as-selections li {
23
+ float: left;
24
+ margin: 1px 4px 1px 0;
25
+ }
26
+
27
+ ul.as-selections li.as-selection-item {
28
+ color: #2b3840;
29
+ font-size: 13px;
30
+ font-family: "Lucida Grande", arial, sans-serif;
31
+ text-shadow: 0 1px 1px #fff;
32
+ background-color: #ddeefe;
33
+ background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#ddeefe), to(#bfe0f1));
34
+ border: 1px solid #acc3ec;
35
+ border-top-color: #c0d9e9;
36
+ padding: 2px 7px 2px 10px;
37
+ border-radius: 12px;
38
+ -webkit-border-radius: 12px;
39
+ -moz-border-radius: 12px;
40
+ box-shadow: 0 1px 1px #e4edf2;
41
+ -webkit-box-shadow: 0 1px 1px #e4edf2;
42
+ -moz-box-shadow: 0 1px 1px #e4edf2;
43
+ }
44
+
45
+ ul.as-selections li.as-selection-item:last-child {
46
+ margin-left: 30px;
47
+ }
48
+
49
+ ul.as-selections li.as-selection-item a.as-close {
50
+ float: right;
51
+ margin: 1px 0 0 7px;
52
+ padding: 0 2px;
53
+ cursor: pointer;
54
+ color: #5491be;
55
+ font-family: "Helvetica", helvetica, arial, sans-serif;
56
+ font-size: 14px;
57
+ font-weight: bold;
58
+ text-shadow: 0 1px 1px #fff;
59
+ -webkit-transition: color .1s ease-in;
60
+ }
61
+
62
+ ul.as-selections li.as-selection-item.blur {
63
+ color: #666666;
64
+ background-color: #f4f4f4;
65
+ background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#f4f4f4), to(#d5d5d5));
66
+ border-color: #bbb;
67
+ border-top-color: #ccc;
68
+ box-shadow: 0 1px 1px #e9e9e9;
69
+ -webkit-box-shadow: 0 1px 1px #e9e9e9;
70
+ -moz-box-shadow: 0 1px 1px #e9e9e9;
71
+ }
72
+
73
+ ul.as-selections li.as-selection-item.blur a.as-close {
74
+ color: #999;
75
+ }
76
+
77
+ ul.as-selections li:hover.as-selection-item {
78
+ color: #2b3840;
79
+ background-color: #bbd4f1;
80
+ background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#bbd4f1), to(#a3c2e5));
81
+ border-color: #6da0e0;
82
+ border-top-color: #8bb7ed;
83
+ }
84
+
85
+ ul.as-selections li:hover.as-selection-item a.as-close {
86
+ color: #4d70b0;
87
+ }
88
+
89
+ ul.as-selections li.as-selection-item.selected {
90
+ border-color: #1f30e4;
91
+ }
92
+
93
+ ul.as-selections li.as-selection-item a:hover.as-close {
94
+ color: #1b3c65;
95
+ }
96
+
97
+ ul.as-selections li.as-selection-item a:active.as-close {
98
+ color: #4d70b0;
99
+ }
100
+
101
+ ul.as-selections li.as-original {
102
+ margin-left: 0;
103
+ }
104
+
105
+ ul.as-selections li.as-original input {
106
+ border: none;
107
+ outline: none;
108
+ font-size: 13px;
109
+ width: 120px;
110
+ height: 18px;
111
+ padding-top: 3px;
112
+ }
113
+
114
+ ul.as-list {
115
+ position: absolute;
116
+ list-style-type: none;
117
+ margin: 2px 0 0 0;
118
+ padding: 0;
119
+ font-size: 14px;
120
+ color: #000;
121
+ font-family: "Lucida Grande", arial, sans-serif;
122
+ background-color: #fff;
123
+ background-color: rgba(255,255,255,0.95);
124
+ z-index: 2;
125
+ box-shadow: 0 2px 12px #222;
126
+ -webkit-box-shadow: 0 2px 12px #222;
127
+ -moz-box-shadow: 0 2px 12px #222;
128
+ border-radius: 5px;
129
+ -webkit-border-radius: 5px;
130
+ -moz-border-radius: 5px;
131
+ }
132
+
133
+ li.as-result-item, li.as-message {
134
+ margin: 0 0 0 0;
135
+ padding: 5px 12px;
136
+ background-color: transparent;
137
+ border: 1px solid #fff;
138
+ border-bottom: 1px solid #ddd;
139
+ cursor: pointer;
140
+ border-radius: 5px;
141
+ -webkit-border-radius: 5px;
142
+ -moz-border-radius: 5px;
143
+ }
144
+
145
+ li:first-child.as-result-item {
146
+ margin: 0;
147
+ }
148
+
149
+ li.as-message {
150
+ margin: 0;
151
+ cursor: default;
152
+ }
153
+
154
+ li.as-result-item.active {
155
+ background-color: #3668d9;
156
+ background-image: -webkit-gradient(linear, 0% 0%, 0% 64%, from(rgb(110, 129, 245)), to(rgb(62, 82, 242)));
157
+ border-color: #3342e8;
158
+ color: #fff;
159
+ text-shadow: 0 1px 2px #122042;
160
+ }
161
+
162
+ li.as-result-item em {
163
+ font-style: normal;
164
+ background: #444;
165
+ padding: 0 2px;
166
+ color: #fff;
167
+ }
168
+
169
+ li.as-result-item.active em {
170
+ background: #253f7a;
171
+ color: #fff;
172
+ }
173
+
174
+ /* Webkit Hacks */
175
+ @media screen and (-webkit-min-device-pixel-ratio:0) {
176
+ ul.as-selections {
177
+ border-top-width: 2px;
178
+ }
179
+ ul.as-selections li.as-selection-item {
180
+ padding-top: 3px;
181
+ padding-bottom: 3px;
182
+ }
183
+ ul.as-selections li.as-selection-item a.as-close {
184
+ margin-top: -1px;
185
+ }
186
+ ul.as-selections li.as-original input {
187
+ height: 19px;
188
+ }
189
+ }
190
+
191
+ /* Opera Hacks */
192
+ @media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) {
193
+ ul.as-list {
194
+ border: 1px solid #888;
195
+ }
196
+ ul.as-selections li.as-selection-item a.as-close {
197
+ margin-left: 4px;
198
+ margin-top: 0;
199
+ }
200
+ }
201
+
202
+ /* IE Hacks */
203
+ ul.as-list {
204
+ border: 1px solid #888\9;
205
+ }
206
+ ul.as-selections li.as-selection-item a.as-close {
207
+ margin-left: 4px\9;
208
+ margin-top: 0\9;
209
+ }
210
+
211
+ /* Firefox 3.0 Hacks */
212
+ ul.as-list, x:-moz-any-link, x:default {
213
+ border: 1px solid #888;
214
+ }
215
+ BODY:first-of-type ul.as-list, x:-moz-any-link, x:default { /* Target FF 3.5+ */
216
+ border: none;
217
+ }