ransack 1.8.4 → 3.2.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 +5 -5
- data/.github/FUNDING.yml +3 -0
- data/.github/SECURITY.md +12 -0
- data/.github/workflows/cronjob.yml +102 -0
- data/.github/workflows/deploy.yml +35 -0
- data/.github/workflows/rubocop.yml +20 -0
- data/.github/workflows/test-deploy.yml +29 -0
- data/.github/workflows/test.yml +130 -0
- data/.gitignore +3 -0
- data/{lib/ransack/adapters/mongoid/3.2/.gitkeep → .nojekyll} +0 -0
- data/.rubocop.yml +44 -0
- data/CHANGELOG.md +352 -0
- data/CONTRIBUTING.md +25 -13
- data/Gemfile +26 -27
- data/README.md +65 -815
- data/Rakefile +1 -22
- data/bug_report_templates/test-ransack-scope-and-column-same-name.rb +78 -0
- data/bug_report_templates/test-ransacker-arel-present-predicate.rb +71 -0
- data/docs/.gitignore +19 -0
- data/docs/.nojekyll +0 -0
- data/docs/babel.config.js +3 -0
- data/docs/blog/2022-03-27-ransack-3.0.0.md +20 -0
- data/docs/docs/getting-started/_category_.json +4 -0
- data/docs/docs/getting-started/advanced-mode.md +46 -0
- data/docs/docs/getting-started/configuration.md +47 -0
- data/docs/docs/getting-started/search-matches.md +67 -0
- data/docs/docs/getting-started/simple-mode.md +284 -0
- data/docs/docs/getting-started/sorting.md +79 -0
- data/docs/docs/getting-started/using-predicates.md +282 -0
- data/docs/docs/going-further/_category_.json +4 -0
- data/docs/docs/going-further/acts-as-taggable-on.md +114 -0
- data/docs/docs/going-further/associations.md +70 -0
- data/docs/docs/going-further/custom-predicates.md +52 -0
- data/docs/docs/going-further/documentation.md +43 -0
- data/docs/docs/going-further/exporting-to-csv.md +49 -0
- data/docs/docs/going-further/external-guides.md +57 -0
- data/docs/docs/going-further/form-customisation.md +63 -0
- data/docs/docs/going-further/i18n.md +53 -0
- data/docs/docs/going-further/img/create_release.png +0 -0
- data/docs/docs/going-further/merging-searches.md +41 -0
- data/docs/docs/going-further/other-notes.md +428 -0
- data/docs/docs/going-further/polymorphic-search.md +40 -0
- data/docs/docs/going-further/ransackers.md +331 -0
- data/docs/docs/going-further/release_process.md +36 -0
- data/docs/docs/going-further/saving-queries.md +82 -0
- data/docs/docs/going-further/searching-postgres.md +57 -0
- data/docs/docs/going-further/wiki-contributors.md +82 -0
- data/docs/docs/intro.md +99 -0
- data/docs/docusaurus.config.js +120 -0
- data/docs/package.json +38 -0
- data/docs/sidebars.js +31 -0
- data/docs/src/components/HomepageFeatures/index.js +64 -0
- data/docs/src/components/HomepageFeatures/styles.module.css +11 -0
- data/docs/src/css/custom.css +39 -0
- data/docs/src/pages/index.module.css +23 -0
- data/docs/src/pages/markdown-page.md +7 -0
- data/docs/static/.nojekyll +0 -0
- data/docs/static/img/docusaurus.png +0 -0
- data/docs/static/img/favicon.ico +0 -0
- data/docs/static/img/logo.svg +1 -0
- data/docs/static/img/tutorial/docsVersionDropdown.png +0 -0
- data/docs/static/img/tutorial/localeDropdown.png +0 -0
- data/docs/static/img/undraw_docusaurus_mountain.svg +171 -0
- data/docs/static/img/undraw_docusaurus_react.svg +170 -0
- data/docs/static/img/undraw_docusaurus_tree.svg +40 -0
- data/docs/static/logo/ransack-h.png +0 -0
- data/docs/static/logo/ransack-h.svg +34 -0
- data/docs/static/logo/ransack-v.png +0 -0
- data/docs/static/logo/ransack-v.svg +34 -0
- data/docs/static/logo/ransack.png +0 -0
- data/docs/static/logo/ransack.svg +21 -0
- data/docs/yarn.lock +8436 -0
- data/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +70 -0
- data/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +92 -0
- data/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +11 -0
- data/lib/polyamorous/activerecord_7.0_ruby_2/join_association.rb +1 -0
- data/lib/polyamorous/activerecord_7.0_ruby_2/join_dependency.rb +1 -0
- data/lib/polyamorous/activerecord_7.0_ruby_2/reflection.rb +1 -0
- data/lib/polyamorous/activerecord_7.1_ruby_2/join_association.rb +1 -0
- data/lib/polyamorous/activerecord_7.1_ruby_2/join_dependency.rb +1 -0
- data/lib/polyamorous/activerecord_7.1_ruby_2/reflection.rb +1 -0
- data/lib/polyamorous/join.rb +70 -0
- data/lib/polyamorous/polyamorous.rb +24 -0
- data/lib/polyamorous/swapping_reflection_class.rb +11 -0
- data/lib/polyamorous/tree_node.rb +7 -0
- data/lib/polyamorous.rb +1 -0
- data/lib/ransack/adapters/active_record/base.rb +14 -3
- data/lib/ransack/adapters/active_record/context.rb +140 -196
- data/lib/ransack/adapters/active_record/ransack/constants.rb +19 -4
- data/lib/ransack/adapters/active_record/ransack/context.rb +9 -19
- data/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +7 -7
- data/lib/ransack/adapters/active_record/ransack/translate.rb +1 -5
- data/lib/ransack/adapters/active_record/ransack/visitor.rb +23 -0
- data/lib/ransack/adapters/active_record.rb +0 -9
- data/lib/ransack/adapters.rb +2 -0
- data/lib/ransack/configuration.rb +52 -2
- data/lib/ransack/constants.rb +1 -5
- data/lib/ransack/context.rb +29 -24
- data/lib/ransack/helpers/form_builder.rb +12 -6
- data/lib/ransack/helpers/form_helper.rb +11 -3
- data/lib/ransack/helpers.rb +1 -1
- data/lib/ransack/locale/ar.yml +70 -0
- data/lib/ransack/locale/az.yml +70 -0
- data/lib/ransack/locale/bg.yml +70 -0
- data/lib/ransack/locale/ca.yml +70 -0
- data/lib/ransack/locale/el.yml +70 -0
- data/lib/ransack/locale/es.yml +22 -22
- data/lib/ransack/locale/fa.yml +70 -0
- data/lib/ransack/locale/fi.yml +71 -0
- data/lib/ransack/locale/nl.yml +4 -4
- data/lib/ransack/locale/ru.yml +70 -0
- data/lib/ransack/locale/sk.yml +70 -0
- data/lib/ransack/locale/sv.yml +70 -0
- data/lib/ransack/locale/tr.yml +70 -0
- data/lib/ransack/locale/zh-CN.yml +12 -12
- data/lib/ransack/nodes/attribute.rb +2 -2
- data/lib/ransack/nodes/condition.rb +7 -1
- data/lib/ransack/nodes/grouping.rb +3 -8
- data/lib/ransack/nodes/sort.rb +3 -3
- data/lib/ransack/nodes/value.rb +3 -3
- data/lib/ransack/predicate.rb +13 -20
- data/lib/ransack/search.rb +7 -4
- data/lib/ransack/translate.rb +115 -115
- data/lib/ransack/version.rb +1 -1
- data/lib/ransack/visitor.rb +1 -12
- data/lib/ransack.rb +7 -5
- data/ransack.gemspec +9 -25
- data/spec/blueprints/articles.rb +1 -1
- data/spec/blueprints/comments.rb +1 -1
- data/spec/blueprints/notes.rb +1 -1
- data/spec/blueprints/tags.rb +1 -1
- data/spec/console.rb +5 -5
- data/spec/helpers/polyamorous_helper.rb +13 -0
- data/spec/helpers/ransack_helper.rb +1 -1
- data/spec/polyamorous/activerecord_compatibility_spec.rb +15 -0
- data/spec/polyamorous/join_association_spec.rb +30 -0
- data/spec/polyamorous/join_dependency_spec.rb +81 -0
- data/spec/polyamorous/join_spec.rb +19 -0
- data/spec/ransack/adapters/active_record/base_spec.rb +105 -11
- data/spec/ransack/adapters/active_record/context_spec.rb +63 -24
- data/spec/ransack/configuration_spec.rb +24 -0
- data/spec/ransack/helpers/form_builder_spec.rb +3 -15
- data/spec/ransack/helpers/form_helper_spec.rb +135 -168
- data/spec/ransack/nodes/condition_spec.rb +13 -0
- data/spec/ransack/nodes/grouping_spec.rb +2 -2
- data/spec/ransack/nodes/value_spec.rb +115 -0
- data/spec/ransack/predicate_spec.rb +54 -2
- data/spec/ransack/search_spec.rb +266 -36
- data/spec/spec_helper.rb +14 -5
- data/spec/support/schema.rb +99 -21
- metadata +117 -187
- data/.travis.yml +0 -86
- data/lib/ransack/adapters/active_record/3.0/compat.rb +0 -179
- data/lib/ransack/adapters/active_record/3.0/context.rb +0 -203
- data/lib/ransack/adapters/active_record/3.1/context.rb +0 -212
- data/lib/ransack/adapters/active_record/3.2/context.rb +0 -44
- data/lib/ransack/adapters/active_record/compat.rb +0 -14
- data/lib/ransack/adapters/mongoid/attributes/attribute.rb +0 -37
- data/lib/ransack/adapters/mongoid/attributes/order_predications.rb +0 -17
- data/lib/ransack/adapters/mongoid/attributes/predications.rb +0 -141
- data/lib/ransack/adapters/mongoid/base.rb +0 -134
- data/lib/ransack/adapters/mongoid/context.rb +0 -212
- data/lib/ransack/adapters/mongoid/inquiry_hash.rb +0 -23
- data/lib/ransack/adapters/mongoid/ransack/constants.rb +0 -88
- data/lib/ransack/adapters/mongoid/ransack/context.rb +0 -60
- data/lib/ransack/adapters/mongoid/ransack/nodes/condition.rb +0 -27
- data/lib/ransack/adapters/mongoid/ransack/translate.rb +0 -13
- data/lib/ransack/adapters/mongoid/ransack/visitor.rb +0 -24
- data/lib/ransack/adapters/mongoid/table.rb +0 -35
- data/lib/ransack/adapters/mongoid.rb +0 -15
- data/spec/mongoid/adapters/mongoid/base_spec.rb +0 -314
- data/spec/mongoid/adapters/mongoid/context_spec.rb +0 -56
- data/spec/mongoid/configuration_spec.rb +0 -162
- data/spec/mongoid/dependencies_spec.rb +0 -8
- data/spec/mongoid/helpers/ransack_helper.rb +0 -11
- data/spec/mongoid/nodes/condition_spec.rb +0 -49
- data/spec/mongoid/nodes/grouping_spec.rb +0 -13
- data/spec/mongoid/predicate_spec.rb +0 -155
- data/spec/mongoid/search_spec.rb +0 -445
- data/spec/mongoid/support/mongoid.yml +0 -11
- data/spec/mongoid/support/schema.rb +0 -135
- data/spec/mongoid/translate_spec.rb +0 -14
- data/spec/mongoid_spec_helper.rb +0 -63
- data/spec/ransack/dependencies_spec.rb +0 -12
data/README.md
CHANGED
|
@@ -1,847 +1,59 @@
|
|
|
1
|
-
# Ransack
|
|
1
|
+
# 
|
|
2
2
|
|
|
3
|
-
[](https://github.com/activerecord-hackery/ransack/actions)
|
|
4
4
|
[](http://badge.fury.io/rb/ransack)
|
|
5
5
|
[](https://codeclimate.com/github/activerecord-hackery/ransack)
|
|
6
|
+
[](#backers) [](#sponsors)
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
created by [Ernie Miller](http://twitter.com/erniemiller)
|
|
9
|
-
and developed/maintained for years by
|
|
10
|
-
[Jon Atack](http://twitter.com/jonatack) and
|
|
11
|
-
[Ryan Bigg](http://twitter.com/ryanbigg) with the help of a great group of
|
|
12
|
-
[contributors](https://github.com/activerecord-hackery/ransack/graphs/contributors).
|
|
13
|
-
While it supports many of the same features as MetaSearch, its underlying
|
|
14
|
-
implementation differs greatly from MetaSearch,
|
|
15
|
-
and backwards compatibility is not a design goal.
|
|
16
|
-
|
|
17
|
-
Ransack enables the creation of both
|
|
18
|
-
[simple](http://ransack-demo.herokuapp.com) and
|
|
19
|
-
[advanced](http://ransack-demo.herokuapp.com/users/advanced_search) search forms
|
|
20
|
-
for your Ruby on Rails application
|
|
21
|
-
([demo source code here](https://github.com/activerecord-hackery/ransack_demo)).
|
|
22
|
-
If you're looking for something that simplifies query generation at the model
|
|
23
|
-
or controller layer, you're probably not looking for Ransack (or MetaSearch,
|
|
24
|
-
for that matter). Try [Squeel](https://github.com/activerecord-hackery/squeel)
|
|
25
|
-
instead.
|
|
26
|
-
|
|
27
|
-
If you're viewing this at
|
|
28
|
-
[github.com/activerecord-hackery/ransack](https://github.com/activerecord-hackery/ransack),
|
|
29
|
-
you're reading the documentation for the master branch with the latest features.
|
|
30
|
-
[View documentation for the last release (1.8.2).](https://github.com/activerecord-hackery/ransack/tree/v1.8.2)
|
|
31
|
-
|
|
32
|
-
## Getting started
|
|
33
|
-
|
|
34
|
-
Ransack is compatible with Rails 3, 4 and 5 on Ruby 1.9 and later.
|
|
35
|
-
JRuby 9 ought to work as well (see
|
|
36
|
-
[this](https://github.com/activerecord-hackery/polyamorous/issues/17)).
|
|
37
|
-
If you are using Ruby 1.8 or an earlier JRuby and run into compatibility
|
|
38
|
-
issues, you can use an earlier version of Ransack, say, up to 1.3.0.
|
|
39
|
-
|
|
40
|
-
Ransack works out-of-the-box with Active Record and also features limited
|
|
41
|
-
support for Mongoid 4 and 5 (without associations, further details
|
|
42
|
-
[below](https://github.com/activerecord-hackery/ransack#mongoid)).
|
|
43
|
-
|
|
44
|
-
In your Gemfile, for the last officially released gem:
|
|
45
|
-
|
|
46
|
-
```ruby
|
|
47
|
-
gem 'ransack'
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
If you would like to use the latest updates (recommended), use the `master`
|
|
51
|
-
branch:
|
|
52
|
-
|
|
53
|
-
```ruby
|
|
54
|
-
gem 'ransack', github: 'activerecord-hackery/ransack'
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
## Issues tracker
|
|
58
|
-
|
|
59
|
-
* Before filing an issue, please read the [Contributing Guide](CONTRIBUTING.md).
|
|
60
|
-
* File an issue if a bug is caused by Ransack, is new (has not already been reported), and _can be reproduced from the information you provide_.
|
|
61
|
-
* Contributions are welcome, but please do not add "+1" comments to issues or pull requests :smiley:
|
|
62
|
-
* Please do not use the issue tracker for personal support requests. Stack Overflow is a better place for that where a wider community can help you!
|
|
63
|
-
|
|
64
|
-
## Usage
|
|
8
|
+
# SPECIAL ANNOUNCEMENT
|
|
65
9
|
|
|
66
|
-
Ransack
|
|
67
|
-
|
|
68
|
-
### Simple Mode
|
|
69
|
-
|
|
70
|
-
This mode works much like MetaSearch, for those of you who are familiar with
|
|
71
|
-
it, and requires very little setup effort.
|
|
72
|
-
|
|
73
|
-
If you're coming from MetaSearch, things to note:
|
|
74
|
-
|
|
75
|
-
1. The default param key for search params is now `:q`, instead of `:search`.
|
|
76
|
-
This is primarily to shorten query strings, though advanced queries (below)
|
|
77
|
-
will still run afoul of URL length limits in most browsers and require a
|
|
78
|
-
switch to HTTP POST requests. This key is [configurable](https://github.com/activerecord-hackery/ransack/wiki/Configuration).
|
|
79
|
-
|
|
80
|
-
2. `form_for` is now `search_form_for`, and validates that a Ransack::Search
|
|
81
|
-
object is passed to it.
|
|
82
|
-
|
|
83
|
-
3. Common ActiveRecord::Relation methods are no longer delegated by the
|
|
84
|
-
search object. Instead, you will get your search results (an
|
|
85
|
-
ActiveRecord::Relation in the case of the ActiveRecord adapter) via a call to
|
|
86
|
-
`Ransack#result`.
|
|
87
|
-
|
|
88
|
-
#### In your controller
|
|
89
|
-
|
|
90
|
-
```ruby
|
|
91
|
-
def index
|
|
92
|
-
@q = Person.ransack(params[:q])
|
|
93
|
-
@people = @q.result(distinct: true)
|
|
94
|
-
end
|
|
95
|
-
```
|
|
96
|
-
or without `distinct: true`, for sorting on an associated table's columns (in
|
|
97
|
-
this example, with preloading each Person's Articles and pagination):
|
|
98
|
-
|
|
99
|
-
```ruby
|
|
100
|
-
def index
|
|
101
|
-
@q = Person.ransack(params[:q])
|
|
102
|
-
@people = @q.result.includes(:articles).page(params[:page])
|
|
103
|
-
|
|
104
|
-
# or use `to_a.uniq` to remove duplicates (can also be done in the view):
|
|
105
|
-
@people = @q.result.includes(:articles).page(params[:page]).to_a.uniq
|
|
106
|
-
end
|
|
107
|
-
```
|
|
10
|
+
Please see the [Ransack Blog](https://activerecord-hackery.github.io/ransack/blog) for a special announcement from the Ransack maintainers and Ernie Miller, the original author of Ransack.
|
|
108
11
|
|
|
109
|
-
#### In your view
|
|
110
12
|
|
|
111
|
-
|
|
112
|
-
which are defined in
|
|
113
|
-
[Ransack::Helpers::FormHelper](lib/ransack/helpers/form_helper.rb).
|
|
114
|
-
|
|
115
|
-
#### Ransack's `search_form_for` helper replaces `form_for` for creating the view search form
|
|
116
|
-
|
|
117
|
-
```erb
|
|
118
|
-
<%= search_form_for @q do |f| %>
|
|
119
|
-
|
|
120
|
-
# Search if the name field contains...
|
|
121
|
-
<%= f.label :name_cont %>
|
|
122
|
-
<%= f.search_field :name_cont %>
|
|
123
|
-
|
|
124
|
-
# Search if an associated articles.title starts with...
|
|
125
|
-
<%= f.label :articles_title_start %>
|
|
126
|
-
<%= f.search_field :articles_title_start %>
|
|
127
|
-
|
|
128
|
-
# Attributes may be chained. Search multiple attributes for one value...
|
|
129
|
-
<%= f.label :name_or_description_or_email_or_articles_title_cont %>
|
|
130
|
-
<%= f.search_field :name_or_description_or_email_or_articles_title_cont %>
|
|
131
|
-
|
|
132
|
-
<%= f.submit %>
|
|
133
|
-
<% end %>
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
`cont` (contains) and `start` (starts with) are just two of the available
|
|
137
|
-
search predicates. See
|
|
138
|
-
[Constants](https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/constants.rb)
|
|
139
|
-
for a full list and the
|
|
140
|
-
[wiki](https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching)
|
|
141
|
-
for more information.
|
|
142
|
-
|
|
143
|
-
The `search_form_for` answer format can be set like this:
|
|
144
|
-
|
|
145
|
-
```erb
|
|
146
|
-
<%= search_form_for(@q, format: :pdf) do |f| %>
|
|
147
|
-
|
|
148
|
-
<%= search_form_for(@q, format: :json) do |f| %>
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
#### Ransack's `sort_link` helper creates table headers that are sortable links
|
|
152
|
-
|
|
153
|
-
```erb
|
|
154
|
-
<%= sort_link(@q, :name) %>
|
|
155
|
-
```
|
|
156
|
-
Additional options can be passed after the column attribute, like a different
|
|
157
|
-
column title or a default sort order:
|
|
158
|
-
|
|
159
|
-
```erb
|
|
160
|
-
<%= sort_link(@q, :name, 'Last Name', default_order: :desc) %>
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
You can use a block if the link markup is hard to fit into the label parameter:
|
|
164
|
-
|
|
165
|
-
```erb
|
|
166
|
-
<%= sort_link(@q, :name) do %>
|
|
167
|
-
<strong>Player Name</strong>
|
|
168
|
-
<% end %>
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
With a polymorphic association, you may need to specify the name of the link
|
|
172
|
-
explicitly to avoid an `uninitialized constant Model::Xxxable` error (see issue
|
|
173
|
-
[#421](https://github.com/activerecord-hackery/ransack/issues/421)):
|
|
174
|
-
|
|
175
|
-
```erb
|
|
176
|
-
<%= sort_link(@q, :xxxable_of_Ymodel_type_some_attribute, 'Attribute Name') %>
|
|
177
|
-
```
|
|
13
|
+
# Introduction
|
|
178
14
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
```erb
|
|
182
|
-
<%= sort_link(@q, :last_name, [:last_name, 'first_name asc'], 'Last Name') %>
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
In the example above, clicking the link will sort by `last_name` and then
|
|
186
|
-
`first_name`. Specifying the sort direction on a field in the array tells
|
|
187
|
-
Ransack to _always_ sort that particular field in the specified direction.
|
|
188
|
-
|
|
189
|
-
Multiple `default_order` fields may also be specified with a hash:
|
|
190
|
-
|
|
191
|
-
```erb
|
|
192
|
-
<%= sort_link(@q, :last_name, %i(last_name first_name),
|
|
193
|
-
default_order: { last_name: 'asc', first_name: 'desc' }) %>
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
This example toggles the sort directions of both fields, by default
|
|
197
|
-
initially sorting the `last_name` field by ascending order, and the
|
|
198
|
-
`first_name` field by descending order.
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
The sort link order indicator arrows may be globally customized by setting a
|
|
202
|
-
`custom_arrows` option in an initializer file like
|
|
203
|
-
`config/initializers/ransack.rb`.
|
|
204
|
-
|
|
205
|
-
You can also enable a `default_arrow` which is displayed on all sortable fields
|
|
206
|
-
which are not currently used in the sorting. This is disabled by default so
|
|
207
|
-
nothing will be displayed:
|
|
208
|
-
|
|
209
|
-
```ruby
|
|
210
|
-
Ransack.configure do |c|
|
|
211
|
-
c.custom_arrows = {
|
|
212
|
-
up_arrow: '<i class="custom-up-arrow-icon"></i>',
|
|
213
|
-
down_arrow: 'U+02193',
|
|
214
|
-
default_arrow: '<i class="default-arrow-icon"></i>'
|
|
215
|
-
}
|
|
216
|
-
end
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
All sort links may be displayed without the order indicator
|
|
220
|
-
arrows by setting `hide_sort_order_indicators` to true in the initializer file.
|
|
221
|
-
Note that this hides the arrows even if they were customized:
|
|
222
|
-
|
|
223
|
-
```ruby
|
|
224
|
-
Ransack.configure do |c|
|
|
225
|
-
c.hide_sort_order_indicators = true
|
|
226
|
-
end
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
Without setting it globally, individual sort links may be displayed without
|
|
230
|
-
the order indicator arrow by passing `hide_indicator: true` in the sort link:
|
|
231
|
-
|
|
232
|
-
```erb
|
|
233
|
-
<%= sort_link(@q, :name, hide_indicator: true) %>
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
#### Ransack's `sort_url` helper is like a `sort_link` but returns only the url
|
|
237
|
-
|
|
238
|
-
`sort_url` has the same API as `sort_link`:
|
|
239
|
-
|
|
240
|
-
```erb
|
|
241
|
-
<%= sort_url(@q, :name, default_order: :desc) %>
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
```erb
|
|
245
|
-
<%= sort_url(@q, :last_name, [:last_name, 'first_name asc']) %>
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
```erb
|
|
249
|
-
<%= sort_url(@q, :last_name, %i(last_name first_name),
|
|
250
|
-
default_order: { last_name: 'asc', first_name: 'desc' }) %>
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
### Advanced Mode
|
|
254
|
-
|
|
255
|
-
"Advanced" searches (ab)use Rails' nested attributes functionality in order to
|
|
256
|
-
generate complex queries with nested AND/OR groupings, etc. This takes a bit
|
|
257
|
-
more work but can generate some pretty cool search interfaces that put a lot of
|
|
258
|
-
power in the hands of your users. A notable drawback with these searches is
|
|
259
|
-
that the increased size of the parameter string will typically force you to use
|
|
260
|
-
the HTTP POST method instead of GET. :(
|
|
261
|
-
|
|
262
|
-
This means you'll need to tweak your routes...
|
|
263
|
-
|
|
264
|
-
```ruby
|
|
265
|
-
resources :people do
|
|
266
|
-
collection do
|
|
267
|
-
match 'search' => 'people#search', via: [:get, :post], as: :search
|
|
268
|
-
end
|
|
269
|
-
end
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
... and add another controller action ...
|
|
273
|
-
|
|
274
|
-
```ruby
|
|
275
|
-
def search
|
|
276
|
-
index
|
|
277
|
-
render :index
|
|
278
|
-
end
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
... and update your `search_form_for` line in the view ...
|
|
282
|
-
|
|
283
|
-
```erb
|
|
284
|
-
<%= search_form_for @q, url: search_people_path,
|
|
285
|
-
html: { method: :post } do |f| %>
|
|
286
|
-
```
|
|
287
|
-
|
|
288
|
-
Once you've done so, you can make use of the helpers in [Ransack::Helpers::FormBuilder](lib/ransack/helpers/form_builder.rb) to
|
|
289
|
-
construct much more complex search forms, such as the one on the
|
|
290
|
-
[demo app](http://ransack-demo.herokuapp.com/users/advanced_search)
|
|
291
|
-
(source code [here](https://github.com/activerecord-hackery/ransack_demo)).
|
|
292
|
-
|
|
293
|
-
### Ransack #search method
|
|
294
|
-
|
|
295
|
-
Ransack will try to make the class method `#search` available in your
|
|
296
|
-
models, but if `#search` has already been defined elsewhere, you can always use
|
|
297
|
-
the default `#ransack` class method. So the following are equivalent:
|
|
298
|
-
|
|
299
|
-
```ruby
|
|
300
|
-
Article.ransack(params[:q])
|
|
301
|
-
Article.search(params[:q])
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
Users have reported issues of `#search` name conflicts with other gems, so
|
|
305
|
-
the `#search` method alias will be deprecated in the next major version of
|
|
306
|
-
Ransack (2.0). It's advisable to use the default `#ransack` instead.
|
|
307
|
-
|
|
308
|
-
For now, if Ransack's `#search` method conflicts with the name of another
|
|
309
|
-
method named `search` in your code or another gem, you may resolve it either by
|
|
310
|
-
patching the `extended` class_method in `Ransack::Adapters::ActiveRecord::Base`
|
|
311
|
-
to remove the line `alias :search :ransack unless base.respond_to? :search`, or
|
|
312
|
-
by placing the following line in your Ransack initializer file at
|
|
313
|
-
`config/initializers/ransack.rb`:
|
|
314
|
-
|
|
315
|
-
```ruby
|
|
316
|
-
Ransack::Adapters::ActiveRecord::Base.class_eval('remove_method :search')
|
|
317
|
-
```
|
|
15
|
+
Ransack will help you easily add **searching to your Rails application**, without any additional dependencies.
|
|
318
16
|
|
|
319
|
-
|
|
17
|
+
There are advanced searching solutions around, like ElasticSearch or Algolia. **Ransack** will do the job for many Rails websites, without the need to run additional infrastructure or work in a different language. With Ransack you do it all with standard Ruby and ERB.
|
|
320
18
|
|
|
321
|
-
|
|
322
|
-
associations.
|
|
19
|
+
Ready to move beyond the basics? Use **advanced features** like i18n and extensive configuration options.
|
|
323
20
|
|
|
324
|
-
|
|
21
|
+
Ransack is supported for Rails 7.0, 6.x on Ruby 2.6.6 and later.
|
|
325
22
|
|
|
326
|
-
|
|
327
|
-
class Employee < ActiveRecord::Base
|
|
328
|
-
belongs_to :supervisor
|
|
23
|
+
## Installation
|
|
329
24
|
|
|
330
|
-
|
|
331
|
-
end
|
|
25
|
+
To install `ransack` and add it to your Gemfile, run
|
|
332
26
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
# has attribute title:string
|
|
337
|
-
end
|
|
338
|
-
|
|
339
|
-
class Supervisor < ActiveRecord::Base
|
|
340
|
-
belongs_to :department
|
|
341
|
-
has_many :employees
|
|
342
|
-
|
|
343
|
-
# has attribute last_name:string
|
|
344
|
-
end
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
... and a controller...
|
|
348
|
-
|
|
349
|
-
```ruby
|
|
350
|
-
class SupervisorsController < ApplicationController
|
|
351
|
-
def index
|
|
352
|
-
@q = Supervisor.ransack(params[:q])
|
|
353
|
-
@supervisors = @q.result.includes(:department, :employees)
|
|
354
|
-
end
|
|
355
|
-
end
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
... you might set up your form like this...
|
|
359
|
-
|
|
360
|
-
```erb
|
|
361
|
-
<%= search_form_for @q do |f| %>
|
|
362
|
-
<%= f.label :last_name_cont %>
|
|
363
|
-
<%= f.search_field :last_name_cont %>
|
|
364
|
-
|
|
365
|
-
<%= f.label :department_title_cont %>
|
|
366
|
-
<%= f.search_field :department_title_cont %>
|
|
367
|
-
|
|
368
|
-
<%= f.label :employees_first_name_or_employees_last_name_cont %>
|
|
369
|
-
<%= f.search_field :employees_first_name_or_employees_last_name_cont %>
|
|
370
|
-
|
|
371
|
-
<%= f.submit "search" %>
|
|
372
|
-
<% end %>
|
|
373
|
-
...
|
|
374
|
-
<%= content_tag :table do %>
|
|
375
|
-
<%= content_tag :th, sort_link(@q, :last_name) %>
|
|
376
|
-
<%= content_tag :th, sort_link(@q, :department_title) %>
|
|
377
|
-
<%= content_tag :th, sort_link(@q, :employees_last_name) %>
|
|
378
|
-
<% end %>
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
If you have trouble sorting on associations, try using an SQL string with the
|
|
382
|
-
pluralized table (`'departments.title'`,`'employees.last_name'`) instead of the
|
|
383
|
-
symbolized association (`:department_title)`, `:employees_last_name`).
|
|
384
|
-
|
|
385
|
-
### Ransack Aliases
|
|
386
|
-
|
|
387
|
-
You can customize the attribute names for your Ransack searches by using a
|
|
388
|
-
`ransack_alias`. This is particularly useful for long attribute names that are
|
|
389
|
-
necessary when querying associations or multiple columns.
|
|
390
|
-
|
|
391
|
-
```ruby
|
|
392
|
-
class Post < ActiveRecord::Base
|
|
393
|
-
belongs_to :author
|
|
394
|
-
|
|
395
|
-
# Abbreviate :author_first_name_or_author_last_name to :author
|
|
396
|
-
ransack_alias :author, :author_first_name_or_author_last_name
|
|
397
|
-
end
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
Now, rather than using `:author_first_name_or_author_last_name_cont` in your
|
|
401
|
-
form, you can simply use `:author_cont`. This serves to produce more expressive
|
|
402
|
-
query parameters in your URLs.
|
|
403
|
-
|
|
404
|
-
```erb
|
|
405
|
-
<%= search_form_for @q do |f| %>
|
|
406
|
-
<%= f.label :author_cont %>
|
|
407
|
-
<%= f.search_field :author_cont %>
|
|
408
|
-
<% end %>
|
|
409
|
-
```
|
|
410
|
-
|
|
411
|
-
### Search Matchers
|
|
412
|
-
|
|
413
|
-
List of all possible predicates
|
|
414
|
-
|
|
415
|
-
* `*_eq` - equal
|
|
416
|
-
* `*_not_eq` - not equal
|
|
417
|
-
* `*_matches` - matches with `LIKE`, e.g. `q[email_matches]=%@gmail.com`
|
|
418
|
-
* Also: `*_does_not_match`, `*_matches_any`, `*_matches_all`, `*_does_not_match_any`, `*_does_not_match_all`
|
|
419
|
-
* `*_lt` - less than
|
|
420
|
-
* `*_lteq` - less than or equal
|
|
421
|
-
* `*_gt` - greater than
|
|
422
|
-
* `*_gteq` - greater than or equal
|
|
423
|
-
* `*_present` - not null and not empty, e.g. `q[name_present]=1` (SQL: `col is not null AND col != ''`)
|
|
424
|
-
* `*_blank` - is null or empty. (SQL: `col is null OR col = ''`)
|
|
425
|
-
* `*_null`, `*_not_null` - is null, is not null
|
|
426
|
-
* `*_in` - match any values in array, e.g. `q[name_in][]=Alice&q[name_in][]=Bob`
|
|
427
|
-
* `*_not_in` - match none of values in array
|
|
428
|
-
* `*_lt_any`, `*_lteq_any`, `*_gt_any`, `*_gteq_any` - Compare to list of values, at least positive. (SQL: `col > value1 OR col > value2`)
|
|
429
|
-
* `*_matches_any`, `*_does_not_match_any` - same as above but with `LIKE`
|
|
430
|
-
* `*_lt_all`, `*_lteq_all`, `*_gt_all`, `*_gteq_all` - Compare to list of values, all positive. (SQL: `col > value1 AND col > value2`)
|
|
431
|
-
* `*_matches_all`, `*_does_not_match_all` - same as above but with `LIKE`
|
|
432
|
-
* `*_not_eq_all` - none of values in a set
|
|
433
|
-
* `*_start`, `*_not_start`, `*_start_any`, `*_start_all`, `*_not_start_any`, `*_not_start_all` - start with, (SQL: `col LIKE 'value%'`)
|
|
434
|
-
* `*_end`, `*_not_end`, `*_end_any`, `*_end_all`, `*_not_end_any`, `*_not_end_all` - end with, (SQL: `col LIKE '%value'`)
|
|
435
|
-
* `*_cont`, `*_cont_any`, `*_cont_all`, `*_not_cont`, `*_not_cont_any`, `*_not_cont_all` - contains value, using `LIKE`
|
|
436
|
-
* `*_true`, `*_false` - is true and is false
|
|
437
|
-
|
|
438
|
-
(See full list: https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/locale/en.yml#L15 and [wiki](https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching))
|
|
439
|
-
|
|
440
|
-
### Using Ransackers to add custom search functions via Arel
|
|
441
|
-
|
|
442
|
-
The main premise behind Ransack is to provide access to
|
|
443
|
-
**Arel predicate methods**. Ransack provides special methods, called
|
|
444
|
-
_ransackers_, for creating additional search functions via Arel. More
|
|
445
|
-
information about `ransacker` methods can be found [here in the wiki](https://github.com/activerecord-hackery/ransack/wiki/Using-Ransackers).
|
|
446
|
-
Feel free to contribute working `ransacker` code examples to the wiki!
|
|
447
|
-
|
|
448
|
-
### Problem with DISTINCT selects
|
|
449
|
-
|
|
450
|
-
If passed `distinct: true`, `result` will generate a `SELECT DISTINCT` to
|
|
451
|
-
avoid returning duplicate rows, even if conditions on a join would otherwise
|
|
452
|
-
result in some. It generates the same SQL as calling `uniq` on the relation.
|
|
453
|
-
|
|
454
|
-
Please note that for many databases, a sort on an associated table's columns
|
|
455
|
-
may result in invalid SQL with `distinct: true` -- in those cases, you will
|
|
456
|
-
will need to modify the result as needed to allow these queries to work.
|
|
457
|
-
|
|
458
|
-
For example, you could call joins and includes on the result which has the
|
|
459
|
-
effect of adding those tables columns to the select statement, overcoming
|
|
460
|
-
the issue, like so:
|
|
461
|
-
|
|
462
|
-
```ruby
|
|
463
|
-
def index
|
|
464
|
-
@q = Person.ransack(params[:q])
|
|
465
|
-
@people = @q.result(distinct: true)
|
|
466
|
-
.includes(:articles)
|
|
467
|
-
.joins(:articles)
|
|
468
|
-
.page(params[:page])
|
|
469
|
-
end
|
|
470
|
-
```
|
|
471
|
-
|
|
472
|
-
If the above doesn't help, you can also use ActiveRecord's `select` query
|
|
473
|
-
to explicitly add the columns you need, which brute force's adding the
|
|
474
|
-
columns you need that your SQL engine is complaining about, you need to
|
|
475
|
-
make sure you give all of the columns you care about, for example:
|
|
476
|
-
|
|
477
|
-
```ruby
|
|
478
|
-
def index
|
|
479
|
-
@q = Person.ransack(params[:q])
|
|
480
|
-
@people = @q.result(distinct: true)
|
|
481
|
-
.select('people.*, articles.name, articles.description')
|
|
482
|
-
.page(params[:page])
|
|
483
|
-
end
|
|
484
|
-
```
|
|
485
|
-
|
|
486
|
-
A final way of last resort is to call `to_a.uniq` on the collection at the end
|
|
487
|
-
with the caveat that the de-duping is taking place in Ruby instead of in SQL,
|
|
488
|
-
which is potentially slower and uses more memory, and that it may display
|
|
489
|
-
awkwardly with pagination if the number of results is greater than the page size.
|
|
490
|
-
|
|
491
|
-
For example:
|
|
492
|
-
|
|
493
|
-
```ruby
|
|
494
|
-
def index
|
|
495
|
-
@q = Person.ransack(params[:q])
|
|
496
|
-
@people = @q.result.includes(:articles).page(params[:page]).to_a.uniq
|
|
497
|
-
end
|
|
498
|
-
```
|
|
499
|
-
|
|
500
|
-
#### `PG::UndefinedFunction: ERROR: could not identify an equality operator for type json`
|
|
501
|
-
|
|
502
|
-
If you get the above error while using `distinct: true` that means that
|
|
503
|
-
one of the columns that Ransack is selecting is a `json` column.
|
|
504
|
-
PostgreSQL does not provide comparison operators for the `json` type. While
|
|
505
|
-
it is possible to work around this, in practice it's much better to convert those
|
|
506
|
-
to `jsonb`, as [recommended by the PostgreSQL documentation](https://www.postgresql.org/docs/9.6/static/datatype-json.html).
|
|
507
|
-
|
|
508
|
-
### Authorization (whitelisting/blacklisting)
|
|
509
|
-
|
|
510
|
-
By default, searching and sorting are authorized on any column of your model
|
|
511
|
-
and no class methods/scopes are whitelisted.
|
|
512
|
-
|
|
513
|
-
Ransack adds four methods to `ActiveRecord::Base` that you can redefine as
|
|
514
|
-
class methods in your models to apply selective authorization:
|
|
515
|
-
`ransackable_attributes`, `ransackable_associations`, `ransackable_scopes` and
|
|
516
|
-
`ransortable_attributes`.
|
|
517
|
-
|
|
518
|
-
Here is how these four methods are implemented in Ransack:
|
|
519
|
-
|
|
520
|
-
```ruby
|
|
521
|
-
# `ransackable_attributes` by default returns all column names
|
|
522
|
-
# and any defined ransackers as an array of strings.
|
|
523
|
-
# For overriding with a whitelist array of strings.
|
|
524
|
-
#
|
|
525
|
-
def ransackable_attributes(auth_object = nil)
|
|
526
|
-
column_names + _ransackers.keys
|
|
527
|
-
end
|
|
528
|
-
|
|
529
|
-
# `ransackable_associations` by default returns the names
|
|
530
|
-
# of all associations as an array of strings.
|
|
531
|
-
# For overriding with a whitelist array of strings.
|
|
532
|
-
#
|
|
533
|
-
def ransackable_associations(auth_object = nil)
|
|
534
|
-
reflect_on_all_associations.map { |a| a.name.to_s }
|
|
535
|
-
end
|
|
536
|
-
|
|
537
|
-
# `ransortable_attributes` by default returns the names
|
|
538
|
-
# of all attributes available for sorting as an array of strings.
|
|
539
|
-
# For overriding with a whitelist array of strings.
|
|
540
|
-
#
|
|
541
|
-
def ransortable_attributes(auth_object = nil)
|
|
542
|
-
ransackable_attributes(auth_object)
|
|
543
|
-
end
|
|
544
|
-
|
|
545
|
-
# `ransackable_scopes` by default returns an empty array
|
|
546
|
-
# i.e. no class methods/scopes are authorized.
|
|
547
|
-
# For overriding with a whitelist array of *symbols*.
|
|
548
|
-
#
|
|
549
|
-
def ransackable_scopes(auth_object = nil)
|
|
550
|
-
[]
|
|
551
|
-
end
|
|
552
|
-
```
|
|
553
|
-
|
|
554
|
-
Any values not returned from these methods will be ignored by Ransack, i.e.
|
|
555
|
-
they are not authorized.
|
|
556
|
-
|
|
557
|
-
All four methods can receive a single optional parameter, `auth_object`. When
|
|
558
|
-
you call the search or ransack method on your model, you can provide a value
|
|
559
|
-
for an `auth_object` key in the options hash which can be used by your own
|
|
560
|
-
overridden methods.
|
|
561
|
-
|
|
562
|
-
Here is an example that puts all this together, adapted from
|
|
563
|
-
[this blog post by Ernie Miller](http://erniemiller.org/2012/05/11/why-your-ruby-class-macros-might-suck-mine-did/).
|
|
564
|
-
In an `Article` model, add the following `ransackable_attributes` class method
|
|
565
|
-
(preferably private):
|
|
566
|
-
|
|
567
|
-
```ruby
|
|
568
|
-
class Article < ActiveRecord::Base
|
|
569
|
-
def self.ransackable_attributes(auth_object = nil)
|
|
570
|
-
if auth_object == :admin
|
|
571
|
-
# whitelist all attributes for admin
|
|
572
|
-
super
|
|
573
|
-
else
|
|
574
|
-
# whitelist only the title and body attributes for other users
|
|
575
|
-
super & %w(title body)
|
|
576
|
-
end
|
|
577
|
-
end
|
|
578
|
-
|
|
579
|
-
private_class_method :ransackable_attributes
|
|
580
|
-
end
|
|
581
|
-
```
|
|
582
|
-
|
|
583
|
-
Here is example code for the `articles_controller`:
|
|
584
|
-
|
|
585
|
-
```ruby
|
|
586
|
-
class ArticlesController < ApplicationController
|
|
587
|
-
def index
|
|
588
|
-
@q = Article.ransack(params[:q], auth_object: set_ransack_auth_object)
|
|
589
|
-
@articles = @q.result
|
|
590
|
-
end
|
|
591
|
-
|
|
592
|
-
private
|
|
593
|
-
|
|
594
|
-
def set_ransack_auth_object
|
|
595
|
-
current_user.admin? ? :admin : nil
|
|
596
|
-
end
|
|
597
|
-
end
|
|
598
|
-
```
|
|
599
|
-
|
|
600
|
-
Trying it out in `rails console`:
|
|
601
|
-
|
|
602
|
-
```ruby
|
|
603
|
-
> Article
|
|
604
|
-
=> Article(id: integer, person_id: integer, title: string, body: text)
|
|
605
|
-
|
|
606
|
-
> Article.ransackable_attributes
|
|
607
|
-
=> ["title", "body"]
|
|
608
|
-
|
|
609
|
-
> Article.ransackable_attributes(:admin)
|
|
610
|
-
=> ["id", "person_id", "title", "body"]
|
|
611
|
-
|
|
612
|
-
> Article.ransack(id_eq: 1).result.to_sql
|
|
613
|
-
=> SELECT "articles".* FROM "articles" # Note that search param was ignored!
|
|
614
|
-
|
|
615
|
-
> Article.ransack({ id_eq: 1 }, { auth_object: nil }).result.to_sql
|
|
616
|
-
=> SELECT "articles".* FROM "articles" # Search param still ignored!
|
|
617
|
-
|
|
618
|
-
> Article.ransack({ id_eq: 1 }, { auth_object: :admin }).result.to_sql
|
|
619
|
-
=> SELECT "articles".* FROM "articles" WHERE "articles"."id" = 1
|
|
620
|
-
```
|
|
621
|
-
|
|
622
|
-
That's it! Now you know how to whitelist/blacklist various elements in Ransack.
|
|
623
|
-
|
|
624
|
-
### Using Scopes/Class Methods
|
|
625
|
-
|
|
626
|
-
Continuing on from the preceding section, searching by scopes requires defining
|
|
627
|
-
a whitelist of `ransackable_scopes` on the model class. The whitelist should be
|
|
628
|
-
an array of *symbols*. By default, all class methods (e.g. scopes) are ignored.
|
|
629
|
-
Scopes will be applied for matching `true` values, or for given values if the
|
|
630
|
-
scope accepts a value:
|
|
631
|
-
|
|
632
|
-
```ruby
|
|
633
|
-
class Employee < ActiveRecord::Base
|
|
634
|
-
scope :activated, ->(boolean = true) { where(active: boolean) }
|
|
635
|
-
scope :salary_gt, ->(amount) { where('salary > ?', amount) }
|
|
636
|
-
|
|
637
|
-
# Scopes are just syntactical sugar for class methods, which may also be used:
|
|
638
|
-
|
|
639
|
-
def self.hired_since(date)
|
|
640
|
-
where('start_date >= ?', date)
|
|
641
|
-
end
|
|
642
|
-
|
|
643
|
-
def self.ransackable_scopes(auth_object = nil)
|
|
644
|
-
if auth_object.try(:admin?)
|
|
645
|
-
# allow admin users access to all three methods
|
|
646
|
-
%i(activated hired_since salary_gt)
|
|
647
|
-
else
|
|
648
|
-
# allow other users to search on `activated` and `hired_since` only
|
|
649
|
-
%i(activated hired_since)
|
|
650
|
-
end
|
|
651
|
-
end
|
|
652
|
-
|
|
653
|
-
private_class_method :ransackable_scopes
|
|
654
|
-
end
|
|
655
|
-
|
|
656
|
-
Employee.ransack({ activated: true, hired_since: '2013-01-01' })
|
|
657
|
-
|
|
658
|
-
Employee.ransack({ salary_gt: 100_000 }, { auth_object: current_user })
|
|
659
|
-
```
|
|
660
|
-
|
|
661
|
-
In Rails 3 and 4, if the `true` value is being passed via url params or some
|
|
662
|
-
other mechanism that will convert it to a string, the true value may not be
|
|
663
|
-
passed to the ransackable scope unless you wrap it in an array
|
|
664
|
-
(i.e. `activated: ['true']`). Ransack will take care of changing 'true' into a
|
|
665
|
-
boolean. This is currently resolved in Rails 5 :smiley:
|
|
666
|
-
|
|
667
|
-
However, perhaps you have `user_id: [1]` and you do not want Ransack to convert
|
|
668
|
-
1 into a boolean. (Values sanitized to booleans can be found in the
|
|
669
|
-
[constants.rb](https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/constants.rb#L28)).
|
|
670
|
-
To turn this off, and handle type conversions yourself, set
|
|
671
|
-
`sanitize_custom_scope_booleans` to false in an initializer file like
|
|
672
|
-
config/initializers/ransack.rb:
|
|
673
|
-
|
|
674
|
-
```ruby
|
|
675
|
-
Ransack.configure do |c|
|
|
676
|
-
c.sanitize_custom_scope_booleans = false
|
|
677
|
-
end
|
|
678
|
-
```
|
|
679
|
-
|
|
680
|
-
Scopes are a recent addition to Ransack and currently have a few caveats:
|
|
681
|
-
First, a scope involving child associations needs to be defined in the parent
|
|
682
|
-
table model, not in the child model. Second, scopes with an array as an
|
|
683
|
-
argument are not easily usable yet, because the array currently needs to be
|
|
684
|
-
wrapped in an array to function (see
|
|
685
|
-
[this issue](https://github.com/activerecord-hackery/ransack/issues/404)),
|
|
686
|
-
which is not compatible with Ransack form helpers. For this use case, it may be
|
|
687
|
-
better for now to use [ransackers](https://github.com/activerecord-hackery/ransack/wiki/Using-Ransackers) instead,
|
|
688
|
-
where feasible. Pull requests with solutions and tests are welcome!
|
|
689
|
-
|
|
690
|
-
### Grouping queries by OR instead of AND
|
|
691
|
-
|
|
692
|
-
The default `AND` grouping can be changed to `OR` by adding `m: 'or'` to the
|
|
693
|
-
query hash.
|
|
694
|
-
|
|
695
|
-
You can easily try it in your controller code by changing `params[:q]` in the
|
|
696
|
-
`index` action to `params[:q].try(:merge, m: 'or')` as follows:
|
|
697
|
-
|
|
698
|
-
```ruby
|
|
699
|
-
def index
|
|
700
|
-
@q = Artist.ransack(params[:q].try(:merge, m: 'or'))
|
|
701
|
-
@artists = @q.result
|
|
702
|
-
end
|
|
703
|
-
```
|
|
704
|
-
Normally, if you wanted users to be able to toggle between `AND` and `OR`
|
|
705
|
-
query grouping, you would probably set up your search form so that `m` was in
|
|
706
|
-
the URL params hash, but here we assigned `m` manually just to try it out
|
|
707
|
-
quickly.
|
|
708
|
-
|
|
709
|
-
Alternatively, trying it in the Rails console:
|
|
710
|
-
|
|
711
|
-
```ruby
|
|
712
|
-
artists = Artist.ransack(name_cont: 'foo', style_cont: 'bar', m: 'or')
|
|
713
|
-
=> Ransack::Search<class: Artist, base: Grouping <conditions: [
|
|
714
|
-
Condition <attributes: ["name"], predicate: cont, values: ["foo"]>,
|
|
715
|
-
Condition <attributes: ["style"], predicate: cont, values: ["bar"]>
|
|
716
|
-
], combinator: or>>
|
|
717
|
-
|
|
718
|
-
artists.result.to_sql
|
|
719
|
-
=> "SELECT \"artists\".* FROM \"artists\"
|
|
720
|
-
WHERE ((\"artists\".\"name\" ILIKE '%foo%'
|
|
721
|
-
OR \"artists\".\"style\" ILIKE '%bar%'))"
|
|
722
|
-
```
|
|
723
|
-
|
|
724
|
-
The combinator becomes `or` instead of the default `and`, and the SQL query
|
|
725
|
-
becomes `WHERE...OR` instead of `WHERE...AND`.
|
|
726
|
-
|
|
727
|
-
This works with associations as well. Imagine an Artist model that has many
|
|
728
|
-
Memberships, and many Musicians through Memberships:
|
|
729
|
-
|
|
730
|
-
```ruby
|
|
731
|
-
artists = Artist.ransack(name_cont: 'foo', musicians_email_cont: 'bar', m: 'or')
|
|
732
|
-
=> Ransack::Search<class: Artist, base: Grouping <conditions: [
|
|
733
|
-
Condition <attributes: ["name"], predicate: cont, values: ["foo"]>,
|
|
734
|
-
Condition <attributes: ["musicians_email"], predicate: cont, values: ["bar"]>
|
|
735
|
-
], combinator: or>>
|
|
736
|
-
|
|
737
|
-
artists.result.to_sql
|
|
738
|
-
=> "SELECT \"artists\".* FROM \"artists\"
|
|
739
|
-
LEFT OUTER JOIN \"memberships\"
|
|
740
|
-
ON \"memberships\".\"artist_id\" = \"artists\".\"id\"
|
|
741
|
-
LEFT OUTER JOIN \"musicians\"
|
|
742
|
-
ON \"musicians\".\"id\" = \"memberships\".\"musician_id\"
|
|
743
|
-
WHERE ((\"artists\".\"name\" ILIKE '%foo%'
|
|
744
|
-
OR \"musicians\".\"email\" ILIKE '%bar%'))"
|
|
745
|
-
```
|
|
746
|
-
|
|
747
|
-
### Using SimpleForm
|
|
748
|
-
|
|
749
|
-
If you would like to combine the Ransack and SimpleForm form builders, set the
|
|
750
|
-
`RANSACK_FORM_BUILDER` environment variable before Rails boots up, e.g. in
|
|
751
|
-
`config/application.rb` before `require 'rails/all'` as shown below (and add
|
|
752
|
-
`gem 'simple_form'` in your Gemfile).
|
|
753
|
-
|
|
754
|
-
```ruby
|
|
755
|
-
require File.expand_path('../boot', __FILE__)
|
|
756
|
-
ENV['RANSACK_FORM_BUILDER'] = '::SimpleForm::FormBuilder'
|
|
757
|
-
require 'rails/all'
|
|
758
|
-
```
|
|
759
|
-
|
|
760
|
-
### I18n
|
|
761
|
-
|
|
762
|
-
Ransack translation files are available in
|
|
763
|
-
[Ransack::Locale](lib/ransack/locale). You may also be interested in one of the
|
|
764
|
-
many translations for Ransack available at
|
|
765
|
-
http://www.localeapp.com/projects/2999.
|
|
766
|
-
|
|
767
|
-
Predicate and attribute translations in forms may be specified as follows (see
|
|
768
|
-
the translation files in [Ransack::Locale](lib/ransack/locale) for more examples):
|
|
769
|
-
|
|
770
|
-
locales/en.yml:
|
|
771
|
-
```yml
|
|
772
|
-
en:
|
|
773
|
-
ransack:
|
|
774
|
-
asc: ascending
|
|
775
|
-
desc: descending
|
|
776
|
-
predicates:
|
|
777
|
-
cont: contains
|
|
778
|
-
not_cont: not contains
|
|
779
|
-
start: starts with
|
|
780
|
-
end: ends with
|
|
781
|
-
gt: greater than
|
|
782
|
-
lt: less than
|
|
783
|
-
models:
|
|
784
|
-
person: Passanger
|
|
785
|
-
attributes:
|
|
786
|
-
person:
|
|
787
|
-
name: Full Name
|
|
788
|
-
article:
|
|
789
|
-
title: Article Title
|
|
790
|
-
body: Main Content
|
|
791
|
-
```
|
|
792
|
-
|
|
793
|
-
Attribute names may also be changed globally, or under `activerecord`:
|
|
794
|
-
|
|
795
|
-
```yml
|
|
796
|
-
en:
|
|
797
|
-
attributes:
|
|
798
|
-
model_name:
|
|
799
|
-
model_field1: field name1
|
|
800
|
-
model_field2: field name2
|
|
801
|
-
activerecord:
|
|
802
|
-
attributes:
|
|
803
|
-
namespace/article:
|
|
804
|
-
title: AR Namespaced Title
|
|
805
|
-
namespace_article:
|
|
806
|
-
title: Old Ransack Namespaced Title
|
|
27
|
+
```ruby title='Gemfile'
|
|
28
|
+
gem 'ransack'
|
|
807
29
|
```
|
|
808
30
|
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
Ransack works with Mongoid in the same way as Active Record, except that with
|
|
812
|
-
Mongoid, associations are not currently supported. Demo source code may be found
|
|
813
|
-
[here](https://github.com/Zhomart/ransack-mongodb-demo). A `result` method
|
|
814
|
-
called on a `ransack` search returns a `Mongoid::Criteria` object:
|
|
31
|
+
### Bleeding edge
|
|
815
32
|
|
|
816
|
-
|
|
817
|
-
@q = Person.ransack(params[:q])
|
|
818
|
-
@people = @q.result # => Mongoid::Criteria
|
|
33
|
+
If you would like to use the latest updates not yet published to RubyGems, use the `main` branch:
|
|
819
34
|
|
|
820
|
-
|
|
821
|
-
|
|
35
|
+
```ruby title='Gemfile'
|
|
36
|
+
gem 'ransack', :github => 'activerecord-hackery/ransack', :branch => 'main'
|
|
822
37
|
```
|
|
823
38
|
|
|
824
|
-
|
|
825
|
-
both in the same application. If both are present, Ransack will default to
|
|
826
|
-
Active Record only. The logic is contained in
|
|
827
|
-
`Ransack::Adapters#instantiate_object_mapper` should you need to override it.
|
|
828
|
-
|
|
829
|
-
## Semantic Versioning
|
|
39
|
+
### Documentation
|
|
830
40
|
|
|
831
|
-
Ransack
|
|
41
|
+
There is [extensive documentation on Ransack](https://activerecord-hackery.github.io/ransack/), which is a [Docusaurus](https://docusaurus.io/) project and run as a GitHub Pages site.
|
|
832
42
|
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
`y` stands for a minor version (new features that are backward-compatible).
|
|
43
|
+
## Issues tracker
|
|
836
44
|
|
|
837
|
-
|
|
45
|
+
* Before filing an issue, please read the [Contributing Guide](CONTRIBUTING.md).
|
|
46
|
+
* File an issue if a bug is caused by Ransack, is new (has not already been reported), and _can be reproduced from the information you provide_.
|
|
47
|
+
* Please consider adding a branch with a failing spec describing the problem.
|
|
48
|
+
* Contributions are welcome. :smiley:
|
|
49
|
+
* Please do not use the issue tracker for personal support requests. Stack Overflow is a better place for that where a wider community can help you!
|
|
838
50
|
|
|
839
|
-
In other words: `Major.Minor.Patch`.
|
|
840
51
|
|
|
841
52
|
## Contributions
|
|
842
53
|
|
|
843
54
|
To support the project:
|
|
844
55
|
|
|
56
|
+
* Consider supporting us via [Open Collective](https://opencollective.com/ransack/backers/badge.svg)
|
|
845
57
|
* Use Ransack in your apps, and let us know if you encounter anything that's
|
|
846
58
|
broken or missing. A failing spec to demonstrate the issue is awesome. A pull
|
|
847
59
|
request with passing tests is even better!
|
|
@@ -852,3 +64,41 @@ directly related to bug reports, pull requests, or documentation improvements.
|
|
|
852
64
|
* Spread the word on Twitter, Facebook, and elsewhere if Ransack's been useful
|
|
853
65
|
to you. The more people who are using the project, the quicker we can find and
|
|
854
66
|
fix bugs!
|
|
67
|
+
|
|
68
|
+
## Contributors
|
|
69
|
+
|
|
70
|
+
Ransack was created by [Ernie Miller](http://twitter.com/erniemiller) and is developed and maintained by:
|
|
71
|
+
* [Sean Carroll](https://github.com/scarroll32)
|
|
72
|
+
* [Deivid Rodriguez](https://github.com/deivid-rodriguez)
|
|
73
|
+
* [Greg Molnar](https://github.com/gregmolnar)
|
|
74
|
+
* [A great group of contributors](https://github.com/activerecord-hackery/ransack/graphs/contributors).
|
|
75
|
+
- Ransack's logo is designed by [Anıl Kılıç](https://github.com/anilkilic).
|
|
76
|
+
|
|
77
|
+
Alumni Maintainers
|
|
78
|
+
- [Jon Atack](http://twitter.com/jonatack)
|
|
79
|
+
- [Ryan Bigg](http://twitter.com/ryanbigg)
|
|
80
|
+
|
|
81
|
+
This project exists thanks to all the people who contribute. <img src="https://opencollective.com/ransack/contributors.svg?width=890&button=false" />
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
## Backers
|
|
85
|
+
|
|
86
|
+
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/ransack#backer)]
|
|
87
|
+
|
|
88
|
+
<a href="https://opencollective.com/ransack#backers" target="_blank"><img src="https://opencollective.com/ransack/backers.svg?width=890" /></a>
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
## Sponsors
|
|
92
|
+
|
|
93
|
+
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/ransack#sponsor)]
|
|
94
|
+
|
|
95
|
+
<a href="https://opencollective.com/ransack/sponsor/0/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/0/avatar.svg" /></a>
|
|
96
|
+
<a href="https://opencollective.com/ransack/sponsor/1/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/1/avatar.svg" /></a>
|
|
97
|
+
<a href="https://opencollective.com/ransack/sponsor/2/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/2/avatar.svg" /></a>
|
|
98
|
+
<a href="https://opencollective.com/ransack/sponsor/3/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/3/avatar.svg" /></a>
|
|
99
|
+
<a href="https://opencollective.com/ransack/sponsor/4/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/4/avatar.svg" /></a>
|
|
100
|
+
<a href="https://opencollective.com/ransack/sponsor/5/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/5/avatar.svg" /></a>
|
|
101
|
+
<a href="https://opencollective.com/ransack/sponsor/6/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/6/avatar.svg" /></a>
|
|
102
|
+
<a href="https://opencollective.com/ransack/sponsor/7/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/7/avatar.svg" /></a>
|
|
103
|
+
<a href="https://opencollective.com/ransack/sponsor/8/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/8/avatar.svg" /></a>
|
|
104
|
+
<a href="https://opencollective.com/ransack/sponsor/9/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/9/avatar.svg" /></a>
|