ransack-enum 0.3.0 → 1.0.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: e7d29a6d9f1aeb5b49f746c0c16e41b4690ebe4070f90fa5186572927b8ac2ee
4
- data.tar.gz: a2358d2de52e1297aa65ef24c8a3a4529ff484e657716b7777d670483f9608b9
3
+ metadata.gz: f1afbcd408f71081fbc447ce1eaf591e6e902978969e802a0c3bf96448e93d48
4
+ data.tar.gz: 10aa583067d1b8704027191cad180353af7675178baf1e3b28dbaecfd5965306
5
5
  SHA512:
6
- metadata.gz: 64372e91b20d14ccf56ee0567f2aecc9940e2d39c83ca505173a49cea70dc6083fdc6586882c9608e2225d55ac46e8c9009e9e689bb2c5ec6ced8d9934cf7ea1
7
- data.tar.gz: a3d1790361f9886debedc89d336cab5fea1eb4e17a4faa216c76262dcdc32535666b213bf1b1a60e62db9e53635b47f08a6f7977a1324275a8306767991d6708
6
+ metadata.gz: 43734b3d37556813d8a4a5837270342a90ddf7ee3f1d9f0a607846313b415ae57ed58dfd9b86c269e5b10cb571c2e3d825ef625e5cba3320ce2c9bdbe20088ea
7
+ data.tar.gz: 5efb2f770b16591b7ca3e382f845802de16bab83c30bbd8c5f65ffac600bbfeae64bc60935a1d587d12583ef7239a8fa4795677cbf7984308fff773dc7c12e06
data/.gitignore CHANGED
@@ -12,3 +12,5 @@ Gemfile.lock
12
12
 
13
13
  # rspec failure tracking
14
14
  .rspec_status
15
+
16
+ .ruby-version
data/.rubocop.yml CHANGED
@@ -1,5 +1,16 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+
1
6
  AllCops:
2
7
  NewCops: enable
8
+ TargetRubyVersion: 2.6
9
+
3
10
  Metrics/BlockLength:
4
11
  Exclude:
5
12
  - spec/**/*_spec.rb
13
+
14
+ Naming/FileName:
15
+ Exclude:
16
+ - lib/ransack-enum.rb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.0.0 / 2021-12-31
4
+ ### New features
5
+ - Supported version 7 of ActiveRecord.
6
+ ### Incompatible changes
7
+ - Require name of gem has changed from `ransack/enum` to `ransack-enum`.
8
+ - Module of gem has changed from `Ransack::Enum` to `RansackEnum`.
9
+
3
10
  ## 0.3.0 / 2020-09-28
4
11
  ### New features
5
12
  - Support boolean enum value
data/Gemfile CHANGED
@@ -6,5 +6,11 @@ source 'https://rubygems.org'
6
6
  gemspec
7
7
 
8
8
  gem 'rake'
9
- gem 'rspec'
10
- gem 'sqlite3'
9
+ gem 'ransack', require: false
10
+ gem 'rspec', require: false
11
+ gem 'rubocop', require: false
12
+ gem 'rubocop-performance', require: false
13
+ gem 'rubocop-rake', require: false
14
+ gem 'rubocop-rspec', require: false
15
+ gem 'sqlite3', require: false
16
+ gem 'yard', require: false
data/README.md CHANGED
@@ -1,18 +1,21 @@
1
- # Ransack::Enum
1
+ # RansackEnum
2
2
 
