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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7549f0d615e6836ad8d5c1ecbeb5da477c53cab979f47fe797b577fe7563d3e3
4
- data.tar.gz: 3cf5ba60975e996fc3e2589c3fd4f0de56069c0f0333fa83ef2f207055fa78b0
3
+ metadata.gz: 6240a0e80bbe84b028e7f40500d0aa9a66dc688d90f49795e634e8b4448a7119
4
+ data.tar.gz: b88afa56b2dff2b1154d71d2af2315c7e0e6c7d0913ce546a871fff53559b9b5
5
5
  SHA512:
6
- metadata.gz: 6e079c505e13f75b63896be2d0339bf74e9df8a85d358094067231513202234e1b332fd62fef230099401c6619b74c9757ca45b71a4e5a45f355309378e40672
7
- data.tar.gz: cc9a1e2c3bf288ab36e3e7dd3f26a35d194e9f4a85ea69cd65fc8ac7af02f25846dd3b0c290710e79a189de19e3908988700a6430849235054ec8a04749cbfc8
6
+ metadata.gz: 130d09f69ec1232da41416e141c5b16ab569e1724d3f832beab8ce353ac76300f14850bdfb59952eb6d5ac98d1ca57770659b2e30d4b9d018a46948da516aa67
7
+ data.tar.gz: 6eaf58d828ef8bfcb4511159c78234e7dc62846bbdb7009e778030804cf7bb1f55726115fa999d20c680ad1f8e04b864db10b915487e233b36d2e6a0948128d7
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.2.0 / 2019-12-02
4
+ ### New features
5
+ - Enable / Disable in ransack configuration
6
+
3
7
  ## 0.1.2 / 2019-12-01
4
8
  ### Hotfix
5
9
  - Fix dependencies
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
@@ -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
- enum_ransacker(definitions) if respond_to?(:ransacker)
14
- end
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
- formatter = proc do |val|
38
- if val.is_a?(Array)
39
- val.map { |v| fmt.call(v) }
40
- else
41
- fmt.call(val)
42
- end
43
- end
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
- ransacker name.to_sym, formatter: formatter
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, Metrics/MethodLength
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ransack
4
4
  module Enum
5
- VERSION = '0.1.2'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
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.1.2
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-30 00:00:00.000000000 Z
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.1.2/CHANGELOG.md
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: