administrate_ransack 0.6.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32afd347fc9d0dda302dca3c72f83a214a22f49394fbf987f45800ab1d549a90
4
- data.tar.gz: 7c355e92c9b79610fa9bc8323e0f8231deb22b05ce1204446f5a72a61ff21749
3
+ metadata.gz: 44c23a183a304146c17d2fc6f5a18763d5a489da01bd97f9a09de0371b0c25b0
4
+ data.tar.gz: 8d41d39d2145f7a33af00b4ac4d903bc74171680ffb4d437fc53bab8613a612b
5
5
  SHA512:
6
- metadata.gz: 1e374f0de682af5cfbdfaf26274768016bfa4446d64e1ca47052a20339d81baa0cc62dc0b08542af0e52c0a0ef1987b080ee015e2ed09ad5d09fecb6815a4671
7
- data.tar.gz: b42a18d64406cbf2e91594c5f7f740508cdc0fb3ed641e0216c551f2a7d2b1467154613dfc9be744faa8863e529b61194ccffe9b28794c5cac6acb07878d44b4
6
+ metadata.gz: 358547f2e396e4dba93e29a4074bce185ea147f54908e40291e8ecf41647acb259aa58678bfbef08d2a6a00d754c6eb5a9ad9dc549bef413b07df2bf72b47b5e
7
+ data.tar.gz: 81f8a53131731d72240cdddfa3c02db06c13c8cc1c70993d8cbdf380bbc1ac89afc909f557a1dde22b54fab18f26e78f9414690b978a7af68d9b91063481f7b6
data/README.md CHANGED
@@ -2,8 +2,7 @@
2
2
  [![gem version](https://badge.fury.io/rb/administrate_ransack.svg)](https://badge.fury.io/rb/administrate_ransack)
3
3
  [![gem downloads](https://badgen.net/rubygems/dt/administrate_ransack)](https://rubygems.org/gems/administrate_ransack)
4
4
  [![linters](https://github.com/blocknotes/administrate_ransack/actions/workflows/linters.yml/badge.svg)](https://github.com/blocknotes/administrate_ransack/actions/workflows/linters.yml)
5
- [![specs](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs.yml/badge.svg)](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs.yml)
6
- [![specs Rails7](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs2.yml/badge.svg)](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs2.yml)
5
+ [![Specs Rails 8.0](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs_rails80.yml/badge.svg)](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
 
@@ -117,7 +116,9 @@ end
117
116
 
118
117
  ## Customizations
119
118
 
120
- - Ransack options can be customized defining a `ransack_options` method in the controller, example:
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`
121
122
 
122
123
  ```rb
123
124
  module Admin
@@ -128,6 +129,15 @@ module Admin
128
129
  # raises an exception on unknown parameters
129
130
  { ignore_unknown_conditions: false }
130
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
131
141
  end
132
142
  end
133
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
@@ -1,5 +1,8 @@
1
1
  en:
2
2
  administrate_ransack:
3
+ ## Optional error key
4
+ # errors:
5
+ # invalid_search: Invalid search term!
3
6
  filters:
4
7
  'clear_filters': Clear filters
5
8
  'no': 'No'
@@ -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
- @ransack_results = super.ransack(params[:q], **options)
10
- @ransack_results.result(distinct: true)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdministrateRansack
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: administrate_ransack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.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: 2024-07-07 00:00:00.000000000 Z
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
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  - !ruby/object:Gem::Version
95
95
  version: '0'
96
96
  requirements: []
97
- rubygems_version: 3.4.19
97
+ rubygems_version: 3.3.26
98
98
  signing_key:
99
99
  specification_version: 4
100
100
  summary: Administrate Ransack plugin