3
- Allow Enum values to be used in [Ransack](https://github.com/activerecord-hackery/ransack) searches.
3
+ Enable enum values in [Ransack](https://github.com/activerecord-hackery/ransack) search.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'ransack-enum', '~> 0.3'
10
+ gem 'ransack', '~> 2.0'
11
+ gem 'ransack-enum', '~> 1.0'
11
12
  ```
12
13
 
13
14
  And then execute:
14
15
 
15
- $ bundle
16
+ ```sh
17
+ $ bundle install
18
+ ```
16
19
 
17
20
  ## Usage
18
21
 
@@ -22,6 +25,9 @@ There is no need to add any special code.
22
25
  ```ruby
23
26
  class Post < ActiveRecord::Base
24
27
  enum status: { unpublished: 0, published: 1 }
28
+
29
+ # after ActiveRecord 7, the following is also possible.
30
+ # enum :status, { unpublished: 0, published: 1 }
25
31
  end
26
32
  ```
27
33
 
@@ -55,8 +61,8 @@ Ransack.configure do |config|
55
61
  # ...
56
62
  end
57
63
 
58
- Ransack::Enum.configure do |config|
59
- # enabled
64
+ RansackEnum.configure do |config|
65
+ # enabled (default)
60
66
  config.enabled = true
61
67
 
62
68
  # disabled
@@ -70,7 +76,7 @@ end
70
76
 
71
77
  ## Development
72
78
 
73
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
79
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
80
 
75
81
  To install this gem onto your local machine, run `bundle exec rake install`.
76
82
 
data/Rakefile CHANGED
@@ -1,8 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
4
 
5
+ require 'rspec/core/rake_task'
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
8
- task default: :spec
8
+ require 'rubocop/rake_task'
9
+ RuboCop::RakeTask.new(:lint) do |t|
10
+ t.options = %w[--parallel]
11
+ end
12
+ namespace :lint do
13
+ desc 'Lint fix (Rubocop)'
14
+ task fix: :auto_correct
15
+ end
16
+
17
+ require 'yard'
18
+ desc 'document'
19
+ task :doc do
20
+ YARD::CLI::CommandParser.run
21
+ end
22
+
23
+ task default: %i[lint spec]
data/bin/console CHANGED
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'bundler/setup'
5
- require 'ransack/enum'
5
+ require 'ransack-enum'
6
6
 
7
7
  # You can add fixtures and/or initialization code here to make experimenting
8
8
  # with your gem easier. You can also use a different console, if you like.
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ransack_enum'
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RansackEnum
4
+ module Adapters
5
+ # RansackEnum::Adapters::ActiveRecord6
6
+ module ActiveRecord6
7
+ # @param definitions [Hash]
8
+ # @return [void]
9
+ def enum(definitions)
10
+ ::RansackEnum::Ransacker.new(self, nil, nil, **definitions.to_h.deep_dup).then do |enum_ransacker|
11
+ super.tap { enum_ransacker.call }
12
+ end
13
+ end
14
+ end
15
+
16
+ private_constant :ActiveRecord6
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RansackEnum
4
+ module Adapters
5
+ # RansackEnum::Adapters::ActiveRecord7
6
+ module ActiveRecord7
7
+ # @param name [Symbol, nil]
8
+ # @param values [Hash, nil]
9
+ # @param options [Hash]
10
+ # @return [void]
11
+ def enum(name = nil, values = nil, **options)
12
+ ::RansackEnum::Ransacker.new(self, name, values.deep_dup, **options.deep_dup).then do |enum_ransacker|
13
+ super.tap { enum_ransacker.call }
14
+ end
15
+ end
16
+ end
17
+
18
+ private_constant :ActiveRecord7
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'adapters/active_record_6'
4
+ require_relative 'adapters/active_record_7'
5
+
6
+ module RansackEnum
7
+ # RansackEnum::Adapters
8
+ module Adapters
9
+ ::ActiveSupport.on_load(:active_record) do
10
+ if Integer(::ActiveRecord::VERSION::STRING.split('.').first) >= 7
11
+ extend ActiveRecord7
12
+ else
13
+ extend ActiveRecord6
14
+ end
15
+ end
16
+ end
17
+
18
+ private_constant :Adapters
19
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RansackEnum
4
+ # RansackEnum::Configuration
5
+ class Configuration
6
+ # @return [void]
7
+ def initialize
8
+ @enabled = true
9
+ end
10
+
11
+ # @param enabled [Boolean]
12
+ # @return [void]
13
+ # @raise [ArgumentError]
14
+ def enabled=(enabled)
15
+ raise ArgumentError if !enabled.is_a?(::TrueClass) && !enabled.is_a?(::FalseClass)
16
+
17
+ @enabled = enabled
18
+ end
19
+
20
+ # @return [Boolean]
21
+ def enabled?
22
+ enabled
23
+ end
24
+
25
+ private
26
+
27
+ # @!attribute [r] enabled
28
+ # @return [Boolean]
29
+ attr_reader :enabled
30
+ end
31
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RansackEnum
4
+ # RansackEnum::Formatter
5
+ class Formatter
6
+ # @param model_class [Class]
7
+ # @param name [String]
8
+ # @param values [Array, Hash]
9
+ # @return [void]
10
+ def initialize(model_class, name, values)
11
+ @model_class = model_class
12
+ @name = name
13
+ @normalized_values = normalize_values(values)
14
+ end
15
+
16
+ # @param value [Object]
17
+ # @return [Object]
18
+ def call(value)
19
+ value.is_a?(::Array) ? (value.map { |val| normalize_value(val) }) : normalize_value(value)
20
+ end
21
+
22
+ private
23
+
24
+ # @!attribute [r] model_class
25
+ # @return [Class]
26
+ attr_reader :model_class
27
+ # @!attribute [r] name
28
+ # @return [String]
29
+ attr_reader :name
30
+ # @!attribute [r] normalized_values
31
+ # @return [Hash]
32
+ attr_reader :normalized_values
33
+
34
+ # @param values [Array, Hash]
35
+ # @return [Hash]
36
+ def normalize_values(values)
37
+ (values.is_a?(::Hash) ? values : [values, ::Array.new(values.size, &:itself)].transpose.to_h).symbolize_keys
38
+ end
39
+
40
+ # @param value [Object]
41
+ # @return [Object]
42
+ def normalize_value(value)
43
+ enum_value(value).then { |val| cast_value(val.nil? ? value : val) }
44
+ end
45
+
46
+ # @param value [Object]
47
+ # @return [Object]
48
+ def enum_value(value)
49
+ normalized_values[value.to_sym] if ::RansackEnum.config.enabled? && value.respond_to?(:to_sym)
50
+ end
51
+
52
+ # @param value [Object]
53
+ # @return [Object]
54
+ def cast_value(value)
55
+ ::Ransack::Nodes::Value.new(nil, value).cast(model_class.attribute_types[name]&.type)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RansackEnum
4
+ # RansackEnum::Ransacker
5
+ class Ransacker
6
+ # @param model_class [Class]
7
+ # @param name [Symbol, nil]
8
+ # @param values [Hash, nil]
9
+ # @param options [Hash]
10
+ # @return [void]
11
+ def initialize(model_class, name = nil, values = nil, **options)
12
+ @model_class = model_class
13
+ if name
14
+ @name = name
15
+ @values = values || options
16
+ else
17
+ @name, @values = options.slice!(:_prefix, :_suffix, :_scopes, :_default).to_a.first
18
+ end
19
+ end
20
+
21
+ # @return [void]
22
+ def call
23
+ return if !model_class.respond_to?(:ransacker) || !name || !values
24
+
25
+ model_class.ransacker(name.to_sym, formatter: ::RansackEnum::Formatter.new(model_class, name.to_s, values))
26
+ end
27
+
28
+ private
29
+
30
+ # @!attribute [r] model_class
31
+ # @return [Class]
32
+ attr_reader :model_class
33
+ # @!attribute [r] name
34
+ # @return [Symbol, nil]
35
+ attr_reader :name
36
+ # @!attribute [r] values
37
+ # @return [Hash, nil]
38
+ attr_reader :values
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RansackEnum
4
+ # @return [String]
5
+ VERSION = '1.0.0'
6
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+ require_relative 'ransack_enum/version'
5
+ require_relative 'ransack_enum/configuration'
6
+ require_relative 'ransack_enum/formatter'
7
+ require_relative 'ransack_enum/ransacker'
8
+ require_relative 'ransack_enum/adapters'
9
+
10
+ # RansackEnum
11
+ module RansackEnum
12
+ # RansackEnum::Error
13
+ class Error < StandardError; end
14
+
15
+ class << self
16
+ # @return [RansackEnum::Configuration]
17
+ def config
18
+ @config ||= ::RansackEnum::Configuration.new
19
+ end
20
+
21
+ # @yieldparam config [RansackEnum::Configuration]
22
+ # @yieldreturn [void]
23
+ # @return [void]
24
+ def configure
25
+ raise ::RansackEnum::Error, 'No block is given!' unless block_given?
26
+
27
+ yield config
28
+ end
29
+ end
30
+ end
data/ransack-enum.gemspec CHANGED
@@ -1,18 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'lib/ransack/enum/version'
3
+ require_relative 'lib/ransack_enum/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'ransack-enum'
7
- spec.version = Ransack::Enum::VERSION
7
+ spec.version = RansackEnum::VERSION
8
8
  spec.authors = %w[shoma07]
9
9
  spec.email = %w[23730734+shoma07@users.noreply.github.com]
10
10
 
11
- spec.summary = 'Allow Enum values to be used in Ransack searches'
12
- spec.description = 'Allow Enum values to be used in Ransack searches'
11
+ spec.summary = 'Enable enum values in Ransack search'
12
+ spec.description = 'Enable enum values in Ransack search'
13
13
  spec.homepage = 'https://github.com/shoma07/ransack-enum'
14
14
  spec.license = 'MIT'
15
- spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
16
16
 
17
17
  spec.metadata['homepage_uri'] = spec.homepage
18
18
  spec.metadata['source_code_uri'] = spec.homepage
@@ -26,6 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.bindir = 'exe'
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = %w[lib]
29
-
30
- spec.add_dependency 'ransack', '~> 2.0'
29
+ spec.metadata = { 'rubygems_mfa_required' => 'true' }
31
30
  end
metadata CHANGED
@@ -1,30 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ransack-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shoma07
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-27 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: ransack
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '2.0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '2.0'
27
- description: Allow Enum values to be used in Ransack searches
11
+ date: 2021-12-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Enable enum values in Ransack search
28
14
  email:
29
15
  - 23730734+shoma07@users.noreply.github.com
30
16
  executables: []
@@ -41,20 +27,22 @@ files:
41
27
  - Rakefile
42
28
  - bin/console
43
29
  - bin/setup
44
- - lib/ransack/enum.rb
45
- - lib/ransack/enum/adapters/active_record.rb
46
- - lib/ransack/enum/adapters/active_record/base.rb
47
- - lib/ransack/enum/configuration.rb
48
- - lib/ransack/enum/version.rb
30
+ - lib/ransack-enum.rb
31
+ - lib/ransack_enum.rb
32
+ - lib/ransack_enum/adapters.rb
33
+ - lib/ransack_enum/adapters/active_record_6.rb
34
+ - lib/ransack_enum/adapters/active_record_7.rb
35
+ - lib/ransack_enum/configuration.rb
36
+ - lib/ransack_enum/formatter.rb
37
+ - lib/ransack_enum/ransacker.rb
38
+ - lib/ransack_enum/version.rb
49
39
  - ransack-enum.gemspec
50
40
  homepage: https://github.com/shoma07/ransack-enum
51
41
  licenses:
52
42
  - MIT
53
43
  metadata:
54
- homepage_uri: https://github.com/shoma07/ransack-enum
55
- source_code_uri: https://github.com/shoma07/ransack-enum
56
- changelog_uri: https://github.com/shoma07/ransack-enum/blob/v0.3.0/CHANGELOG.md
57
- post_install_message:
44
+ rubygems_mfa_required: 'true'
45
+ post_install_message:
58
46
  rdoc_options: []
59
47
  require_paths:
60
48
  - lib
@@ -62,15 +50,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
50
  requirements:
63
51
  - - ">="
64
52
  - !ruby/object:Gem::Version
65
- version: 2.3.0
53
+ version: 2.6.0
66
54
  required_rubygems_version: !ruby/object:Gem::Requirement
67
55
  requirements:
68
56
  - - ">="
69
57
  - !ruby/object:Gem::Version
70
58
  version: '0'
71
59
  requirements: []
72
- rubygems_version: 3.1.2
73
- signing_key:
60
+ rubygems_version: 3.2.3
61
+ signing_key:
74
62
  specification_version: 4
75
- summary: Allow Enum values to be used in Ransack searches
63
+ summary: Enable enum values in Ransack search
76
64
  test_files: []
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Ransack
4
- module Enum
5
- module Adapters
6
- module ActiveRecord
7
- # Ransack::Enum::Adapters::ActiveRecord::Base
8
- module Base
9
- # @see https://github.com/rails/rails/blob/66cabeda2c46c582d19738e1318be8d59584cc5b/activerecord/lib/active_record/enum.rb#L150
10
- # @param [Hash] definitions
11
- # @return [Hash]
12
- def enum(definitions)
13
- enum_ransacker(*definitions.to_a.first) if respond_to?(:ransacker)
14
- super
15
- end
16
-
17
- private
18
-
19
- # @param [Symbol] name
20
- # @param [Hash] values
21
- # @return [Hash]
22
- def enum_ransacker(name, values)
23
- converter = enum_ransack_value_converter(name.to_s, values)
24
- ransacker name, formatter: lambda { |value|
25
- value.is_a?(Array) ? value.map(&converter) : converter.call(value)
26
- }
27
- end
28
-
29
- # @param [String] name
30
- # @param [Hash] values
31
- # @return [Proc]
32
- def enum_ransack_value_converter(name, values)
33
- lambda do |value|
34
- val = values[value.to_sym]
35
- return val if Ransack::Enum.enabled? && !val.nil?
36
-
37
- Ransack::Nodes::Value.new(nil, value).cast(attribute_types[name]&.type)
38
- end
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'ransack/enum/adapters/active_record/base'
4
-
5
- ActiveSupport.on_load(:active_record) do
6
- extend Ransack::Enum::Adapters::ActiveRecord::Base
7
- end
@@ -1,27 +0,0 @@
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 = { enabled: true }
10
-
11
- def configure
12
- yield self
13
- end
14
-
15
- # @param [TrueClass, FalseClass] enabled
16
- # @return [TrueClass, FalseClass]
17
- def enabled=(enabled)
18
- options[:enabled] = enabled
19
- end
20
-
21
- # @return [TrueClass, FalseClass]
22
- def enabled?
23
- options[:enabled]
24
- end
25
- end
26
- end
27
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Ransack
4
- module Enum
5
- VERSION = '0.3.0'
6
- end
7
- end
data/lib/ransack/enum.rb DELETED
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support/core_ext'
4
- require 'ransack'
5
- require 'ransack/enum/version'
6
- require 'ransack/enum/configuration'
7
- require 'ransack/enum/adapters/active_record'
8
-
9
- module Ransack
10
- # Ransack::Enum
11
- module Enum
12
- extend Configuration
13
- end
14
- end