autosuggest-rb 0.0.4 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.markdown +12 -0
- data/autosuggest-rb.gemspec +4 -1
- data/lib/autosuggest/controller_macros.rb +7 -2
- data/lib/autosuggest/form_helper.rb +18 -2
- data/lib/autosuggest/helpers.rb +45 -0
- data/lib/autosuggest/version.rb +1 -1
- data/test/db_stores_test.rb +12 -0
- data/test/form_helper_test.rb +5 -0
- data/test/support/active_record.rb +4 -4
- data/test/support/mongo_mapper.rb +39 -0
- data/test/support/mongoid.rb +43 -0
- data/test/test_helper.rb +2 -1
- metadata +42 -4
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Derrick Camerino
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
CHANGED
@@ -140,4 +140,16 @@ Example: resultClick: function(data){ console.log(data); }
|
|
140
140
|
**resultsComplete:** callback function - Custom function that is run when the suggestion results dropdown list is made visible. Will run after every search query.
|
141
141
|
|
142
142
|
|
143
|
+
## Author
|
144
|
+
|
145
|
+
Derrick Camerino http://programifications.com
|
146
|
+
|
147
|
+
## Contributing
|
148
|
+
|
149
|
+
Pull requests are very welcome! Make sure your patches are unit tested. Please do not change the version in your pull-request.
|
150
|
+
|
151
|
+
## Copyright
|
152
|
+
|
153
|
+
Copyright (c) 2009 Derrick Camerino. See LICENSE for details.
|
154
|
+
|
143
155
|
|
data/autosuggest-rb.gemspec
CHANGED
@@ -17,9 +17,12 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.add_dependency('rails', '~>3.0')
|
18
18
|
s.add_dependency('yajl-ruby')
|
19
19
|
|
20
|
-
s.add_development_dependency('sqlite3-ruby')
|
20
|
+
s.add_development_dependency('sqlite3-ruby', '1.3.3')
|
21
21
|
s.add_development_dependency('shoulda', '2.11.3')
|
22
22
|
s.add_development_dependency('ruby-debug19')
|
23
|
+
s.add_development_dependency('mongoid', '2.0.2')
|
24
|
+
s.add_development_dependency('mongo_mapper', '0.9.1')
|
25
|
+
s.add_development_dependency('bson_ext', '1.3.1')
|
23
26
|
|
24
27
|
s.files = `git ls-files`.split("\n")
|
25
28
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -12,9 +12,14 @@ module Autosuggest
|
|
12
12
|
limit = options[:limit] || 10
|
13
13
|
like_clause = defined?(PGconn) ? 'ILIKE' : 'LIKE'
|
14
14
|
|
15
|
+
options[:display] ||= name
|
16
|
+
options[:order] ||= "#{name} ASC"
|
17
|
+
options[:limit] ||= 10
|
18
|
+
options[:like_clause] = defined?(PGconn) ? 'ILIKE' : 'LIKE'
|
19
|
+
options[:name] = name
|
20
|
+
|
15
21
|
define_method "autosuggest_#{object}_#{name}" do
|
16
|
-
|
17
|
-
results = objectify(object).where("#{name} #{like_clause} ?", "%#{params[:query]}%").order(order).limit(limit)
|
22
|
+
results = db_store(object).query(options.merge(:query => params[:query], :object => objectify(object)))
|
18
23
|
render :json => Yajl::Encoder.encode(results.map{|r| {:name => r.send(display_name), :value => r.id}})
|
19
24
|
end
|
20
25
|
end
|
@@ -8,10 +8,13 @@ module ActionView
|
|
8
8
|
autosuggest_options.reverse_merge!("queryParam" => "query", "selectedItemProp" => "name", "searchObjProps" => "name", "neverSubmit" => "true", "asHtmlName" => "#{object_name}[set_#{method}]")
|
9
9
|
|
10
10
|
_out = text_field(object_name, method, options)
|
11
|
+
|
11
12
|
_out << raw(%{
|
12
13
|
<script type="text/javascript">
|
13
14
|
$(document).ready(function(){
|
14
|
-
|
15
|
+
// removing name attribute since values will be returned in #{object_name}[set_#{method}]
|
16
|
+
$('.#{text_field_class}').autoSuggest('#{source}', #{autosuggest_options.to_json})
|
17
|
+
.removeAttr('name');
|
15
18
|
});
|
16
19
|
</script>
|
17
20
|
})
|
@@ -21,7 +24,20 @@ module ActionView
|
|
21
24
|
|
22
25
|
module FormTagHelper
|
23
26
|
def autosuggest_field_tag(name, value, source, options={})
|
24
|
-
|
27
|
+
text_field_class = "autosuggest_#{name}"
|
28
|
+
options[:class] = "#{options[:class].to_s} #{text_field_class}"
|
29
|
+
autosuggest_options = options.delete(:autosuggest_options) || {}
|
30
|
+
autosuggest_options.reverse_merge!("queryParam" => "query", "selectedItemProp" => "name", "searchObjProps" => "name", "neverSubmit" => "true", "asHtmlName" => "#{name}")
|
31
|
+
|
32
|
+
_out = text_field_tag(name, value, options)
|
33
|
+
_out << raw(%{
|
34
|
+
<script type="text/javascript">
|
35
|
+
$(document).ready(function(){
|
36
|
+
$('.#{text_field_class}').autoSuggest('#{source}', #{autosuggest_options.to_json});
|
37
|
+
});
|
38
|
+
</script>
|
39
|
+
})
|
40
|
+
_out
|
25
41
|
end
|
26
42
|
end
|
27
43
|
end
|
data/lib/autosuggest/helpers.rb
CHANGED
@@ -1,4 +1,32 @@
|
|
1
1
|
module Autosuggest
|
2
|
+
module ActiveRecord
|
3
|
+
def self.query(options)
|
4
|
+
options[:object].where("#{options[:name]} #{options[:like_clause]} ?", "%#{options[:query]}%").order(options[:order]).limit(options[:limit])
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module Mongoid
|
9
|
+
def self.query(options)
|
10
|
+
options[:object].where(options[:name].to_sym => /.*#{options[:query]}.*/i).limit(options[:limit]).order_by(resolve_order_syntax(options[:order]))
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.resolve_order_syntax(order)
|
14
|
+
fields = order.split
|
15
|
+
[fields[0], fields[1].downcase.to_sym]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module MongoMapper
|
20
|
+
def self.query(options)
|
21
|
+
options[:object].where(options[:name].to_sym => /#{options[:query]}/i).limit(options[:limit]).sort(resolve_order_syntax(options[:order]))
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.resolve_order_syntax(order)
|
25
|
+
fields = order.split
|
26
|
+
[fields[0], fields[1].downcase.to_sym]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
2
30
|
module Helpers
|
3
31
|
# Returns parameter object_sym as a constant
|
4
32
|
#
|
@@ -8,5 +36,22 @@ module Autosuggest
|
|
8
36
|
def objectify(object_sym)
|
9
37
|
object_sym.to_s.camelize.constantize
|
10
38
|
end
|
39
|
+
|
40
|
+
# Decipher what database implementation the model is using
|
41
|
+
# db_store(:ingredient)
|
42
|
+
# # returns ActiveRecord assuming Ingredient is an AR model
|
43
|
+
#
|
44
|
+
def db_store(object)
|
45
|
+
ancestors = objectify(object).ancestors.map(&:to_s)
|
46
|
+
if ancestors.include?('ActiveRecord::Base')
|
47
|
+
ActiveRecord
|
48
|
+
elsif ancestors.include?('Mongoid::Document')
|
49
|
+
Mongoid
|
50
|
+
elsif ancestors.include?('MongoMapper::Document')
|
51
|
+
MongoMapper
|
52
|
+
else
|
53
|
+
raise 'Database Store not supported'
|
54
|
+
end
|
55
|
+
end
|
11
56
|
end
|
12
57
|
end
|
data/lib/autosuggest/version.rb
CHANGED
data/test/db_stores_test.rb
CHANGED
@@ -1,8 +1,20 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
require 'controller_test'
|
3
3
|
require 'support/active_record'
|
4
|
+
require 'support/mongoid'
|
5
|
+
require 'support/mongo_mapper'
|
4
6
|
|
5
7
|
class ActiveRecordControllerTest < ActionController::TestCase
|
6
8
|
include Autosuggest::TestCase::ActiveRecord
|
7
9
|
include Autosuggest::TestCase
|
8
10
|
end
|
11
|
+
|
12
|
+
class MongoidControllerTest < ActionController::TestCase
|
13
|
+
include Autosuggest::TestCase::Mongoid
|
14
|
+
include Autosuggest::TestCase
|
15
|
+
end
|
16
|
+
|
17
|
+
class MongoMapperControllerTest < ActionController::TestCase
|
18
|
+
include Autosuggest::TestCase::MongoMapper
|
19
|
+
include Autosuggest::TestCase
|
20
|
+
end
|
data/test/form_helper_test.rb
CHANGED
@@ -15,6 +15,11 @@ class FormHelperTest < ActionView::TestCase
|
|
15
15
|
|
16
16
|
context "autosuggest_field_tag" do
|
17
17
|
should "render properly" do
|
18
|
+
#raise "test in browser"
|
19
|
+
#raise "fix the params['tag'] issue"
|
20
|
+
output = autosuggest_field_tag(:some_name, '', 'some/path')
|
21
|
+
assert_match /class=\" autosuggest_some_name\"/, output
|
22
|
+
assert_match /\$\('.autosuggest_some_name'\)/, output
|
18
23
|
end
|
19
24
|
|
20
25
|
end
|
@@ -12,10 +12,6 @@ module Autosuggest
|
|
12
12
|
create_models
|
13
13
|
|
14
14
|
@controller = RecipesController.new
|
15
|
-
|
16
|
-
@tag1 = @tag_class.create(:name => 'Chinese')
|
17
|
-
@tag2 = @tag_class.create(:name => 'Chicken')
|
18
|
-
@tag3 = @tag_class.create(:name => 'Cajun')
|
19
15
|
end
|
20
16
|
|
21
17
|
def teardown
|
@@ -33,6 +29,10 @@ module Autosuggest
|
|
33
29
|
"Tag: #{name}"
|
34
30
|
end
|
35
31
|
end
|
32
|
+
|
33
|
+
@tag1 = @tag_class.create(:name => 'Chinese')
|
34
|
+
@tag2 = @tag_class.create(:name => 'Chicken')
|
35
|
+
@tag3 = @tag_class.create(:name => 'Cajun')
|
36
36
|
end
|
37
37
|
|
38
38
|
def destroy_models
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Autosuggest
|
2
|
+
module TestCase
|
3
|
+
module MongoMapper
|
4
|
+
def setup
|
5
|
+
::MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
|
6
|
+
::MongoMapper.database = "autosuggest-rb"
|
7
|
+
|
8
|
+
create_models
|
9
|
+
|
10
|
+
@controller = RecipesController.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
destroy_models
|
15
|
+
::MongoMapper.database.collections.select {|c| c.name !~ /system/ }.each(&:drop)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def create_models
|
20
|
+
@tag_class = Object.const_set(:Tag, Class.new)
|
21
|
+
@tag_class.send(:include, ::MongoMapper::Document)
|
22
|
+
@tag_class.key(:name, :class => String)
|
23
|
+
@tag_class.class_eval do
|
24
|
+
def display_name
|
25
|
+
"Tag: #{name}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
@tag1 = @tag_class.create(:name => 'Chinese')
|
30
|
+
@tag2 = @tag_class.create(:name => 'Chicken')
|
31
|
+
@tag3 = @tag_class.create(:name => 'Cajun')
|
32
|
+
end
|
33
|
+
|
34
|
+
def destroy_models
|
35
|
+
Object.send(:remove_const, :Tag)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Autosuggest
|
2
|
+
module TestCase
|
3
|
+
module Mongoid
|
4
|
+
def setup
|
5
|
+
::Mongoid.configure do |config|
|
6
|
+
name = "autosuggest-rb"
|
7
|
+
host = "localhost"
|
8
|
+
config.master = Mongo::Connection.new.db(name)
|
9
|
+
config.logger = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
create_models
|
13
|
+
|
14
|
+
@controller = RecipesController.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def teardown
|
18
|
+
destroy_models
|
19
|
+
::Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def create_models
|
24
|
+
@tag_class = Object.const_set(:Tag, Class.new)
|
25
|
+
@tag_class.send(:include, ::Mongoid::Document)
|
26
|
+
@tag_class.field(:name, :class => String)
|
27
|
+
@tag_class.class_eval do
|
28
|
+
def display_name
|
29
|
+
"Tag: #{name}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
@tag1 = @tag_class.create(:name => 'Chinese')
|
34
|
+
@tag2 = @tag_class.create(:name => 'Chicken')
|
35
|
+
@tag3 = @tag_class.create(:name => 'Cajun')
|
36
|
+
end
|
37
|
+
|
38
|
+
def destroy_models
|
39
|
+
Object.send(:remove_const, :Tag)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -3,6 +3,8 @@ ENV['RAILS_ENV'] ||= 'test'
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'bundler/setup'
|
5
5
|
require 'rails/all'
|
6
|
+
require 'mongoid'
|
7
|
+
require 'mongo_mapper'
|
6
8
|
require 'rails/test_help'
|
7
9
|
require 'autosuggest-rb'
|
8
10
|
require 'ruby-debug'
|
@@ -22,7 +24,6 @@ end
|
|
22
24
|
|
23
25
|
ActionController::Base.send :include, Autosuggest::Application.routes.url_helpers
|
24
26
|
|
25
|
-
Ingredient = Class.new
|
26
27
|
RecipesController = Class.new(ActionController::Base)
|
27
28
|
RecipesController.autosuggest(:tag, :name)
|
28
29
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: autosuggest-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.1.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Derrick Camerino
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-06-
|
13
|
+
date: 2011-06-03 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -41,9 +41,9 @@ dependencies:
|
|
41
41
|
requirement: &id003 !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
|
-
- - "
|
44
|
+
- - "="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
46
|
+
version: 1.3.3
|
47
47
|
type: :development
|
48
48
|
version_requirements: *id003
|
49
49
|
- !ruby/object:Gem::Dependency
|
@@ -68,6 +68,39 @@ dependencies:
|
|
68
68
|
version: "0"
|
69
69
|
type: :development
|
70
70
|
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: mongoid
|
73
|
+
prerelease: false
|
74
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - "="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 2.0.2
|
80
|
+
type: :development
|
81
|
+
version_requirements: *id006
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: mongo_mapper
|
84
|
+
prerelease: false
|
85
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - "="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 0.9.1
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id007
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: bson_ext
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - "="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.3.1
|
102
|
+
type: :development
|
103
|
+
version_requirements: *id008
|
71
104
|
description: This is a gem that wraps the AutoSuggest javascript plugin
|
72
105
|
email:
|
73
106
|
- robustdj@gmail.com
|
@@ -80,6 +113,7 @@ extra_rdoc_files: []
|
|
80
113
|
files:
|
81
114
|
- .gitignore
|
82
115
|
- Gemfile
|
116
|
+
- LICENSE
|
83
117
|
- README.markdown
|
84
118
|
- Rakefile
|
85
119
|
- autosuggest-rb.gemspec
|
@@ -95,6 +129,8 @@ files:
|
|
95
129
|
- test/db_stores_test.rb
|
96
130
|
- test/form_helper_test.rb
|
97
131
|
- test/support/active_record.rb
|
132
|
+
- test/support/mongo_mapper.rb
|
133
|
+
- test/support/mongoid.rb
|
98
134
|
- test/test_helper.rb
|
99
135
|
has_rdoc: true
|
100
136
|
homepage: ""
|
@@ -129,4 +165,6 @@ test_files:
|
|
129
165
|
- test/db_stores_test.rb
|
130
166
|
- test/form_helper_test.rb
|
131
167
|
- test/support/active_record.rb
|
168
|
+
- test/support/mongo_mapper.rb
|
169
|
+
- test/support/mongoid.rb
|
132
170
|
- test/test_helper.rb
|