rails_autocomplete 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_autocomplete.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Omar Abdel-Wahab
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # RailsAutocomplete
2
+
3
+ A hassle-free autocomplete for Rails 3.2 using Twitter bootstrap.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rails_autocomplete'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rails_autocomplete
18
+
19
+ Then you need to add the following line to your application.js:
20
+
21
+ //= require rails_autocomplete
22
+
23
+ ## Usage
24
+
25
+ Assuming you have a Post model where you want to autocomplete posts based on their title field:
26
+
27
+
28
+ Add the following to app/views/posts/_form.html.erb:
29
+
30
+ <%= f.autocomplete_field :post, autocomplete_posts_path %>
31
+
32
+ app/controllers/posts_controller.rb:
33
+
34
+ def autocomplete
35
+ @posts = Post.autocomplete(:name, params[:q])
36
+ respond_to do |format|
37
+ format.json { render json: @posts }
38
+ end
39
+ end
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ module RailsAutocomplete
2
+ module ActiveRecordExtension
3
+ def autocomplete attr, query
4
+ where("LOWER(#{attr}) LIKE ?", "%#{query}%")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ require "rails_autocomplete/version"
2
+
3
+ module RailsAutocomplete
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ require 'action_view/helpers/capture_helper'
2
+ require 'action_view/helpers/sanitize_helper'
3
+ require 'action_view/helpers/url_helper'
4
+ require 'action_view/helpers/text_helper'
5
+ require "action_view/helpers/tag_helper"
6
+ require "action_view/helpers/form_tag_helper"
7
+
8
+ class ActionView::Helpers::FormBuilder
9
+ include ActionView::Helpers::FormTagHelper
10
+ include ActionView::Helpers::TagHelper
11
+
12
+ def autocomplete_field name, path
13
+ hash = Digest::MD5.hexdigest(name.to_s)
14
+ output = text_field_tag("#{name}_autocomplete", nil, { "class" => "rails-autocomplete", "data-autocomplete-parent" => hash, "data-autocomplete-url" => path, "data-provide" => "typeahead", "data-source" => "rails_autocomplete", "autocomplete" => "off" }).html_safe
15
+ output << self.hidden_field(name, { "data-autocomplete-child" => hash }).html_safe
16
+ output
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ require "rails_autocomplete/active_record_extension"
2
+
3
+ module RailsAutocomplete
4
+ class Railtie < ::Rails::Railtie
5
+ config.before_initialize do
6
+ ActiveSupport.on_load :active_record do
7
+ include RailsAutocomplete::ActiveRecordExtension
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAutocomplete
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "rails"
2
+ require "active_record"
3
+ require "rails_autocomplete/version"
4
+ require "rails_autocomplete/engine"
5
+ require "rails_autocomplete/railtie"
6
+ require "rails_autocomplete/form_helper"
7
+
8
+ ActiveRecord::Base.extend RailsAutocomplete::ActiveRecordExtension
9
+
10
+ module RailsAutocomplete
11
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_autocomplete/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rails_autocomplete"
8
+ gem.version = RailsAutocomplete::VERSION
9
+ gem.authors = ["Omar Abdel-Wahab"]
10
+ gem.email = ["owahab@gmail.com"]
11
+ gem.description = %q{Hassle-free autocomplete for Rails 3}
12
+ gem.summary = %q{Hassle-free autocomplete for Rails 3}
13
+ gem.homepage = "http://github.com/rayasocialmedia/"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "rails", ">= 3.1"
21
+ gem.add_dependency "jquery-rails"
22
+ gem.add_dependency "twitter-bootstrap-rails", ">= 2.1.0"
23
+
24
+ gem.add_development_dependency "bundler", ">= 1.0.0"
25
+ gem.add_development_dependency "rspec", "~> 2.3"
26
+ gem.add_development_dependency "sqlite3-ruby"
27
+ end
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+
3
+ describe "Autocomplete" do
4
+ context "Post" do
5
+ it "should return matching titles" do
6
+ Post.autocomplete(:title, 'a').map(&:title).should include('Alpha')
7
+ end
8
+ it "should not return non-matching titles" do
9
+ Post.autocomplete(:title, 'b').map(&:title).should_not include('Charile')
10
+ end
11
+ end
12
+ end
Binary file
@@ -0,0 +1,7 @@
1
+ require 'rails_autocomplete'
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
4
+ :database => File.dirname(__FILE__) + "/rails_autocomplete.sqlite3")
5
+ load File.dirname(__FILE__) + '/support/schema.rb'
6
+ load File.dirname(__FILE__) + '/support/models.rb'
7
+ load File.dirname(__FILE__) + '/support/data.rb'
@@ -0,0 +1,3 @@
1
+ Post.create(:title => "Alpha")
2
+ Post.create(:title => "Beta")
3
+ Post.create(:title => "Charlie")
@@ -0,0 +1,2 @@
1
+ class Post < ActiveRecord::Base
2
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :posts, :force => true do |t|
5
+ t.string :title
6
+ t.timestamps
7
+ end
8
+ end
@@ -0,0 +1,31 @@
1
+ $(function() {
2
+ $('.rails-autocomplete').typeahead({
3
+ source: function(query, process) {
4
+ items = [];
5
+ map = {};
6
+ return $.ajax({
7
+ url: $(this)[0].$element.data('autocomplete-url'),
8
+ context: $(this)[0].$element,
9
+ type: 'get',
10
+ data: { q: query },
11
+ dataType: 'json',
12
+ beforeSend: function(xhr) {
13
+ $(this).toggleClass('rails-autocomplete-loading');
14
+ },
15
+ success: function(data) {
16
+ $.each(data, function (i, item) {
17
+ map[item.name] = item;
18
+ items.push(item.name);
19
+ });
20
+ $(this).toggleClass('rails-autocomplete-loading');
21
+ return process(items);
22
+ }
23
+ });
24
+ },
25
+ updater: function(item) {
26
+ selectedItem = map[item].id;
27
+ $('input[data-autocomplete-child=' + $(this)[0].$element.data('autocomplete-parent') + ']').attr('value', selectedItem);
28
+ return item;
29
+ }
30
+ });
31
+ });
@@ -0,0 +1,3 @@
1
+ //= require jquery
2
+ //= require jquery-ui
3
+ //= require rails_autocomplete/autocomplete
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_autocomplete
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Omar Abdel-Wahab
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jquery-rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: twitter-bootstrap-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 2.1.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '2.3'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '2.3'
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3-ruby
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Hassle-free autocomplete for Rails 3
111
+ email:
112
+ - owahab@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - Gemfile
119
+ - LICENSE.txt
120
+ - README.md
121
+ - Rakefile
122
+ - lib/rails_autocomplete.rb
123
+ - lib/rails_autocomplete/active_record_extension.rb
124
+ - lib/rails_autocomplete/engine.rb
125
+ - lib/rails_autocomplete/form_helper.rb
126
+ - lib/rails_autocomplete/railtie.rb
127
+ - lib/rails_autocomplete/version.rb
128
+ - rails_autocomplete.gemspec
129
+ - spec/lib/rails_autocomplete_spec.rb
130
+ - spec/rails_autocomplete.sqlite3
131
+ - spec/spec_helper.rb
132
+ - spec/support/data.rb
133
+ - spec/support/models.rb
134
+ - spec/support/schema.rb
135
+ - vendor/assets/javascripts/rails_autocomplete.js
136
+ - vendor/assets/javascripts/rails_autocomplete/autocomplete.js
137
+ homepage: http://github.com/rayasocialmedia/
138
+ licenses: []
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 1.8.24
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: Hassle-free autocomplete for Rails 3
161
+ test_files:
162
+ - spec/lib/rails_autocomplete_spec.rb
163
+ - spec/rails_autocomplete.sqlite3
164
+ - spec/spec_helper.rb
165
+ - spec/support/data.rb
166
+ - spec/support/models.rb
167
+ - spec/support/schema.rb
168
+ has_rdoc: