administrate_ransack 0.5.1 → 0.7.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 +31 -3
- data/Rakefile +11 -0
- data/config/locales/en.yml +3 -0
- data/lib/administrate_ransack/searchable.rb +25 -3
- data/lib/administrate_ransack/version.rb +1 -1
- metadata +13 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44c23a183a304146c17d2fc6f5a18763d5a489da01bd97f9a09de0371b0c25b0
|
4
|
+
data.tar.gz: 8d41d39d2145f7a33af00b4ac4d903bc74171680ffb4d437fc53bab8613a612b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 358547f2e396e4dba93e29a4074bce185ea147f54908e40291e8ecf41647acb259aa58678bfbef08d2a6a00d754c6eb5a9ad9dc549bef413b07df2bf72b47b5e
|
7
|
+
data.tar.gz: 81f8a53131731d72240cdddfa3c02db06c13c8cc1c70993d8cbdf380bbc1ac89afc909f557a1dde22b54fab18f26e78f9414690b978a7af68d9b91063481f7b6
|
data/README.md
CHANGED
@@ -2,8 +2,7 @@
|
|
2
2
|
[](https://badge.fury.io/rb/administrate_ransack)
|
3
3
|
[](https://rubygems.org/gems/administrate_ransack)
|
4
4
|
[](https://github.com/blocknotes/administrate_ransack/actions/workflows/linters.yml)
|
5
|
-
[](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs2.yml)
|
5
|
+
[](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs_rails80.yml)
|
7
6
|
|
8
7
|
A plugin for [Administrate](https://github.com/thoughtbot/administrate) to use [Ransack](https://github.com/activerecord-hackery/ransack) for filtering resources.
|
9
8
|
|
@@ -27,6 +26,24 @@ prepend AdministrateRansack::Searchable
|
|
27
26
|
<%= render('administrate_ransack/filters') %>
|
28
27
|
```
|
29
28
|
|
29
|
+
- Update your model (ex. Post) exposing the ransackable attributes and associations, skipping this step will raise an exception that explains in details:
|
30
|
+
|
31
|
+
```rb
|
32
|
+
class Post < ApplicationRecord
|
33
|
+
# ...
|
34
|
+
|
35
|
+
class << self
|
36
|
+
def ransackable_attributes(_auth_object = nil)
|
37
|
+
%w[title description]
|
38
|
+
end
|
39
|
+
|
40
|
+
def ransackable_associations(_auth_object = nil)
|
41
|
+
%w[author]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
30
47
|
- See the Usage section for extra options
|
31
48
|
|
32
49
|
## Usage
|
@@ -99,7 +116,9 @@ end
|
|
99
116
|
|
100
117
|
## Customizations
|
101
118
|
|
102
|
-
- Ransack options can be
|
119
|
+
- Ransack options can be changed defining a method `ransack_options` in the resource controller
|
120
|
+
- The Ransack results by default use _distinct_, to change this behavior it is possible to define a `ransack_result_distinct` method
|
121
|
+
- When a search term is not recognized a flash alert is shown (using a value from locales at `administrate_ransack.errors.invalid_search`), a custom behavior can be apply defining a method `invalid_search_callback`
|
103
122
|
|
104
123
|
```rb
|
105
124
|
module Admin
|
@@ -110,6 +129,15 @@ module Admin
|
|
110
129
|
# raises an exception on unknown parameters
|
111
130
|
{ ignore_unknown_conditions: false }
|
112
131
|
end
|
132
|
+
|
133
|
+
def ransack_result_distinct
|
134
|
+
# disable distinct
|
135
|
+
false
|
136
|
+
end
|
137
|
+
|
138
|
+
def invalid_search_callback(e)
|
139
|
+
raise e
|
140
|
+
end
|
113
141
|
end
|
114
142
|
end
|
115
143
|
```
|
data/Rakefile
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
|
10
|
+
load 'rails/tasks/engine.rake'
|
11
|
+
|
12
|
+
load 'rails/tasks/statistics.rake'
|
13
|
+
|
3
14
|
require 'bundler/gem_tasks'
|
4
15
|
|
5
16
|
begin
|
data/config/locales/en.yml
CHANGED
@@ -5,9 +5,10 @@ require 'ransack'
|
|
5
5
|
module AdministrateRansack
|
6
6
|
module Searchable
|
7
7
|
def scoped_resource
|
8
|
-
options = respond_to?(:ransack_options) ? ransack_options : {}
|
9
|
-
|
10
|
-
@ransack_results
|
8
|
+
options = respond_to?(:ransack_options, true) ? ransack_options : {}
|
9
|
+
distinct = respond_to?(:ransack_result_distinct, true) ? ransack_result_distinct : true
|
10
|
+
@ransack_results = prepare_search(resource_collection: super, query_params: params[:q], options: options)
|
11
|
+
@ransack_results.result(distinct: distinct)
|
11
12
|
end
|
12
13
|
|
13
14
|
# ref => https://github.com/thoughtbot/administrate/blob/v0.18.0/app/helpers/administrate/application_helper.rb#L72-L78
|
@@ -24,5 +25,26 @@ module AdministrateRansack
|
|
24
25
|
base.helper_method :sanitized_order_params
|
25
26
|
end
|
26
27
|
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def prepare_search(resource_collection:, query_params:, options:)
|
32
|
+
resource_collection.ransack(query_params, **options)
|
33
|
+
rescue ArgumentError => e
|
34
|
+
if defined?(Ransack::InvalidSearchError) && e.is_a?(Ransack::InvalidSearchError) # rubocop:disable Style/GuardClause
|
35
|
+
ransack_invalid_search_error(e)
|
36
|
+
resource_collection.ransack({}, **options)
|
37
|
+
else
|
38
|
+
raise e
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def ransack_invalid_search_error(error)
|
43
|
+
if respond_to?(:invalid_search_callback, true)
|
44
|
+
invalid_search_callback(error)
|
45
|
+
else
|
46
|
+
flash.now[:alert] = I18n.t('administrate_ransack.errors.invalid_search', default: error.message)
|
47
|
+
end
|
48
|
+
end
|
27
49
|
end
|
28
50
|
end
|
metadata
CHANGED
@@ -1,43 +1,49 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: administrate_ransack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattia Roccoberton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: administrate
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0.18'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0.18'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: ransack
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '2.3'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '5'
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
38
|
-
- - "
|
41
|
+
- - ">="
|
39
42
|
- !ruby/object:Gem::Version
|
40
43
|
version: '2.3'
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '5'
|
41
47
|
description: A plugin for Administrate to use Ransack for search filters
|
42
48
|
email:
|
43
49
|
- mat@blocknot.es
|
@@ -88,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
94
|
- !ruby/object:Gem::Version
|
89
95
|
version: '0'
|
90
96
|
requirements: []
|
91
|
-
rubygems_version: 3.
|
97
|
+
rubygems_version: 3.3.26
|
92
98
|
signing_key:
|
93
99
|
specification_version: 4
|
94
100
|
summary: Administrate Ransack plugin
|