searchable_models 0.0.1
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +212 -0
- data/Rakefile +32 -0
- data/lib/searchable_models/searchable.rb +135 -0
- data/lib/searchable_models/version.rb +3 -0
- data/lib/searchable_models.rb +8 -0
- data/lib/tasks/searchable_models_tasks.rake +4 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/car.rb +41 -0
- data/test/dummy/app/models/component.rb +3 -0
- data/test/dummy/app/models/door.rb +4 -0
- data/test/dummy/app/models/group.rb +2 -0
- data/test/dummy/app/models/provider.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +78 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/migrate/20141113130904_create_cars.rb +15 -0
- data/test/dummy/db/migrate/20141113142341_create_groups.rb +9 -0
- data/test/dummy/db/migrate/20141113142350_add_group_id_to_cars.rb +5 -0
- data/test/dummy/db/migrate/20141113153040_create_doors.rb +10 -0
- data/test/dummy/db/migrate/20141114120705_create_providers.rb +9 -0
- data/test/dummy/db/migrate/20141114120731_create_components.rb +11 -0
- data/test/dummy/db/migrate/20141114120804_add_component_id_to_doors.rb +5 -0
- data/test/dummy/db/migrate/20141114122828_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb +31 -0
- data/test/dummy/db/migrate/20141114122829_add_missing_unique_indices.acts_as_taggable_on_engine.rb +20 -0
- data/test/dummy/db/migrate/20141114122830_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb +15 -0
- data/test/dummy/db/migrate/20141114122831_add_missing_taggable_index.acts_as_taggable_on_engine.rb +10 -0
- data/test/dummy/db/migrate/20141114130703_add_car_translations.rb +9 -0
- data/test/dummy/db/schema.rb +28 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +305 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/fixtures/cars.yml +29 -0
- data/test/dummy/test/fixtures/components.yml +14 -0
- data/test/dummy/test/fixtures/doors.yml +14 -0
- data/test/dummy/test/fixtures/groups.yml +5 -0
- data/test/dummy/test/fixtures/providers.yml +8 -0
- data/test/dummy/test/models/car_test.rb +68 -0
- data/test/searchable_models_test.rb +7 -0
- data/test/test_helper.rb +34 -0
- metadata +274 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: e9e62955eaa1d05c716d44e22d3193ae0cc6777c
|
|
4
|
+
data.tar.gz: 753ea3a32ef65111c5830758af5019951dd6af44
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 66acdb4ecaf008930a5e870afd20e275daba8e6f34f141c6ff9ccdbf66f303a592ddd6d845a68e372cc6888cfb72803fa01fdce4303df62d930e80bf5d90cccb
|
|
7
|
+
data.tar.gz: e5c168949a068fbfa843e8d0546225271f725753502c38d19b3f703eebf2af3fbc97203b855cfd3cd6dcc85cb4b410e1dbefe52b894aa9e876e8e6cd911b9627
|
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright 2014 YOURNAME
|
|
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.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# SearchableModels
|
|
2
|
+
|
|
3
|
+
SearchableModels provide a `search` scope on ActiveRecord models.
|
|
4
|
+
|
|
5
|
+
This idea is to describe which fields are searchable and the gem will do the rest. You just need to call the `#search` method and you're set.
|
|
6
|
+
|
|
7
|
+
## Compatibility
|
|
8
|
+
|
|
9
|
+
SearchableModels has been tested and used with:
|
|
10
|
+
* ruby 2
|
|
11
|
+
* rails 4.1
|
|
12
|
+
|
|
13
|
+
Other versions may or may not work.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
To use it, add it to your Gemfile:
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
gem 'searchable_models', '~> 0.1'
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
and bundle:
|
|
24
|
+
|
|
25
|
+
```shell
|
|
26
|
+
bundle
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
You just need to use the `search_on` function in your models and the scope `search` will be available on your model.
|
|
32
|
+
|
|
33
|
+
There are different types of search. See below for the details.
|
|
34
|
+
|
|
35
|
+
### Simple search
|
|
36
|
+
This is the simplest search you could do. It will search for an exact match.
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
class Car < ActiveRecord::Base
|
|
40
|
+
search_on :name
|
|
41
|
+
search_on :number_of_doors
|
|
42
|
+
end
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
We can then search `Car`s with:
|
|
46
|
+
```ruby
|
|
47
|
+
Car.search(:name => "Ferrari")
|
|
48
|
+
Car.search(:number_of_doors => 3)
|
|
49
|
+
Car.search(:name => "Porsche", :number_of_doors => 4)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
This search will join all the conditions with an AND. It will use an exact match. This search can be done on any type of field: strings, integer and others.
|
|
53
|
+
|
|
54
|
+
### Fuzzy search
|
|
55
|
+
This search is similar to the simple search but will use a fuzzy operator instead of an exact match. This *must* used on string fields.
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
class Car < ActiveRecord::Base
|
|
59
|
+
search_on :name, :mode => :fuzzy
|
|
60
|
+
end
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
Car.search(:name => "rari")
|
|
65
|
+
Car.search(:name => "rsche")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Note that the search is case insensitive.
|
|
69
|
+
|
|
70
|
+
### Grouped search
|
|
71
|
+
Sometimes we want to use the same input parameter for several fields. For this, we can group the search.
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
class Car < ActiveRecord::Base
|
|
75
|
+
search_on :name, :mode => :fuzzy, :param => :query
|
|
76
|
+
search_on :model, :param => :query
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
Car.search(:query => "rari")
|
|
82
|
+
Car.search(:query => "Dodge")
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
You can group a simple and a fuzzy search together.
|
|
86
|
+
|
|
87
|
+
### Rename input parameter
|
|
88
|
+
The `param` option can also be used to rename the input parameter. Let's you want to map the parameter `car_name` to the column `name`:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
class Car < ActiveRecord::Base
|
|
92
|
+
search_on :name, :mode => :fuzzy, :param => :car_name
|
|
93
|
+
end
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
Car.search(:car_name => "rari")
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
This feature can be used with any type of search.
|
|
101
|
+
|
|
102
|
+
### Reference search
|
|
103
|
+
You can search on a `belongs_to` reference column.
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
class Car < ActiveRecord::Base
|
|
107
|
+
belongs_to :fleet
|
|
108
|
+
search_on :fleet_id
|
|
109
|
+
end
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
Car.search(:fleet_id)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Search on associated table
|
|
117
|
+
You can search on a column of an associated table
|
|
118
|
+
|
|
119
|
+
```ruby
|
|
120
|
+
class Car < ActiveRecord::Base
|
|
121
|
+
has_many :components
|
|
122
|
+
search_on :provider_name, :mode => :fuzzy, :through => :components
|
|
123
|
+
end
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
```ruby
|
|
127
|
+
Car.search(:provider_name => "Toshi")
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The `through` option is not limited to one table, you can nest as many as tables you may need
|
|
131
|
+
|
|
132
|
+
```ruby
|
|
133
|
+
class Car < ActiveRecord::Base
|
|
134
|
+
has_one :engine
|
|
135
|
+
search_on :provider_name, :mode => :fuzzy, :through => { :engine => :components }
|
|
136
|
+
end
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Scope search
|
|
140
|
+
You may want to use a custom scope in your search.
|
|
141
|
+
|
|
142
|
+
```ruby
|
|
143
|
+
class Car < ActiveRecord::Base
|
|
144
|
+
scope :imported_on, ->(date) { where(:import_date => date) }
|
|
145
|
+
|
|
146
|
+
search_on :import_date, :mode => :scope, :scope => :imported_on
|
|
147
|
+
end
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```ruby
|
|
151
|
+
Car.search(:import_date => "1990-10-10")
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
This feature is limited to scopes with only one parameter.
|
|
155
|
+
|
|
156
|
+
### Enum search
|
|
157
|
+
You can search using a field declared as a Rails 4 enum.
|
|
158
|
+
|
|
159
|
+
```ruby
|
|
160
|
+
class Car < ActiveRecord::Base
|
|
161
|
+
enum :kind => %i(van sedan family)
|
|
162
|
+
search_on :kind, :mode => :enum
|
|
163
|
+
end
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
```ruby
|
|
167
|
+
Car.search(:kind => "van")
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### `acts-as-taggable-on` support
|
|
171
|
+
If your model is tagged with [acts-as-taggable-on](https://github.com/mbleigh/acts-as-taggable-on), you can create a search on the tags. By default, all the tags conditions are merged using *AND* but you can specify an *OR* merge if you want.
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
```ruby
|
|
175
|
+
class Car < ActiveRecord::Base
|
|
176
|
+
acts_as_taggable
|
|
177
|
+
search_on :tags
|
|
178
|
+
end
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
```ruby
|
|
182
|
+
Car.search(:tags => [:blue, :old])
|
|
183
|
+
Car.search(:tags => [:blue, :yellow, :red], :tags_combination => :or)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### `globalize` support
|
|
187
|
+
If your model is translated using [https://github.com/globalize/globalize](Globalize), you can create a search on the translated fields.
|
|
188
|
+
|
|
189
|
+
```ruby
|
|
190
|
+
class Car < ActiveRecord::Base
|
|
191
|
+
translates :commercial_name
|
|
192
|
+
search_on :commercial_name, :mode => :fuzzy
|
|
193
|
+
end
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
```ruby
|
|
197
|
+
Car.search(:commercial_name => "van")
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
You can use an exact or a fuzzy search on translated fields
|
|
201
|
+
|
|
202
|
+
## Contributing
|
|
203
|
+
|
|
204
|
+
1. Fork it
|
|
205
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
206
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
207
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
208
|
+
5. Create new Pull Request
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
This project rocks and uses MIT-LICENSE.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
begin
|
|
2
|
+
require 'bundler/setup'
|
|
3
|
+
rescue LoadError
|
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
require 'rdoc/task'
|
|
8
|
+
|
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
11
|
+
rdoc.title = 'SearchableModels'
|
|
12
|
+
rdoc.options << '--line-numbers'
|
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
Bundler::GemHelper.install_tasks
|
|
21
|
+
|
|
22
|
+
require 'rake/testtask'
|
|
23
|
+
|
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
|
25
|
+
t.libs << 'lib'
|
|
26
|
+
t.libs << 'test'
|
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
|
28
|
+
t.verbose = false
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
task default: :test
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
module SearchableModels
|
|
2
|
+
module Searchable
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
mattr_accessor :search_fields, :instance_accessor => false do
|
|
7
|
+
{}
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
mattr_accessor :search_order, :instance_accessor => false do
|
|
11
|
+
:id
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
define_singleton_method(:search) do |params|
|
|
15
|
+
results = all
|
|
16
|
+
|
|
17
|
+
fields = search_fields.select do |_, v|
|
|
18
|
+
v.try(:exclude?, :mode) \
|
|
19
|
+
|| %i(exact scope enum).include?(v.try(:[], :mode))
|
|
20
|
+
end
|
|
21
|
+
results = _search(results, fields, params)
|
|
22
|
+
|
|
23
|
+
fields = search_fields.select { |_, v| v.try(:[], :mode) == :fuzzy }
|
|
24
|
+
results = _fuzzy_search(results, fields, params)
|
|
25
|
+
|
|
26
|
+
results.uniq.order(search_order)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
module ClassMethods
|
|
31
|
+
def search_on(*args)
|
|
32
|
+
self.search_fields.merge!(args.first => args.extract_options!)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def search_ordered_by(field)
|
|
36
|
+
self.search_order = field
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
## private search util functions, not part of the public API
|
|
42
|
+
def _search(results, fields, params)
|
|
43
|
+
fields.each do |field, options|
|
|
44
|
+
next unless (value = params[options[:param] || field])
|
|
45
|
+
results = case
|
|
46
|
+
when field == :tags
|
|
47
|
+
_tags_search(results, value, params[:tags_combination])
|
|
48
|
+
when field =~ /_id(?:s)?$/ && options[:through]
|
|
49
|
+
_association_ids_search(results, field, value, options[:through])
|
|
50
|
+
when options[:mode] == :scope && options[:scope]
|
|
51
|
+
_search_with_scope(results, value, options[:scope])
|
|
52
|
+
when options[:mode] == :enum
|
|
53
|
+
_search_with_enum(results, field, value)
|
|
54
|
+
else
|
|
55
|
+
_simple_search(results, field, value)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
results
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def _fuzzy_search(results, fields, params)
|
|
62
|
+
searchable_fields = {}
|
|
63
|
+
fields.each do |field, options|
|
|
64
|
+
_check_type_for_fuzzy_search(field)
|
|
65
|
+
param_key = options[:param] || field
|
|
66
|
+
searchable_fields[param_key] ||= []
|
|
67
|
+
searchable_fields[param_key] << field
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
searchable_fields.each do |param_key, columns|
|
|
71
|
+
next unless params[param_key]
|
|
72
|
+
where_string = ""
|
|
73
|
+
columns.each do |column|
|
|
74
|
+
where_string << " OR " unless where_string.blank?
|
|
75
|
+
table = table_name
|
|
76
|
+
if try(:translated?, column)
|
|
77
|
+
table = translation_options[:table_name]
|
|
78
|
+
results = results.joins(:translations)
|
|
79
|
+
end
|
|
80
|
+
where_string << "LOWER(#{table}.#{column}) like :value"
|
|
81
|
+
end
|
|
82
|
+
condition = { :value => "%#{params[param_key].to_s.downcase}%" }
|
|
83
|
+
results = results.where("(#{where_string})", condition)
|
|
84
|
+
end
|
|
85
|
+
results
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def _tags_search(results, value, tags_combination = "AND")
|
|
89
|
+
return results unless value.try(:any?)
|
|
90
|
+
case tags_combination
|
|
91
|
+
when :or
|
|
92
|
+
results.tagged_with(value, :any => true)
|
|
93
|
+
else
|
|
94
|
+
results.tagged_with(value)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def _search_with_scope(results, value, scope)
|
|
99
|
+
results.send(scope, value)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def _search_with_enum(results, field, value)
|
|
103
|
+
results.where(field => send(field.to_s.pluralize).try(:[], value))
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def _simple_search(results, field, value)
|
|
107
|
+
results.where(field => value)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def _association_ids_search(results, field, value, through)
|
|
111
|
+
return results unless through
|
|
112
|
+
table = _get_last_value(through).to_s.pluralize
|
|
113
|
+
results.joins(through).where(table => { field => value })
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def _get_last_value(object)
|
|
117
|
+
return _get_last_value(object.first.last) if object.is_a?(Hash)
|
|
118
|
+
object
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def _through_association_ids_search(results, field, value, through)
|
|
122
|
+
return results unless through
|
|
123
|
+
results.joins(through).where(through => { field => value })
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def _check_type_for_fuzzy_search(field)
|
|
127
|
+
return if try(:translated?, field) \
|
|
128
|
+
|| columns_hash[field.to_s].try(:type) == :string
|
|
129
|
+
fail(ArgumentError, "#{field} must of type string to run a fuzzy search")
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
ActiveRecord::Base.send(:include, SearchableModels::Searchable)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
== README
|
|
2
|
+
|
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
|
4
|
+
application up and running.
|
|
5
|
+
|
|
6
|
+
Things you may want to cover:
|
|
7
|
+
|
|
8
|
+
* Ruby version
|
|
9
|
+
|
|
10
|
+
* System dependencies
|
|
11
|
+
|
|
12
|
+
* Configuration
|
|
13
|
+
|
|
14
|
+
* Database creation
|
|
15
|
+
|
|
16
|
+
* Database initialization
|
|
17
|
+
|
|
18
|
+
* How to run the test suite
|
|
19
|
+
|
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
|
21
|
+
|
|
22
|
+
* Deployment instructions
|
|
23
|
+
|
|
24
|
+
* ...
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
|
2
|
+
// listed below.
|
|
3
|
+
//
|
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
|
6
|
+
//
|
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
|
8
|
+
// compiled file.
|
|
9
|
+
//
|
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
|
11
|
+
// about supported directives.
|
|
12
|
+
//
|
|
13
|
+
//= require_tree .
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
|
3
|
+
* listed below.
|
|
4
|
+
*
|
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
|
7
|
+
*
|
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
|
11
|
+
* file per style scope.
|
|
12
|
+
*
|
|
13
|
+
*= require_tree .
|
|
14
|
+
*= require_self
|
|
15
|
+
*/
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
class Car < ActiveRecord::Base
|
|
2
|
+
# fuzzy search on one field
|
|
3
|
+
search_on :brand, :mode => :fuzzy
|
|
4
|
+
|
|
5
|
+
# fuzzy search on one field with named parameter
|
|
6
|
+
search_on :model, :mode => :fuzzy, :param => :m
|
|
7
|
+
|
|
8
|
+
# fuzzy search on multiple fields with named parameter
|
|
9
|
+
search_on :name, :mode => :fuzzy, :param => :query
|
|
10
|
+
search_on :description, :mode => :fuzzy, :param => :query
|
|
11
|
+
|
|
12
|
+
# exact search on one field
|
|
13
|
+
search_on :number_of_doors
|
|
14
|
+
|
|
15
|
+
# search on belong_to ids
|
|
16
|
+
belongs_to :group
|
|
17
|
+
search_on :group_id
|
|
18
|
+
|
|
19
|
+
# search with through
|
|
20
|
+
has_many :doors
|
|
21
|
+
search_on :component_id, :through => :doors
|
|
22
|
+
|
|
23
|
+
# search with deep through
|
|
24
|
+
search_on :provider_id, :through => { :doors => :component }
|
|
25
|
+
|
|
26
|
+
# search with scope
|
|
27
|
+
scope :imported_on, ->(date) { where(:import_date => date) }
|
|
28
|
+
search_on :import_date, :mode => :scope, :scope => :imported_on
|
|
29
|
+
|
|
30
|
+
# search on enums
|
|
31
|
+
enum :kind => %i(van sedan family)
|
|
32
|
+
search_on :kind, :mode => :enum, :param => :type_of_car
|
|
33
|
+
|
|
34
|
+
# search on tags (support for acts-as-taggable-on)
|
|
35
|
+
acts_as_taggable
|
|
36
|
+
search_on :tags
|
|
37
|
+
|
|
38
|
+
# search on i18n fields (support for globalize)
|
|
39
|
+
translates :commercial_name
|
|
40
|
+
search_on :commercial_name, :mode => :fuzzy
|
|
41
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>Dummy</title>
|
|
5
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
|
6
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
|
7
|
+
<%= csrf_meta_tags %>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
|
|
11
|
+
<%= yield %>
|
|
12
|
+
|
|
13
|
+
</body>
|
|
14
|
+
</html>
|
data/test/dummy/bin/rake
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
|
2
|
+
|
|
3
|
+
require 'rails/all'
|
|
4
|
+
|
|
5
|
+
Bundler.require(*Rails.groups)
|
|
6
|
+
require "searchable_models"
|
|
7
|
+
|
|
8
|
+
module Dummy
|
|
9
|
+
class Application < Rails::Application
|
|
10
|
+
# Settings in config/environments/* take precedence over those specified here.
|
|
11
|
+
# Application configuration should go into files in config/initializers
|
|
12
|
+
# -- all .rb files in that directory are automatically loaded.
|
|
13
|
+
|
|
14
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
|
15
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
|
16
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
|
17
|
+
|
|
18
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
|
19
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
|
20
|
+
# config.i18n.default_locale = :de
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# SQLite version 3.x
|
|
2
|
+
# gem install sqlite3
|
|
3
|
+
#
|
|
4
|
+
# Ensure the SQLite 3 gem is defined in your Gemfile
|
|
5
|
+
# gem 'sqlite3'
|
|
6
|
+
#
|
|
7
|
+
default: &default
|
|
8
|
+
adapter: sqlite3
|
|
9
|
+
pool: 5
|
|
10
|
+
timeout: 5000
|
|
11
|
+
|
|
12
|
+
development:
|
|
13
|
+
<<: *default
|
|
14
|
+
database: db/development.sqlite3
|
|
15
|
+
|
|
16
|
+
# Warning: The database defined as "test" will be erased and
|
|
17
|
+
# re-generated from your development database when you run "rake".
|
|
18
|
+
# Do not set this db to the same as development or production.
|
|
19
|
+
test:
|
|
20
|
+
<<: *default
|
|
21
|
+
database: db/test.sqlite3
|
|
22
|
+
|
|
23
|
+
production:
|
|
24
|
+
<<: *default
|
|
25
|
+
database: db/production.sqlite3
|