ransack 4.1.1 → 4.4.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 +4 -4
- data/README.md +5 -3
- data/lib/polyamorous/activerecord/join_association_7_2.rb +55 -0
- data/lib/polyamorous/polyamorous.rb +5 -1
- data/lib/ransack/adapters/active_record/context.rb +32 -5
- data/lib/ransack/constants.rb +1 -1
- data/lib/ransack/context.rb +7 -4
- data/lib/ransack/helpers/form_builder.rb +6 -7
- data/lib/ransack/helpers/form_helper.rb +86 -20
- data/lib/ransack/invalid_search_error.rb +3 -0
- data/lib/ransack/locale/ja.yml +51 -51
- data/lib/ransack/locale/ko.yml +70 -0
- data/lib/ransack/locale/uk.yml +72 -0
- data/lib/ransack/nodes/condition.rb +39 -7
- data/lib/ransack/nodes/grouping.rb +1 -1
- data/lib/ransack/nodes/sort.rb +1 -1
- data/lib/ransack/nodes/value.rb +9 -1
- data/lib/ransack/search.rb +4 -3
- data/lib/ransack/version.rb +1 -1
- data/lib/ransack.rb +9 -0
- data/spec/polyamorous/join_association_spec.rb +0 -1
- data/spec/polyamorous/join_dependency_spec.rb +0 -1
- data/spec/ransack/adapters/active_record/base_spec.rb +106 -3
- data/spec/ransack/adapters/active_record/context_spec.rb +72 -0
- data/spec/ransack/helpers/form_builder_spec.rb +0 -2
- data/spec/ransack/helpers/form_helper_spec.rb +219 -5
- data/spec/ransack/nodes/condition_spec.rb +230 -0
- data/spec/ransack/nodes/grouping_spec.rb +2 -2
- data/spec/ransack/nodes/value_spec.rb +12 -1
- data/spec/ransack/predicate_spec.rb +16 -9
- data/spec/ransack/search_spec.rb +121 -1
- data/spec/ransack/translate_spec.rb +0 -1
- data/spec/spec_helper.rb +2 -3
- data/spec/support/schema.rb +42 -0
- metadata +17 -86
- data/.github/FUNDING.yml +0 -3
- data/.github/SECURITY.md +0 -12
- data/.github/workflows/codeql.yml +0 -72
- data/.github/workflows/cronjob.yml +0 -99
- data/.github/workflows/deploy.yml +0 -35
- data/.github/workflows/rubocop.yml +0 -20
- data/.github/workflows/test-deploy.yml +0 -29
- data/.github/workflows/test.yml +0 -131
- data/.gitignore +0 -7
- data/.nojekyll +0 -0
- data/.rubocop.yml +0 -50
- data/CHANGELOG.md +0 -1176
- data/CONTRIBUTING.md +0 -171
- data/Gemfile +0 -53
- data/Rakefile +0 -24
- data/bug_report_templates/test-ransack-scope-and-column-same-name.rb +0 -78
- data/bug_report_templates/test-ransacker-arel-present-predicate.rb +0 -75
- data/docs/.gitignore +0 -19
- data/docs/.nojekyll +0 -0
- data/docs/babel.config.js +0 -3
- data/docs/blog/2022-03-27-ransack-3.0.0.md +0 -20
- data/docs/docs/getting-started/_category_.json +0 -4
- data/docs/docs/getting-started/advanced-mode.md +0 -46
- data/docs/docs/getting-started/configuration.md +0 -47
- data/docs/docs/getting-started/search-matches.md +0 -67
- data/docs/docs/getting-started/simple-mode.md +0 -288
- data/docs/docs/getting-started/sorting.md +0 -71
- data/docs/docs/getting-started/using-predicates.md +0 -282
- data/docs/docs/going-further/_category_.json +0 -4
- data/docs/docs/going-further/acts-as-taggable-on.md +0 -114
- data/docs/docs/going-further/associations.md +0 -70
- data/docs/docs/going-further/custom-predicates.md +0 -52
- data/docs/docs/going-further/documentation.md +0 -43
- data/docs/docs/going-further/exporting-to-csv.md +0 -49
- data/docs/docs/going-further/external-guides.md +0 -57
- data/docs/docs/going-further/form-customisation.md +0 -63
- data/docs/docs/going-further/i18n.md +0 -53
- data/docs/docs/going-further/img/create_release.png +0 -0
- data/docs/docs/going-further/merging-searches.md +0 -41
- data/docs/docs/going-further/other-notes.md +0 -428
- data/docs/docs/going-further/polymorphic-search.md +0 -46
- data/docs/docs/going-further/ransackers.md +0 -331
- data/docs/docs/going-further/release_process.md +0 -36
- data/docs/docs/going-further/saving-queries.md +0 -82
- data/docs/docs/going-further/searching-postgres.md +0 -57
- data/docs/docs/going-further/wiki-contributors.md +0 -82
- data/docs/docs/intro.md +0 -99
- data/docs/docusaurus.config.js +0 -120
- data/docs/package.json +0 -42
- data/docs/sidebars.js +0 -31
- data/docs/src/components/HomepageFeatures/index.js +0 -64
- data/docs/src/components/HomepageFeatures/styles.module.css +0 -11
- data/docs/src/css/custom.css +0 -39
- data/docs/src/pages/index.module.css +0 -23
- data/docs/src/pages/markdown-page.md +0 -7
- 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 +0 -1
- 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 +0 -171
- data/docs/static/img/undraw_docusaurus_react.svg +0 -170
- data/docs/static/img/undraw_docusaurus_tree.svg +0 -40
- data/docs/static/logo/ransack-h.png +0 -0
- data/docs/static/logo/ransack-h.svg +0 -34
- data/docs/static/logo/ransack-v.png +0 -0
- data/docs/static/logo/ransack-v.svg +0 -34
- data/docs/static/logo/ransack.png +0 -0
- data/docs/static/logo/ransack.svg +0 -21
- data/docs/yarn.lock +0 -8879
- data/ransack.gemspec +0 -26
@@ -1,114 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Acts-as-taggable-on
|
3
|
-
sidebar_position: 13
|
4
|
-
---
|
5
|
-
|
6
|
-
## Using Acts As Taggable On
|
7
|
-
|
8
|
-
If you have an `ActiveRecord` model and you're using [acts-as-taggable-on](https://github.com/mbleigh/acts-as-taggable-on),
|
9
|
-
chances are you might want to search on tagged fields. Follow the instructions to install the gem and then set up your project files.
|
10
|
-
|
11
|
-
### Configure the model
|
12
|
-
|
13
|
-
`app/models/tasks.rb`
|
14
|
-
|
15
|
-
You can call the tagging field anything you like, it just needs to be plural. No migration is needed as this is stored in the internal ActsAsTaggable tables (`tags` and `taggings`).
|
16
|
-
|
17
|
-
```ruby
|
18
|
-
class Task < ApplicationRecord
|
19
|
-
acts_as_taggable_on :projects
|
20
|
-
end
|
21
|
-
```
|
22
|
-
|
23
|
-
### Controller
|
24
|
-
|
25
|
-
Add a field to strong params in the controller. Use the singular name with `_list`.
|
26
|
-
|
27
|
-
`app/controllers/tasks_controller.rb`
|
28
|
-
|
29
|
-
```ruby
|
30
|
-
def strong_params
|
31
|
-
params
|
32
|
-
.require(:tasks)
|
33
|
-
.permit(:task, :example_field, :project_list)
|
34
|
-
```
|
35
|
-
|
36
|
-
### Form
|
37
|
-
|
38
|
-
We need to `send` the tag fieldname to our model, also using the singular naming.
|
39
|
-
|
40
|
-
```erb
|
41
|
-
<div class='form-group'>
|
42
|
-
<%= f.label :project_list %>
|
43
|
-
<%= f.text_field :project_list, value: @task.send(:project_list).to_s %>
|
44
|
-
</div>
|
45
|
-
```
|
46
|
-
|
47
|
-
Now we can collect our data via the form, with tags separated by commas.
|
48
|
-
|
49
|
-
## Ransack Search
|
50
|
-
|
51
|
-
Imagine you have the following two instances of `Task`:
|
52
|
-
|
53
|
-
```ruby
|
54
|
-
{ id: 1, name: 'Clean up my room', projects: [ 'Home', 'Personal' ] }
|
55
|
-
{ id: 2, name: 'Complete math exercises', projects: [ 'Homework', 'Study' ] }
|
56
|
-
```
|
57
|
-
|
58
|
-
When you're writing a `Ransack` search form, you can choose any of the following options:
|
59
|
-
|
60
|
-
```erb
|
61
|
-
<%= search_form_for @search do |f| %>
|
62
|
-
<%= f.text_field :projects_name_in %> <!-- option a -->
|
63
|
-
<%= f.text_field :projects_name_eq %> <!-- option b -->
|
64
|
-
<%= f.text_field :projects_name_cont %> <!-- option c -->
|
65
|
-
<% end %>
|
66
|
-
```
|
67
|
-
|
68
|
-
### Option A - Match keys exactly
|
69
|
-
|
70
|
-
Option `A` will match keys exactly. This is the solution to choose if you want to distinguish 'Home' from 'Homework': searching for 'Home' will return just the `Task` with id 1. It also allows searching for more than one tag at once (comma separated):
|
71
|
-
- `Home, Personal` will return task 1
|
72
|
-
- `Home, Homework` will return task 1 and 2
|
73
|
-
|
74
|
-
### Option B - match key combinations
|
75
|
-
|
76
|
-
Option `B` will match all keys exactly. This is the solution if you wanna search for specific combinations of tags:
|
77
|
-
- `Home` will return nothing, as there is no Task with just the `Home` tag
|
78
|
-
- `Home, Personal` will return task 1
|
79
|
-
|
80
|
-
### Option C - match substrings
|
81
|
-
|
82
|
-
Option `C` is used to match substrings. This is useful when you don't care for the exact tag, but only for part of it:
|
83
|
-
- `Home` will return task 1 and 2 (`/Home/` matches both `"Home"` and `"Homework"`)
|
84
|
-
|
85
|
-
### Option D - select from a list of tags
|
86
|
-
|
87
|
-
In Option `D` we allow the user to select a list of valid tags and then search against them. We use the plural name here.
|
88
|
-
|
89
|
-
```erb
|
90
|
-
<div class='form-group'>
|
91
|
-
<%= f.label :projects_name, 'Project' %>
|
92
|
-
<%= f.select :projects_name_in, ActsAsTaggableOn::Tag.distinct.order(:name).pluck(:name) %>
|
93
|
-
</div>
|
94
|
-
```
|
95
|
-
|
96
|
-
## Multitenancy
|
97
|
-
|
98
|
-
ActsAsTaggableOn allows scoping of tags based on another field on the model. Suppose we have a `language` field on the model, as an effective second level key. We would adjust our model to look like this:
|
99
|
-
|
100
|
-
```ruby
|
101
|
-
class Task < ApplicationRecord
|
102
|
-
acts_as_taggable_on :projects
|
103
|
-
acts_as_taggable_tenant :language
|
104
|
-
end
|
105
|
-
```
|
106
|
-
|
107
|
-
The Ransack search is then filtered using the `for_tenant` method
|
108
|
-
|
109
|
-
```erb
|
110
|
-
<div class='form-group'>
|
111
|
-
<%= f.label :projects_name, 'Project' %>
|
112
|
-
<%= f.select :projects_name_in, ActsAsTaggableOn::Tag.for_tenant('fr').distinct.order(:name).pluck(:name) %>
|
113
|
-
</div>
|
114
|
-
|
@@ -1,70 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 1
|
3
|
-
title: Associations
|
4
|
-
---
|
5
|
-
|
6
|
-
### Associations
|
7
|
-
|
8
|
-
You can easily use Ransack to search for objects in `has_many` and `belongs_to`
|
9
|
-
associations.
|
10
|
-
|
11
|
-
Given these associations...
|
12
|
-
|
13
|
-
```ruby
|
14
|
-
class Employee < ActiveRecord::Base
|
15
|
-
belongs_to :supervisor
|
16
|
-
|
17
|
-
# has attributes first_name:string and last_name:string
|
18
|
-
end
|
19
|
-
|
20
|
-
class Department < ActiveRecord::Base
|
21
|
-
has_many :supervisors
|
22
|
-
|
23
|
-
# has attribute title:string
|
24
|
-
end
|
25
|
-
|
26
|
-
class Supervisor < ActiveRecord::Base
|
27
|
-
belongs_to :department
|
28
|
-
has_many :employees
|
29
|
-
|
30
|
-
# has attribute last_name:string
|
31
|
-
end
|
32
|
-
```
|
33
|
-
|
34
|
-
... and a controller...
|
35
|
-
|
36
|
-
```ruby
|
37
|
-
class SupervisorsController < ApplicationController
|
38
|
-
def index
|
39
|
-
@q = Supervisor.ransack(params[:q])
|
40
|
-
@supervisors = @q.result.includes(:department, :employees)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
```
|
44
|
-
|
45
|
-
... you might set up your form like this...
|
46
|
-
|
47
|
-
```erb
|
48
|
-
<%= search_form_for @q do |f| %>
|
49
|
-
<%= f.label :last_name_cont %>
|
50
|
-
<%= f.search_field :last_name_cont %>
|
51
|
-
|
52
|
-
<%= f.label :department_title_cont %>
|
53
|
-
<%= f.search_field :department_title_cont %>
|
54
|
-
|
55
|
-
<%= f.label :employees_first_name_or_employees_last_name_cont %>
|
56
|
-
<%= f.search_field :employees_first_name_or_employees_last_name_cont %>
|
57
|
-
|
58
|
-
<%= f.submit "search" %>
|
59
|
-
<% end %>
|
60
|
-
...
|
61
|
-
<%= content_tag :table do %>
|
62
|
-
<%= content_tag :th, sort_link(@q, :last_name) %>
|
63
|
-
<%= content_tag :th, sort_link(@q, :department_title) %>
|
64
|
-
<%= content_tag :th, sort_link(@q, :employees_last_name) %>
|
65
|
-
<% end %>
|
66
|
-
```
|
67
|
-
|
68
|
-
If you have trouble sorting on associations, try using an SQL string with the
|
69
|
-
pluralized table (`'departments.title'`,`'employees.last_name'`) instead of the
|
70
|
-
symbolized association (`:department_title)`, `:employees_last_name`).
|
@@ -1,52 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 1
|
3
|
-
title: Custom predicates
|
4
|
-
---
|
5
|
-
|
6
|
-
If you'd like to add your own custom Ransack predicates:
|
7
|
-
|
8
|
-
```ruby
|
9
|
-
# config/initializers/ransack.rb
|
10
|
-
|
11
|
-
Ransack.configure do |config|
|
12
|
-
config.add_predicate 'equals_diddly', # Name your predicate
|
13
|
-
# What non-compound ARel predicate will it use? (eq, matches, etc)
|
14
|
-
arel_predicate: 'eq',
|
15
|
-
# Format incoming values as you see fit. (Default: Don't do formatting)
|
16
|
-
formatter: proc { |v| "#{v}-diddly" },
|
17
|
-
# Validate a value. An "invalid" value won't be used in a search.
|
18
|
-
# Below is default.
|
19
|
-
validator: proc { |v| v.present? },
|
20
|
-
# Should compounds be created? Will use the compound (any/all) version
|
21
|
-
# of the arel_predicate to create a corresponding any/all version of
|
22
|
-
# your predicate. (Default: true)
|
23
|
-
compounds: true,
|
24
|
-
# Force a specific column type for type-casting of supplied values.
|
25
|
-
# (Default: use type from DB column)
|
26
|
-
type: :string,
|
27
|
-
# Use LOWER(column on database).
|
28
|
-
# (Default: false)
|
29
|
-
case_insensitive: true
|
30
|
-
end
|
31
|
-
```
|
32
|
-
You can check all Arel predicates [here](https://github.com/rails/rails/blob/main/activerecord/lib/arel/predications.rb).
|
33
|
-
|
34
|
-
If Arel does not have the predicate you are looking for, consider monkey patching it:
|
35
|
-
|
36
|
-
```ruby
|
37
|
-
# config/initializers/ransack.rb
|
38
|
-
|
39
|
-
module Arel
|
40
|
-
module Predications
|
41
|
-
def gteq_or_null(other)
|
42
|
-
left = gteq(other)
|
43
|
-
right = eq(nil)
|
44
|
-
left.or(right)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
Ransack.configure do |config|
|
50
|
-
config.add_predicate 'gteq_or_null', arel_predicate: 'gteq_or_null'
|
51
|
-
end
|
52
|
-
```
|
@@ -1,43 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 11
|
3
|
-
title: Documentation
|
4
|
-
---
|
5
|
-
|
6
|
-
Ransack uses [Docusaurus](https://docusaurus.io/) for documentation. To contribute to the docs simply use the "Edit this page" link from any page to directly edit, or else pull the repo and edit locally.
|
7
|
-
|
8
|
-
### Local Development
|
9
|
-
|
10
|
-
Switch to docs folder
|
11
|
-
|
12
|
-
```
|
13
|
-
cd docs
|
14
|
-
```
|
15
|
-
|
16
|
-
Install docusaurus and other dependencies
|
17
|
-
|
18
|
-
```
|
19
|
-
yarn install
|
20
|
-
```
|
21
|
-
|
22
|
-
|
23
|
-
Start a local development server and open up a browser window. Most changes are reflected live without having to restart the server.
|
24
|
-
|
25
|
-
```
|
26
|
-
yarn start
|
27
|
-
```
|
28
|
-
|
29
|
-
### Build
|
30
|
-
|
31
|
-
```
|
32
|
-
yarn build
|
33
|
-
```
|
34
|
-
|
35
|
-
This command generates static content into the `build` directory and can be served using any static contents hosting service.
|
36
|
-
|
37
|
-
### Deployment
|
38
|
-
|
39
|
-
Using SSH:
|
40
|
-
|
41
|
-
```
|
42
|
-
USE_SSH=true yarn deploy
|
43
|
-
```
|
@@ -1,49 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 2
|
3
|
-
title: CSV Export
|
4
|
-
---
|
5
|
-
|
6
|
-
Exporting to CSV
|
7
|
-
|
8
|
-
Example downloading a csv file preserving ransack search, based on [this gist](https://gist.github.com/pama/adff25ed1f4b796ce088ea362a08e1c5)
|
9
|
-
|
10
|
-
```ruby title='index.html.erb'
|
11
|
-
<h1>Users</h1>
|
12
|
-
|
13
|
-
<%= search_form_for @q, url: dashboard_index_path do |f| %>
|
14
|
-
<%= f.label :name_cont %>
|
15
|
-
<%= f.search_field :name_cont %>
|
16
|
-
|
17
|
-
<%= f.submit %>
|
18
|
-
<% end %>
|
19
|
-
|
20
|
-
<ul>
|
21
|
-
<% @users.each do |user| %>
|
22
|
-
<li><%= user.name %> [<%= user.devices.map {|device| device.name }.join(', ') %>]</li>
|
23
|
-
<% end %>
|
24
|
-
</ul>
|
25
|
-
|
26
|
-
<% if params[:q] %>
|
27
|
-
<%= link_to 'Export 1', dashboard_index_path({name: params[:q][:name_cont]}.merge({format: :csv})) %>
|
28
|
-
<% else %>
|
29
|
-
<%= link_to 'Export 2', dashboard_index_path(format: 'csv') %>
|
30
|
-
<% end %>
|
31
|
-
```
|
32
|
-
|
33
|
-
```ruby title='user.rb'
|
34
|
-
require 'csv'
|
35
|
-
|
36
|
-
class User < ApplicationRecord
|
37
|
-
has_many :devices
|
38
|
-
|
39
|
-
def self.get_csv(users)
|
40
|
-
CSV.generate do |csv|
|
41
|
-
csv << ["Name", "Devices"]
|
42
|
-
|
43
|
-
users.each do |user|
|
44
|
-
csv << [user.name, user.devices.map{|device| device.name}.join(', ')]
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
```
|
@@ -1,57 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 9
|
3
|
-
title: External resources
|
4
|
-
---
|
5
|
-
|
6
|
-
There is a plethora of material on Ransack around the internet. We've collected some here for your convenience.
|
7
|
-
|
8
|
-
Do you want to have a blog post or other content on Ransack highlighted? Please just edit the page, add your content and a Pull Request will be sent to Ransack maintainers for approval.
|
9
|
-
|
10
|
-
# Screencasts
|
11
|
-
|
12
|
-
- [DriftingRuby: Ransack Search and Hotwire](https://www.driftingruby.com/episodes/ransack-search-and-hotwire)
|
13
|
-
- [GoRails: Forum Series Part 6: Search with Ransack](https://gorails.com/episodes/forum-search-with-ransack)
|
14
|
-
- [Railscast 370 - Ransack](http://railscasts.com/episodes/370-ransack)
|
15
|
-
- [Search And Sort Ransack Associations With The Rails Ransack Gem | Ruby On Rails 6 Ransack Tutorial](https://www.youtube.com/watch?v=rtg-5EXwpbg)
|
16
|
-
|
17
|
-
|
18
|
-
# Gems
|
19
|
-
|
20
|
-
- [ActiveAdmin](https://activeadmin.info/) The Administration Framework for Rails **_uses Ransack internally_**
|
21
|
-
- [Ransack Memory](https://github.com/richardrails/ransack_memory) Automatically save and load Ransack's filtered params into the Rail's session
|
22
|
-
- [Mobility Ransack](https://github.com/shioyama/mobility-ransack) Search attributes translated by Mobility with Ransack.
|
23
|
-
- [Ransack UI](https://github.com/ndbroadbent/ransack_ui) Framework for building a search UI with Ransack **_seems abandoned_**
|
24
|
-
|
25
|
-
# Blogs
|
26
|
-
|
27
|
-
- [Search And Sort In Ruby On Rails 6 With The Ransack Gem](https://deanin.com/blog/ransack/)
|
28
|
-
- [Implement Ransack Gem in Ruby on Rails](https://www.botreetechnologies.com/blog/implementing-advanced-search-in-ruby-on-rails-with-ransack/)
|
29
|
-
- [Searching and Sorting with Ransack](https://jaspercurry.medium.com/searching-and-sorting-on-rails-with-ransack-560e862e650a)
|
30
|
-
- [How to Build Your Own ActiveAdmin Filters with Ransack](https://www.viget.com/articles/how-to-build-your-own-filters-with-ransack/)
|
31
|
-
- [Avoid Ransack's N+1 Pitfall!](https://dev.to/husteadrobert/avoid-ransacks-n1-pitfall-33of)
|
32
|
-
- [Filter and paging with Kaminari](https://gist.github.com/MyklClason/e4dc96fd0e009b7b3a9c84ddbb1e11d2)
|
33
|
-
- [Pagination for Ransack Forms](https://nicholaide.github.io/ransack/2016/11/26/ransack-pagination.html)
|
34
|
-
- [AJAX Search, Sort, Paginate with Ransack and Kaminari](https://techbrownbags.wordpress.com/2014/01/17/rails-ajax-search-sort-paginate-with-ransack-kaminari/)
|
35
|
-
- [Searching with Ransack in Ruby on Rails](http://blog.magmalabs.io/2019/03/12/searching-with-ransack-in-ruby-on-rails.html)
|
36
|
-
- [Role scopes with gem Ransack](https://blog.corsego.com/rolify-scopes)
|
37
|
-
- [Searching and Sorting with Ransack](https://www.mintbit.com/blog/searching-and-sorting-with-ransack)
|
38
|
-
- [Using custom scopes with Ransack gem in Rails](https://profilehunt.net/blog/using-custom-scopes-with-ransack-in-rails)
|
39
|
-
- [Query Date Range With Ransack](https://lingceng.github.io/blog/2015/12/28/query-date-range-with-ransack/)
|
40
|
-
- [ransack vs searchkick: Building a search feature in Rails](https://www.cookieshq.co.uk/posts/ransack-vs-searchkick-building-a-search-feature-in-rails)
|
41
|
-
- [Using ransack and delegate in Rails](https://huangwenwei.com/blogs/using-ransack-and-delegate-in-rails)
|
42
|
-
- [Using Ransack as a Search Engine](https://medium.com/@jelaniwoods/using-ransack-as-a-search-engine-92e002a68da)
|
43
|
-
- [Advanced Search with Ransack](https://www.sitepoint.com/advanced-search-ransack/)
|
44
|
-
- [Sort a table of records in Rails with Ransack](https://alankydd.wordpress.com/2012/03/12/sort-a-table-of-records-in-rails-with-ransack/)
|
45
|
-
- [Ransack: Search with Multiple Checkboxes (Rails)](https://iamjosh.wordpress.com/2014/03/07/ransack-search-with-multiple-checkboxes/)
|
46
|
-
- [Rails : Ransack : Sorting data by ratings](https://cbabhusal.wordpress.com/2017/01/03/rails-ransack-sorting-data-by-ratings/)
|
47
|
-
- [Setting Up Rails 5 API Only App with ActiveAdmin enabled](https://rrott.com/blog/ror/rails-5-api-with-activeadmin-integration/)
|
48
|
-
- [Ransack, the library formerly known as MetaSearch 2.0](https://ernie.io/2011/04/01/ransack-the-library-formerly-known-as-metasearch-2-0/) **_some Ransack history_**
|
49
|
-
|
50
|
-
## In French
|
51
|
-
|
52
|
-
- [Faciliter les recherches avec Ransack](https://www.synbioz.com/blog/tech/faciliter-les-recherches-avec-ransack)
|
53
|
-
|
54
|
-
## In Vietnamese
|
55
|
-
|
56
|
-
- [Ransack - công cụ tuyệt vời giúp tìm kiếm và sắp xếp dữ liệu đơn giản hơn
|
57
|
-
](https://nddblog.com/posts/ransack-cong-cu-tuyet-voi-giup-tim-kiem-va-sap-xep-du-lieu-don-gian-hon)
|
@@ -1,63 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 4
|
3
|
-
title: Form customisation
|
4
|
-
---
|
5
|
-
|
6
|
-
Predicate and attribute labels in forms may be specified with I18n in a translation file (see the locale files in [Ransack::Locale](https://github.com/activerecord-hackery/ransack/tree/main/lib/ransack/locale) for more examples):
|
7
|
-
|
8
|
-
```yml
|
9
|
-
# locales/en.yml
|
10
|
-
en:
|
11
|
-
ransack:
|
12
|
-
asc: ascending
|
13
|
-
desc: descending
|
14
|
-
predicates:
|
15
|
-
cont: contains
|
16
|
-
not_cont: not contains
|
17
|
-
start: starts with
|
18
|
-
end: ends with
|
19
|
-
gt: greater than
|
20
|
-
lt: less than
|
21
|
-
attributes:
|
22
|
-
person:
|
23
|
-
name: Full Name
|
24
|
-
article:
|
25
|
-
title: Article Title
|
26
|
-
body: Main Content
|
27
|
-
```
|
28
|
-
The names of attribute fields may also be changed globally or under activerecord:
|
29
|
-
|
30
|
-
```yml
|
31
|
-
# locales/en.yml
|
32
|
-
en:
|
33
|
-
attributes:
|
34
|
-
model_name:
|
35
|
-
model_field1: field name1
|
36
|
-
model_field2: field name2
|
37
|
-
activerecord:
|
38
|
-
attributes:
|
39
|
-
namespace/article:
|
40
|
-
title: AR Namespaced Title
|
41
|
-
namespace_article:
|
42
|
-
title: Old Ransack Namespaced Title
|
43
|
-
```
|
44
|
-
|
45
|
-
To limit the predicates in the `predicate_select` form helper in a view template, pass an array of permitted predicates with `only`:
|
46
|
-
|
47
|
-
```erb
|
48
|
-
<%= f.predicate_select only: %i(cont not_cont eq not_eq blank null) %>
|
49
|
-
```
|
50
|
-
|
51
|
-
Compound predicates (`_any` & `_all`) may be removed by passing the option `compounds: false`.
|
52
|
-
|
53
|
-
```erb
|
54
|
-
<%= f.predicate_select compounds: false %>
|
55
|
-
```
|
56
|
-
|
57
|
-
Searchable attributes versus non-searchable ones may be specified as follows:
|
58
|
-
|
59
|
-
```ruby
|
60
|
-
def self.ransackable_attributes(auth_object = nil)
|
61
|
-
%w(searchable_attribute_1 searchable_attribute_2 ...) + _ransackers.keys
|
62
|
-
end
|
63
|
-
```
|
@@ -1,53 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 3
|
3
|
-
title: i18n
|
4
|
-
---
|
5
|
-
|
6
|
-
# i18n and Ransack
|
7
|
-
|
8
|
-
Ransack translation files are available in
|
9
|
-
[Ransack::Locale](https://github.com/activerecord-hackery/ransack/tree/main/lib/ransack/locale). You may also be interested in one of the
|
10
|
-
many translations for Ransack available at
|
11
|
-
http://www.localeapp.com/projects/2999.
|
12
|
-
|
13
|
-
Predicate and attribute translations in forms may be specified as follows (see
|
14
|
-
the translation files in [Ransack::Locale](https://github.com/activerecord-hackery/ransack/tree/main/lib/ransack/locale) for more examples):
|
15
|
-
|
16
|
-
locales/en.yml:
|
17
|
-
```yml
|
18
|
-
en:
|
19
|
-
ransack:
|
20
|
-
asc: ascending
|
21
|
-
desc: descending
|
22
|
-
predicates:
|
23
|
-
cont: contains
|
24
|
-
not_cont: not contains
|
25
|
-
start: starts with
|
26
|
-
end: ends with
|
27
|
-
gt: greater than
|
28
|
-
lt: less than
|
29
|
-
models:
|
30
|
-
person: Passenger
|
31
|
-
attributes:
|
32
|
-
person:
|
33
|
-
name: Full Name
|
34
|
-
article:
|
35
|
-
title: Article Title
|
36
|
-
body: Main Content
|
37
|
-
```
|
38
|
-
|
39
|
-
Attribute names may also be changed globally, or under `activerecord`:
|
40
|
-
|
41
|
-
```yml
|
42
|
-
en:
|
43
|
-
attributes:
|
44
|
-
model_name:
|
45
|
-
model_field1: field name1
|
46
|
-
model_field2: field name2
|
47
|
-
activerecord:
|
48
|
-
attributes:
|
49
|
-
namespace/article:
|
50
|
-
title: AR Namespaced Title
|
51
|
-
namespace_article:
|
52
|
-
title: Old Ransack Namespaced Title
|
53
|
-
```
|
Binary file
|
@@ -1,41 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 5
|
3
|
-
title: Merging searches
|
4
|
-
---
|
5
|
-
|
6
|
-
To find records that match multiple searches, it's possible to merge all the ransack search conditions into an ActiveRecord relation to perform a single query. In order to avoid conflicts between joined table names it's necessary to set up a shared context to track table aliases used across all the conditions before initializing the searches:
|
7
|
-
|
8
|
-
```ruby
|
9
|
-
shared_context = Ransack::Context.for(Person)
|
10
|
-
|
11
|
-
search_parents = Person.ransack(
|
12
|
-
{ parent_name_eq: "A" }, context: shared_context
|
13
|
-
)
|
14
|
-
|
15
|
-
search_children = Person.ransack(
|
16
|
-
{ children_name_eq: "B" }, context: shared_context
|
17
|
-
)
|
18
|
-
|
19
|
-
shared_conditions = [search_parents, search_children].map { |search|
|
20
|
-
Ransack::Visitor.new.accept(search.base)
|
21
|
-
}
|
22
|
-
|
23
|
-
Person.joins(shared_context.join_sources)
|
24
|
-
.where(shared_conditions.reduce(&:or))
|
25
|
-
.to_sql
|
26
|
-
```
|
27
|
-
Produces:
|
28
|
-
```sql
|
29
|
-
SELECT "people".*
|
30
|
-
FROM "people"
|
31
|
-
LEFT OUTER JOIN "people" "parents_people"
|
32
|
-
ON "parents_people"."id" = "people"."parent_id"
|
33
|
-
LEFT OUTER JOIN "people" "children_people"
|
34
|
-
ON "children_people"."parent_id" = "people"."id"
|
35
|
-
WHERE (
|
36
|
-
("parents_people"."name" = 'A' OR "children_people"."name" = 'B')
|
37
|
-
)
|
38
|
-
ORDER BY "people"."id" DESC
|
39
|
-
```
|
40
|
-
|
41
|
-
Admittedly this is not as simple as it should be, but it's workable for now. (Implementing [issue 417](https://github.com/activerecord-hackery/ransack/issues/417) could make this more straightforward.)
|