ransack-enum 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 97f30e49879d033fe4c565d88b0684425231f15f587e8aec3c172733f93933c1
4
+ data.tar.gz: ca2b830be92aa094cff2582b3bba3564a39c13a00470c220f2fe74be470491d6
5
+ SHA512:
6
+ metadata.gz: 8c66bafa64fe674569f78b7221ecd2090d311423e69fe937623aff056134536650bed8283a724ff5cb952e4793578772b330085750b72bf331dd2746f21ecb26
7
+ data.tar.gz: f516648b499dbab637d630b237501fc1a357f8d0994e507dcdd7f7ebb49f83b52307580bff7908675ad0c860fcde7ae8ed779337b3105139785ba97735cd7fea
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor
10
+ .DS_Store
11
+ Gemfile.lock
12
+ .rspec
13
+
14
+ # rspec failure tracking
15
+ .rspec_status
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6.5
3
+ Exclude:
4
+ - Gemfile
5
+ - Gemfile.lock
6
+ - ransack-enum.gemspec
7
+ - vendor/bundle/**/*
8
+ - vendor/bundler/**/*
9
+ Metrics/BlockLength:
10
+ Exclude:
11
+ - spec/**/*
12
+ Metrics/LineLength:
13
+ Max: 80
14
+ IgnoredPatterns: ['(\A|\s+)#']
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.1.0 / 2019-11-30
4
+ ### New features
5
+ - create gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ransack-enum.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 shoma07
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ # Ransack::Enum
2
+
3
+ Allow Enum values to be used in [Ransack](https://github.com/activerecord-hackery/ransack) searches.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ransack-enum',
11
+ git: 'https://github.com/shoma07/ransack-enum.git',
12
+ branch: 'master'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Usage
20
+
21
+ When enum is defined in ActiveRecord, the search method of ransack is redefined.
22
+ There is no need to add any special code.
23
+
24
+ ```ruby
25
+ class Post < ActiveRecord::Base
26
+ enum status: { unpublished: 0, published: 1 }
27
+ end
28
+ ```
29
+
30
+ ### Search by enum value.
31
+
32
+ ```ruby
33
+ Post.ransack(status_eq: 'published').result.to_sql
34
+ # SELECT "posts".* FROM "posts" WHERE "posts"."status" = 1
35
+ Post.ransack(status_in: %w[unpublished published]).result.to_sql
36
+ # SELECT "posts".* FROM "posts" WHERE "posts"."status" IN (0, 1)
37
+ ```
38
+
39
+ ### Search with actual values as before.
40
+
41
+ ```ruby
42
+ Post.ransack(status_eq: 1).result.to_sql
43
+ # SELECT "posts".* FROM "posts" WHERE "posts"."status" = 1
44
+ Post.ransack(status_in: [0, 1]).result.to_sql
45
+ # SELECT "posts".* FROM "posts" WHERE "posts"."status" IN (0, 1)
46
+ ```
47
+
48
+ ## Feature
49
+
50
+ - Enable / Disable in ransack configuration
51
+ - Enable to convert enum i18n value to search value
52
+
53
+ ## Development
54
+
55
+ 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.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shoma07/ransack-enum.
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ransack/enum'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext'
4
+ require 'ransack/enum/version'
5
+ require 'ransack/enum/adapters'
6
+
7
+ module Ransack
8
+ module Enum
9
+ class Error < StandardError; end
10
+ # Your code goes here...
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ransack/enum/adapters/active_record'
4
+
5
+ module Ransack
6
+ module Enum
7
+ # Ransack::Enum::Adapters
8
+ module Adapters
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ransack/enum/adapters/active_record/base'
4
+
5
+ module Ransack
6
+ module Enum
7
+ module Adapters
8
+ # Ransack::Enum::Adapters::ActiveRecord
9
+ module ActiveRecord
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ ActiveSupport.on_load(:active_record) do
16
+ extend Ransack::Enum::Adapters::ActiveRecord::Base
17
+ end
@@ -0,0 +1,53 @@
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
+ def enum(definitions)
12
+ super
13
+ enum_ransacker(definitions)
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
35
+ end
36
+
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
44
+
45
+ ransacker name.to_sym, formatter: formatter
46
+ end
47
+ end
48
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ransack
4
+ module Enum
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "ransack/enum/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ransack-enum"
7
+ spec.version = Ransack::Enum::VERSION
8
+ spec.authors = ["shoma07"]
9
+ spec.email = ["23730734+shoma07@users.noreply.github.com"]
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"
13
+ spec.homepage = "https://github.com/shoma07/ransack-enum"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "https://github.com/shoma07/ransack-enum/blob/master/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "ransack", "~> 2.3"
30
+ spec.add_dependency "activesupport", ">= 5.2.1"
31
+
32
+ spec.add_development_dependency "bundler", "~> 2.0"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "sqlite3", "~> 1.4"
36
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ransack-enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - shoma07
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-30 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.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 5.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 5.2.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.4'
97
+ description: Allow Enum values to be used in Ransack searches
98
+ email:
99
+ - 23730734+shoma07@users.noreply.github.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".rubocop.yml"
107
+ - ".travis.yml"
108
+ - CHANGELOG.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/ransack/enum.rb
116
+ - lib/ransack/enum/adapters.rb
117
+ - lib/ransack/enum/adapters/active_record.rb
118
+ - lib/ransack/enum/adapters/active_record/base.rb
119
+ - lib/ransack/enum/version.rb
120
+ - ransack-enum.gemspec
121
+ homepage: https://github.com/shoma07/ransack-enum
122
+ licenses:
123
+ - MIT
124
+ metadata:
125
+ homepage_uri: https://github.com/shoma07/ransack-enum
126
+ source_code_uri: https://github.com/shoma07/ransack-enum
127
+ changelog_uri: https://github.com/shoma07/ransack-enum/blob/master/CHANGELOG.md
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubygems_version: 3.0.3
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Allow Enum values to be used in Ransack searches
147
+ test_files: []