sql_enum 0.4.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: b4f885963c1a9b5c4d6612807ea7627f34fc455a730a45ed93fe380bc2a4d0ff
4
- data.tar.gz: 8489d2a5a017117bb1d5a0d5bb88ccab8f11e4be19a915374ee3d12222d45f76
3
+ metadata.gz: 77e67bb36b77c10947ed386e5ee8d88b500e2b9e86e62a5b688747b81ad8a3e5
4
+ data.tar.gz: 99d61ac4776af2d3cf3f7e3c28c7ff8f931aeda99d915a29ae2578495b819b67
5
5
  SHA512:
6
- metadata.gz: 6f4ed451ed9dd0d9f6b0e0d5475efadea5bc1de0d3f19e54bfd2066dfb2685cdb7e9f601bd1ca5b977af17b0df19c89860b5ee2bc5791fbdaf19f2644bbd32de
7
- data.tar.gz: c27592b8fc57952f8237d8dd19c21e50335b7f9a5de2081074a331b5972f7fa4eacbd61745e3da3424086f5f35e64a2df909d866973d96c3268fa758bdd2f36c
6
+ metadata.gz: 930d73715dcdb3ae76416d0600a9fe6ee7b09abaa699a1556cd6c71d88be6e39e4f8413a93c3b009485f2fb0cef63a8a202f2f9c28e2982360f2ae211fe0392d
7
+ data.tar.gz: 67928721dc4507edf4cf3f677041ffeb29f7c01d2f7808998ae6dc404e2b659f3e73ca05066a3b28c571654b58ce334b087c0e610c1e63fc7e9f30371871dfa8
data/.env.local ADDED
@@ -0,0 +1 @@
1
+ DATABASE_URL="mysql2://root:@mysql/"
@@ -10,14 +10,14 @@ jobs:
10
10
  strategy:
11
11
  matrix:
12
12
  ruby:
13
- - '2.7'
14
13
  - '3.0'
15
14
  - '3.1'
16
15
  - '3.2'
16
+ - '3.3'
17
17
  rails:
18
- - '6.0'
19
18
  - '6.1'
20
-
19
+ - '7.0'
20
+ - '7.1'
21
21
  runs-on: ubuntu-latest
22
22
 
23
23
  name: RSpec (Rails ${{ matrix.rails }}) (Ruby ${{ matrix.ruby }})
data/.gitignore CHANGED
@@ -11,3 +11,4 @@
11
11
  .rspec_status
12
12
 
