ransack-enum 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +22 -2
- data/lib/ransack/enum.rb +7 -0
- data/lib/ransack/enum/adapters/active_record/base.rb +23 -32
- data/lib/ransack/enum/configuration.rb +23 -0
- data/lib/ransack/enum/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6240a0e80bbe84b028e7f40500d0aa9a66dc688d90f49795e634e8b4448a7119
|
4
|
+
data.tar.gz: b88afa56b2dff2b1154d71d2af2315c7e0e6c7d0913ce546a871fff53559b9b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 130d09f69ec1232da41416e141c5b16ab569e1724d3f832beab8ce353ac76300f14850bdfb59952eb6d5ac98d1ca57770659b2e30d4b9d018a46948da516aa67
|
7
|
+
data.tar.gz: 6eaf58d828ef8bfcb4511159c78234e7dc62846bbdb7009e778030804cf7bb1f55726115fa999d20c680ad1f8e04b864db10b915487e233b36d2e6a0948128d7
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Allow Enum values to be used in [Ransack](https://github.com/activerecord-hacker
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'ransack-enum'
|
10
|
+
gem 'ransack-enum', '~> 0.2'
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -43,9 +43,29 @@ Post.ransack(status_in: [0, 1]).result.to_sql
|
|
43
43
|
# SELECT "posts".* FROM "posts" WHERE "posts"."status" IN (0, 1)
|
44
44
|
```
|
45
45
|
|
46
|
+
### Configuration
|
47
|
+
|
48
|
+
Enable / Disable enum value search in the configuration.
|
49
|
+
Default is enabled.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# config/initializers/ransack.rb
|
53
|
+
|
54
|
+
Ransack.configure do |config|
|
55
|
+
# ...
|
56
|
+
end
|
57
|
+
|
58
|
+
Ransack::Enum.configure do |config|
|
59
|
+
# enabled
|
60
|
+
config.enabled = true
|
61
|
+
|
62
|
+
# disabled
|
63
|
+
# config.enabled = false
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
46
67
|
## Feature
|
47
68
|
|
48
|
-
- Enable / Disable in ransack configuration
|
49
69
|
- Enable to convert enum i18n value to search value
|
50
70
|
|
51
71
|
## Development
|
data/lib/ransack/enum.rb
CHANGED
@@ -3,10 +3,17 @@
|
|
3
3
|
require 'active_support/core_ext'
|
4
4
|
require 'ransack/enum/version'
|
5
5
|
require 'ransack/enum/adapters'
|
6
|
+
require 'ransack/enum/configuration'
|
6
7
|
|
7
8
|
module Ransack
|
9
|
+
# Ransack::Enum
|
8
10
|
module Enum
|
11
|
+
extend Configuration
|
9
12
|
class Error < StandardError; end
|
10
13
|
# Your code goes here...
|
11
14
|
end
|
12
15
|
end
|
16
|
+
|
17
|
+
Ransack::Enum.configure do |config|
|
18
|
+
config.enabled = true
|
19
|
+
end
|
@@ -8,44 +8,35 @@ module Ransack
|
|
8
8
|
module Base
|
9
9
|
# @see https://github.com/rails/rails/blob/66cabeda2c46c582d19738e1318be8d59584cc5b/activerecord/lib/active_record/enum.rb#L150
|
10
10
|
# @param [Hash] definitions
|
11
|
+
# @return [Hash]
|
11
12
|
def enum(definitions)
|
12
|
-
super
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
17
|
-
# @param [Hash] definitions
|
18
|
-
def enum_ransacker(definitions)
|
19
|
-
definitions.each do |name, values|
|
20
|
-
fmt = lambda do |v|
|
21
|
-
return values[v.to_sym] if values[v.to_sym]
|
22
|
-
|
23
|
-
case columns_hash[name.to_s]&.type
|
24
|
-
when :integer
|
25
|
-
v.to_i
|
26
|
-
when :boolean
|
27
|
-
if ActiveRecord::VERSION::MAJOR >= 5
|
28
|
-
ActiveRecord::Type::Boolean.new.cast(v)
|
29
|
-
else
|
30
|
-
ActiveRecord::Type::Boolean.new.type_cast_from_database(v)
|
31
|
-
end
|
32
|
-
else
|
33
|
-
v
|
34
|
-
end
|
13
|
+
result = super(definitions)
|
14
|
+
if respond_to?(:ransacker)
|
15
|
+
definitions.each do |name, values|
|
16
|
+
enum_ransacker name, values
|
35
17
|
end
|
18
|
+
end
|
19
|
+
result
|
20
|
+
end
|
36
21
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
22
|
+
# rubocop:disable Metrics/AbcSize
|
23
|
+
# @param [Symbol] name
|
24
|
+
# @param [Hash] values
|
25
|
+
# @return [Hash]
|
26
|
+
def enum_ransacker(name, values)
|
27
|
+
lmd = lambda do |val|
|
28
|
+
v = values[val.to_sym]
|
29
|
+
return v if Ransack::Enum.options[:enabled] && v
|
44
30
|
|
45
|
-
|
31
|
+
type = attribute_types[name.to_s]&.type
|
32
|
+
Ransack::Nodes::Value.new(nil, val).cast(type)
|
46
33
|
end
|
34
|
+
|
35
|
+
ransacker name.to_sym, formatter: proc { |val|
|
36
|
+
val.is_a?(Array) ? val.map { |v| lmd.call(v) } : lmd.call(val)
|
37
|
+
}
|
47
38
|
end
|
48
|
-
# rubocop:enable Metrics/AbcSize
|
39
|
+
# rubocop:enable Metrics/AbcSize
|
49
40
|
end
|
50
41
|
end
|
51
42
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ransack
|
4
|
+
module Enum
|
5
|
+
# Ransack::Enum::Configuration
|
6
|
+
module Configuration
|
7
|
+
mattr_accessor :options
|
8
|
+
|
9
|
+
self.options = {
|
10
|
+
enabled: true
|
11
|
+
}
|
12
|
+
|
13
|
+
def configure
|
14
|
+
yield self
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param [TrueClass, FalseClass] boolean
|
18
|
+
def enabled=(boolean)
|
19
|
+
options[:enabled] = boolean
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/ransack/enum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ransack-enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- shoma07
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ransack
|
@@ -102,6 +102,7 @@ files:
|
|
102
102
|
- lib/ransack/enum/adapters.rb
|
103
103
|
- lib/ransack/enum/adapters/active_record.rb
|
104
104
|
- lib/ransack/enum/adapters/active_record/base.rb
|
105
|
+
- lib/ransack/enum/configuration.rb
|
105
106
|
- lib/ransack/enum/version.rb
|
106
107
|
- ransack-enum.gemspec
|
107
108
|
homepage: https://github.com/shoma07/ransack-enum
|
@@ -110,7 +111,7 @@ licenses:
|
|
110
111
|
metadata:
|
111
112
|
homepage_uri: https://github.com/shoma07/ransack-enum
|
112
113
|
source_code_uri: https://github.com/shoma07/ransack-enum
|
113
|
-
changelog_uri: https://github.com/shoma07/ransack-enum/blob/v0.
|
114
|
+
changelog_uri: https://github.com/shoma07/ransack-enum/blob/v0.2.0/CHANGELOG.md
|
114
115
|
post_install_message:
|
115
116
|
rdoc_options: []
|
116
117
|
require_paths:
|