rs_autocomplete_rails 0.5.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7a7ced81437e581c5d8ebb9f0739c31a47d92693
4
+ data.tar.gz: ea78519f404d15a80ce0c1bf0c42b969de0c4790
5
+ SHA512:
6
+ metadata.gz: 5d793edfe78629c5d7ddec6c179e17e60e8f23cf4131e3c42558a5bd687e72b8f99fcaab937d8a60ce836aeef68c295c2e84800379d80089b5e743ff6194ddcc
7
+ data.tar.gz: 80574970f375bb54c081dbb4065b767c40f8ff521be77bbc58f88a817f487f521f918ceb41f50da96a084fd1eed7bcd7eea591b666903458c349d210617a1fa6
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Justin Tomich
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/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'bundler/setup'
2
+ require 'bundler/gem_tasks'
3
+ require 'appraisal'
4
+
5
+ require 'rdoc/task'
6
+
7
+ RDoc::Task.new(:rdoc) do |rdoc|
8
+ rdoc.rdoc_dir = 'rdoc'
9
+ rdoc.title = 'AutocompleteRails'
10
+ rdoc.options << '--line-numbers'
11
+ rdoc.rdoc_files.include('README.rdoc')
12
+ rdoc.rdoc_files.include('lib/**/*.rb')
13
+ end
14
+
15
+ APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
16
+ load 'rails/tasks/engine.rake'
17
+ load 'rails/tasks/statistics.rake'
18
+ require 'rspec/core/rake_task'
19
+
20
+ namespace :dummy do
21
+ require_relative "spec/dummy/config/application"
22
+ Dummy::Application.load_tasks
23
+ end
24
+
25
+ RSpec::Core::RakeTask.new(:spec)
26
+
27
+ desc 'Run all specs in spec directory (excluding plugin specs)'
28
+ task default: :spec
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,8 @@
1
+ require 'autocomplete_rails/engine'
2
+ require 'autocomplete_rails/controller'
3
+
4
+ # Top level module of autocomplete_rails,
5
+ module AutocompleteRails
6
+ end
7
+
8
+ ActionController::Base.send(:include, AutocompleteRails::Controller)
@@ -0,0 +1,147 @@
1
+ module AutocompleteRails
2
+ module Controller
3
+ def self.included(target)
4
+ target.extend AutocompleteRails::Controller::ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ #
9
+ # Generate an autocomplete controller action.
10
+ #
11
+ # The controller action is intended to interact with jquery UI's autocomplete widget. The autocomplete
12
+ # controller provides suggestions while you type into a field that is provisioned with JQuery's autocomplete
13
+ # widget.
14
+ #
15
+ # The generated method is named "autocomplete_#{model_symbol}_#{value_method}", for example:
16
+ #
17
+ # class UsersController
18
+ # autocomplete :user, :email
19
+ # end
20
+ #
21
+ # generates a method named `autocomplete_user_email`.
22
+ #
23
+ #
24
+ # Parameters:
25
+ # * model_symbol - model class to autocomplete, e.g.
26
+ # * value_method - method on model to autocomplete. This supplies the 'value' field in results.
27
+ # Also used as the label unless you supply options[:label_method].
28
+ # * options - hash of optional settings.
29
+ # * &block - an optional block to further modify the results set,
30
+ # e.g. `{ |results| results.where(smth: @instance_variable) }`
31
+ #
32
+ #
33
+ # Options accepts a hash of:
34
+ # * :label_method - call a separate method for the label, otherwise defaults to value_method. If your label
35
+ # method is a method that is *not* a column in your DB, you may need options[:full_model].
36
+ # * :full_model - load full model instead of only selecting the specified values. Default is false.
37
+ # * :limit - default is 10.
38
+ # * :case_sensitive - if true, the search is case sensitive. Default is false.
39
+ # * :additional_data - collect additional data. Will be added to select unless full_model is invoked.
40
+ # * :full_search - search the entire value string for the term. Defaults to false, in which case the value
41
+ # field being searched (see value_method above) must start with the search term.
42
+ # * :scopes - Build your autocomplete query from the specified ActiveRecord scope(s). Multiple scopes can be
43
+ # used, pass them in as an array. Example: `scopes: [:scope1, :scope2]`
44
+ # If you need to pass additional arguments to your scope, define them as an array of arrays.
45
+ # Example: `scopes: [:scope1, [:scope2, 'argument 1', 'argument 2] ]`
46
+ # * :order - specify an order clause, defaults to 'LOWER(#{table}.#{value_method}) ASC'
47
+ #
48
+ # Be sure to add a route to reach the generated controller method. Example:
49
+ #
50
+ # resources :users do
51
+ # get :autocomplete_user_email, on: :collection
52
+ # end
53
+ #
54
+ # The following example searches for users by email, but displays their :full_name as the label.
55
+ # The full_model flag is also loaded, as full_name is a method that synthesizes multiple columns.
56
+ #
57
+ # class UsersController
58
+ # autocomplete :user, :email, label_method: full_name, full_model: true
59
+ # end
60
+ #
61
+ def autocomplete(model_symbol, value_method, options = {}, &block)
62
+ label_method = options[:label_method] || value_method
63
+ model = model_symbol.to_s.camelize.constantize
64
+ autocomplete_method_name = "autocomplete_#{model_symbol}_#{value_method}"
65
+
66
+ define_method(autocomplete_method_name) do
67
+ results = autocomplete_results(model, value_method, label_method, options, &block)
68
+ render json: autocomplete_build_json(results, value_method, label_method, options), root: false
69
+ end
70
+ end
71
+ end
72
+
73
+ protected
74
+
75
+ def autocomplete_results(model, value_method, label_method = nil, options, &block)
76
+ term = params[:term]
77
+ return {} if term.blank?
78
+
79
+ results = model.where(nil) # make an empty scope to add select, where, etc, to.
80
+ scopes = Array(options[:scopes])
81
+ unless scopes.empty?
82
+ scopes.each do |scope|
83
+ if scope.is_a?(Array)
84
+ results = results.send(scope.slice(0), *scope.drop(1))
85
+ else
86
+ results = results.send(scope)
87
+ end
88
+ end
89
+ end
90
+ results = instance_exec(results, &block) if block
91
+ results = results.select(autocomplete_select_clause(model, value_method, label_method, options)) unless
92
+ options[:full_model]
93
+ results.
94
+ where(autocomplete_where_clause(term, model, value_method, options)).
95
+ limit(autocomplete_limit_clause(options)).
96
+ order(autocomplete_order_clause(model, value_method, options))
97
+ end
98
+
99
+ def autocomplete_select_clause(model, value_method, label_method, options)
100
+ table_name = model.table_name
101
+ selects = []
102
+ selects << "#{table_name}.#{model.primary_key}"
103
+ selects << "#{table_name}.#{value_method}"
104
+ selects << "#{table_name}.#{label_method}" if label_method
105
+ options[:additional_data].each { |datum| selects << "#{table_name}.#{datum}" } if options[:additional_data]
106
+ selects
107
+ end
108
+
109
+ def autocomplete_where_clause(term, model, value_method, options)
110
+ term = term.gsub(/[_%]/) { |x| "\\#{x}" } # escape any _'s or %'s in the search term
111
+ term = "#{term}%"
112
+ term = "%#{term}" if options[:full_search]
113
+ table_name = model.table_name
114
+ lower = options[:case_sensitive] ? '' : 'LOWER'
115
+ ["#{lower}(#{table_name}.#{value_method}) LIKE #{lower}(?)", term] # escape default: \ on postgres, mysql, sqlite
116
+ #["#{lower}(#{table_name}.#{value_method}) LIKE #{lower}(?) ESCAPE \"\\\"", term] # use single-quotes, not double
117
+ end
118
+
119
+ def autocomplete_limit_clause(options)
120
+ options[:limit] ||= 10
121
+ end
122
+
123
+ def autocomplete_order_clause(model, value_method, options)
124
+ return options[:order] if options[:order]
125
+
126
+ # default to ASC order
127
+ table_prefix = "#{model.table_name}."
128
+ Arel.sql("LOWER(#{table_prefix}#{value_method}) ASC")
129
+ end
130
+
131
+ def autocomplete_build_json(results, value_method, label_method, options)
132
+ results.collect do |result|
133
+ data = HashWithIndifferentAccess.new(id: result.id,
134
+ label: result.send(label_method),
135
+ value: result.send(value_method))
136
+ options[:additional_data].each do |method|
137
+ data[method] = result.send(method)
138
+ end if options[:additional_data]
139
+ data
140
+ end
141
+ end
142
+
143
+ def postgres?(model_class)
144
+ model_class.connection.class.to_s.match /Postgre/
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,11 @@
1
+ require 'autocomplete_rails/controller'
2
+
3
+ module AutocompleteRails
4
+ class Engine < ::Rails::Engine
5
+ config.generators do |g|
6
+ g.test_framework :rspec
7
+ g.fixture_replacement :factory_bot, dir: 'spec/factories'
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,3 @@
1
+ module AutocompleteRails
2
+ VERSION = "0.5.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :autocomplete_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rs_autocomplete_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Tomich
8
+ - David Sowry
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2021-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '4.0'
21
+ - - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: '7'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: '4.0'
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '7'
34
+ - !ruby/object:Gem::Dependency
35
+ name: factory_bot
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.10.0
41
+ type: :development
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.10.0
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec-rails
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-mocks
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.1'
76
+ - !ruby/object:Gem::Dependency
77
+ name: shoulda-matchers
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.8'
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.8'
90
+ - !ruby/object:Gem::Dependency
91
+ name: sqlite3
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ - !ruby/object:Gem::Dependency
105
+ name: bundler
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ - !ruby/object:Gem::Dependency
119
+ name: rake
120
+ requirement: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ - !ruby/object:Gem::Dependency
133
+ name: appraisal
134
+ requirement: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ type: :development
140
+ prerelease: false
141
+ version_requirements: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ description: Fork of autocomplete_rails with addded rails 6 support.
147
+ email:
148
+ - justin@tomich.org
149
+ - sowry@rascality.nz
150
+ executables: []
151
+ extensions: []
152
+ extra_rdoc_files: []
153
+ files:
154
+ - MIT-LICENSE
155
+ - Rakefile
156
+ - config/routes.rb
157
+ - lib/autocomplete_rails.rb
158
+ - lib/autocomplete_rails/controller.rb
159
+ - lib/autocomplete_rails/engine.rb
160
+ - lib/autocomplete_rails/version.rb
161
+ - lib/tasks/autocomplete_rails_tasks.rake
162
+ homepage: https://github.com/Rascality/autocomplete_rails
163
+ licenses:
164
+ - MIT
165
+ metadata: {}
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubyforge_project:
182
+ rubygems_version: 2.6.14.4
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: Fork of autocomplete_rails with addded rails 6 support.
186
+ test_files: []