13
13
  gemfiles/*.lock
14
+ .env.local
data/Appraisals CHANGED
@@ -1,12 +1,11 @@
1
- appraise "rails-6.0" do
2
- gem "rails", "~> 6.0.0"
1
+ appraise "rails-6.1" do
2
+ gem "rails", "~> 6.1"
3
3
  end
4
4
 
5
- appraise "rails-6.1" do
6
- gem "rails", "~> 6.1.0"
5
+ appraise "rails-7.0" do
6
+ gem "rails", "~> 7.0"
7
7
  end
8
8
 
9
- # Rails 7.0 doesn't work yet
10
- # appraise "rails-7.0" do
11
- # gem "rails", "~> 7.0"
12
- # end
9
+ appraise "rails-7.1" do
10
+ gem "rails", "~> 7.1"
11
+ end
data/Dockerfile CHANGED
@@ -6,4 +6,5 @@ RUN mkdir -p lib/sql_enum gemfiles
6
6
  COPY lib/sql_enum/version.rb ./lib/sql_enum/
7
7
  COPY gemfiles/*.gemfile gemfiles/
8
8
  COPY sql_enum.gemspec Gemfile Gemfile.lock Appraisals ./
9
- RUN bundle install && exec appraisal install
9
+ RUN bundle install && \
10
+ bundle exec appraisal install
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source "https://gem-proxy.chime.com"
1
+ source "https://rubygems.org"
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in sql_enum.gemspec
6
6
  gemspec
data/Gemfile.lock CHANGED
@@ -1,43 +1,48 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sql_enum (0.4.0)
5
- activerecord (>= 6.0, < 7.0)
6
- activesupport (>= 6.0, < 7.0)
4
+ sql_enum (1.0.0)
5
+ activerecord (>= 6.1.0)
6
+ activesupport (>= 6.1.0)
7
7
  mysql2
8
8
 
9
9
  GEM
10
- remote: https://gem-proxy.chime.com/
10
+ remote: https://rubygems.org/
11
11
  specs:
12
- activemodel (6.1.7.6)
13
- activesupport (= 6.1.7.6)
14
- activerecord (6.1.7.6)
15
- activemodel (= 6.1.7.6)
16
- activesupport (= 6.1.7.6)
17
- activesupport (6.1.7.6)
12
+ activemodel (7.0.8)
13
+ activesupport (= 7.0.8)
14
+ activerecord (7.0.8)
15
+ activemodel (= 7.0.8)
16
+ activesupport (= 7.0.8)
17
+ activesupport (7.0.8)
18
18
  concurrent-ruby (~> 1.0, >= 1.0.2)
19
19
  i18n (>= 1.6, < 2)
20
20
  minitest (>= 5.1)
21
21
  tzinfo (~> 2.0)
22
- zeitwerk (~> 2.3)
23
22
  appraisal (2.5.0)
24
23
  bundler
25
24
  rake
26
25
  thor (>= 0.14.0)
27
- concurrent-ruby (1.2.2)
28
- debug (1.8.0)
29
- irb (>= 1.5.0)
30
- reline (>= 0.3.1)
26
+ awesome_print (1.9.2)
27
+ concurrent-ruby (1.2.3)
28
+ debug (1.9.1)
29
+ irb (~> 1.10)
30
+ reline (>= 0.3.8)
31
31
  diff-lcs (1.5.0)
32
32
  i18n (1.14.1)
33
33
  concurrent-ruby (~> 1.0)
34
- io-console (0.6.0)
35
- irb (1.7.4)
36
- reline (>= 0.3.6)
37
- minitest (5.19.0)
34
+ io-console (0.7.2)
35
+ irb (1.11.1)
36
+ rdoc
37
+ reline (>= 0.4.2)
38
+ minitest (5.21.2)
38
39
  mysql2 (0.5.5)
39
- rake (13.0.6)
40
- reline (0.3.8)
40
+ psych (5.1.2)
41
+ stringio
42
+ rake (13.1.0)
43
+ rdoc (6.6.2)
44
+ psych (>= 4.0.0)
45
+ reline (0.4.2)
41
46
  io-console (~> 0.5)
42
47
  rspec (3.12.0)
43
48
  rspec-core (~> 3.12.0)
@@ -52,10 +57,10 @@ GEM
52
57
  diff-lcs (>= 1.2.0, < 2.0)
53
58
  rspec-support (~> 3.12.0)
54
59
  rspec-support (3.12.1)
55
- thor (1.2.2)
60
+ stringio (3.1.0)
61
+ thor (1.3.0)
56
62
  tzinfo (2.0.6)
57
63
  concurrent-ruby (~> 1.0)
58
- zeitwerk (2.6.11)
59
64
 
60
65
  PLATFORMS
61
66
  ruby
@@ -63,6 +68,7 @@ PLATFORMS
63
68
 
64
69
  DEPENDENCIES
65
70
  appraisal
71
+ awesome_print
66
72
  bundler
67
73
  debug
68
74
  rake (~> 13.0)
@@ -70,4 +76,4 @@ DEPENDENCIES
70
76
  sql_enum!
71
77
 
72
78
  BUNDLED WITH
73
- 2.2.30
79
+ 2.5.5
data/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  Enables usage of native sql enums with ActiveRecord
4
4
 
5
+ ## NOTE
6
+
7
+ Version 1.0 of this is compatible with Rails 7 and above.
8
+
9
+ For Rails versions below Rails 7, use version 0.4
10
+
5
11
  ## Installation
6
12
 
7
13
  Add this line to your application's Gemfile:
data/docker-compose.yml CHANGED
@@ -12,12 +12,13 @@ services:
12
12
 
13
13
  mysql:
14
14
  image: mysql:5.7
15
+ platform: linux/amd64
15
16
  environment:
16
17
  - MYSQL_ALLOW_EMPTY_PASSWORD=yes
17
18
  - MYSQL_DATABASE
18
19
  - MYSQL_USER
19
20
  - MYSQL_PASSWORD
20
21
  healthcheck:
21
- test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
22
+ test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
22
23
  timeout: 20s
23
24
  retries: 10
@@ -1,7 +1,7 @@
1
1
  # This file was generated by Appraisal
2
2
 
3
- source "https://gem-proxy.chime.com"
3
+ source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 6.1.0"
5
+ gem "rails", "~> 6.1"
6
6
 
7
7
  gemspec path: "../"
@@ -1,7 +1,7 @@
1
1
  # This file was generated by Appraisal
2
2
 
3
- source "https://gem-proxy.chime.com"
3
+ source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 7.0.0"
5
+ gem "rails", "~> 7.0"
6
6
 
7
7
  gemspec path: "../"
@@ -1,7 +1,7 @@
1
1
  # This file was generated by Appraisal
2
2
 
3
- source "https://gem-proxy.chime.com"
3
+ source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 5.1.0"
5
+ gem "rails", "~> 7.1"
6
6
 
7
7
  gemspec path: "../"
@@ -1,21 +1,46 @@
1
- # This module fails in Rails 7.0 becuase the method being modified has been
2
- # changed to a class method.
3
-
4
1
  module ActiveRecord
5
2
  module ConnectionAdapters
6
3
  class AbstractMysqlAdapter
7
- def initialize_type_map_with_enum(m = type_map)
8
- initialize_without_enum(m)
9
- register_enum_type(m)
4
+ class << self
5
+ def register_enum_type(mapping)
6
+ mapping.register_type(%r(enum)i) do |sql_type|
7
+ Type::Enum.new(limit: sql_type.to_s.scan(/'(.*?)'/).flatten)
8
+ end
9
+ end
10
10
  end
11
11
 
12
- alias_method :initialize_without_enum, :initialize_type_map
13
- alias_method :initialize_type_map, :initialize_type_map_with_enum
12
+ # In Rails 6.1, registering the enum type is an instance method and is
13
+ # done on initialization, In Rails 7.0 it is a class method and
14
+ # the registration happens when the class is loaded. So, in Rails 6.1,
15
+ # we can override the `initialize_type_map` method to register the enum
16
+ # but in Rails 7.1, we need to call register_enum_type explicitly.
14
17
 
15
- def register_enum_type(mapping)
16
- mapping.register_type(%r(enum)i) do |sql_type|
17
- Type::Enum.new(limit: sql_type.scan(/'(.*?)'/).flatten)
18
+ if SqlEnum.rails_version_match?("6.1")
19
+ module SqlEnumMapper
20
+ def initialize_type_map(m = type_map)
21
+ super(m)
22
+ AbstractMysqlAdapter.register_enum_type(m)
23
+ end
18
24
  end
25
+
26
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend(SqlEnumMapper)
27
+ end
28
+
29
+
30
+ if SqlEnum.rails_version_match?("7.0")
31
+ [
32
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter::TYPE_MAP,
33
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter::TYPE_MAP_WITH_BOOLEAN
34
+ ].each do |m|
35
+ AbstractMysqlAdapter.register_enum_type(m)
36
+ end
37
+ end
38
+
39
+ # Rails 7.1 drops the TYPE_MAP_WITH_BOOLEAN constant
40
+ if SqlEnum.rails_version_match?("7.1")
41
+ AbstractMysqlAdapter.register_enum_type(
42
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter::TYPE_MAP
43
+ )
19
44
  end
20
45
  end
21
46
  end
@@ -1,6 +1,9 @@
1
1
  module SqlEnum
2
2
  module ClassMethods
3
3
  def sql_enum(column_name, options = {})
4
+ # skip redefinitions
5
+ return if defined_enums.key?(column_name.to_s)
6
+
4
7
  # Query values
5
8
  enum_column = EnumColumn.new(table_name, column_name)
6
9
  values_map = enum_column.values.to_h { |value| [value.to_sym, value.to_s] }
@@ -14,20 +17,15 @@ module SqlEnum
14
17
 
15
18
  # Override reader to return symbols
16
19
  type_definition = ->(subtype) { EnumType.new(attr, send(column_name.to_s.pluralize), subtype) }
17
- case method(:decorate_attribute_type).arity
18
- when 2 # Rails 5.1, 5.2, 6.0
19
- decorate_attribute_type(column_name, :enum, &type_definition)
20
- else
21
- decorate_attribute_type(column_name, &type_definition)
22
- end
20
+ attribute(column_name, &type_definition)
23
21
 
24
22
  prefix_str = format_affix(column_name, prefix, suffix: '_')
25
23
  suffix_str = format_affix(column_name, suffix, prefix: '_')
26
24
 
27
25
  # Fix query methods to compare symbols to symbols
28
26
  values_map.each_value do |value|
29
- method_name = "#{prefix_str}#{value}#{suffix_str}"
30
- define_method("#{method_name}?") { self[column_name] == value.to_sym }
27
+ method_name = "#{prefix_str}#{value}#{suffix_str}?"
28
+ define_method(method_name) { self[column_name] == value.to_sym }
31
29
  end
32
30
  end
33
31
 
@@ -1,3 +1,3 @@
1
1
  module SqlEnum
2
- VERSION = "0.4.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/sql_enum.rb CHANGED
@@ -7,6 +7,10 @@ module SqlEnum
7
7
  self.configuration ||= Configuration.new
8
8
  yield(configuration)
9
9
  end
10
+
11
+ def self.rails_version_match?(version_string)
12
+ ActiveSupport.version.to_s.start_with?(version_string)
13
+ end
10
14
  end
11
15
 
12
16
  require 'active_record'
data/sql_enum.gemspec CHANGED
@@ -25,11 +25,12 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ["lib"]
27
27
 
28
- spec.add_dependency "activesupport", ">= 6.0", "< 7.0"
29
- spec.add_dependency "activerecord", ">= 6.0", "< 7.0"
30
28
  spec.add_dependency "mysql2"
29
+ spec.add_dependency "activesupport", ">= 6.1.0"
30
+ spec.add_dependency "activerecord", ">= 6.1.0"
31
31
 
32
32
  spec.add_development_dependency "appraisal"
33
+ spec.add_development_dependency "awesome_print"
33
34
  spec.add_development_dependency "bundler"
34
35
  spec.add_development_dependency "debug"
35
36
  spec.add_development_dependency "rake", "~> 13.0"
metadata CHANGED
@@ -1,63 +1,65 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sql_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fletcher Fowler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-28 00:00:00.000000000 Z
11
+ date: 2024-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activesupport
14
+ name: mysql2
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '6.0'
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '7.0'
19
+ version: '0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: '6.0'
30
- - - "<"
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
- version: '7.0'
33
+ version: 6.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 6.1.0
33
41
  - !ruby/object:Gem::Dependency
34
42
  name: activerecord
35
43
  requirement: !ruby/object:Gem::Requirement
36
44
  requirements:
37
45
  - - ">="
38
46
  - !ruby/object:Gem::Version
39
- version: '6.0'
40
- - - "<"
41
- - !ruby/object:Gem::Version
42
- version: '7.0'
47
+ version: 6.1.0
43
48
  type: :runtime
44
49
  prerelease: false
45
50
  version_requirements: !ruby/object:Gem::Requirement
46
51
  requirements:
47
52
  - - ">="
48
53
  - !ruby/object:Gem::Version
49
- version: '6.0'
50
- - - "<"
51
- - !ruby/object:Gem::Version
52
- version: '7.0'
54
+ version: 6.1.0
53
55
  - !ruby/object:Gem::Dependency
54
- name: mysql2
56
+ name: appraisal
55
57
  requirement: !ruby/object:Gem::Requirement
56
58
  requirements:
57
59
  - - ">="
58
60
  - !ruby/object:Gem::Version
59
61
  version: '0'
60
- type: :runtime
62
+ type: :development
61
63
  prerelease: false
62
64
  version_requirements: !ruby/object:Gem::Requirement
63
65
  requirements:
@@ -65,7 +67,7 @@ dependencies:
65
67
  - !ruby/object:Gem::Version
66
68
  version: '0'
67
69
  - !ruby/object:Gem::Dependency
68
- name: appraisal
70
+ name: awesome_print
69
71
  requirement: !ruby/object:Gem::Requirement
70
72
  requirements:
71
73
  - - ">="
@@ -142,6 +144,7 @@ extensions: []
142
144
  extra_rdoc_files: []
143
145
  files:
144
146
  - ".env"
147
+ - ".env.local"
145
148
  - ".github/workflows/tests.yml"
146
149
  - ".gitignore"
147
150
  - ".rspec"
@@ -157,11 +160,9 @@ files:
157
160
  - bin/console
158
161
  - bin/setup
159
162
  - docker-compose.yml
160
- - gemfiles/rails_5.1.gemfile
161
- - gemfiles/rails_5.2.gemfile
162
- - gemfiles/rails_6.0.gemfile
163
163
  - gemfiles/rails_6.1.gemfile
164
164
  - gemfiles/rails_7.0.gemfile
165
+ - gemfiles/rails_7.1.gemfile
165
166
  - lib/active_record/connection_adapters/abstract_mysql.rb
166
167
  - lib/active_record/connection_adapters/mysql/column_methods.rb
167
168
  - lib/active_record/connection_adapters/mysql2.rb
@@ -195,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
196
  - !ruby/object:Gem::Version
196
197
  version: '0'
197
198
  requirements: []
198
- rubygems_version: 3.1.6
199
+ rubygems_version: 3.5.3
199
200
  signing_key:
200
201
  specification_version: 4
201
202
  summary: Enable using native sql enums
@@ -1,7 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://gem-proxy.chime.com"
4
-
5
- gem "rails", "~> 5.2.0"
6
-
7
- gemspec path: "../"
@@ -1,7 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://gem-proxy.chime.com"
4
-
5
- gem "rails", "~> 6.0.0"
6
-
7
- gemspec path: "../"