activerecord-mysql-enum 0.1.2 → 1.0.0.pre.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 990d55ec62633ed4bc3da5770f0b37fe020b24681b689d2528defa237d3d3411
4
- data.tar.gz: 4500ece77eae411c0d4c51f1f7732258cb2c8e41ce8dc0bc7a0db56de0f491f6
3
+ metadata.gz: 654ebffea2d493ba5392b1aab3f69048c2c38462cf1782d109ee417e46605a89
4
+ data.tar.gz: 28009a9d255721863644711c217633a26b7de8275764508f80bca2ac1a086d7f
5
5
  SHA512:
6
- metadata.gz: e993d90711a95befe09afff1f556e7f3a533cd2668a13164e69bb078601735f621bef907e373415280e6fb3a343b561a4ed479c70bdf3d95ce937ca86029654d
7
- data.tar.gz: a1599a97a54c13f69f12f2c02be06b004d9b613ebe8913e3044eaa7b62a3b2b133c27231d4918508bd9af34388d31a0df8d49c01228444eeda68506302a400f3
6
+ metadata.gz: d68c31a06bb274bfc8599f4c120976e3b5534df6bdb299fee0ba9495e08ef8a4fece362d2da6e0fb0b793a3d0275a393a27dbcf87302804ffbe4e3197e14bd88
7
+ data.tar.gz: d6e78050ab6bdce2a23e8f3479a82eef0dd669cbb357bafd843a550de1781b96d35fc14888f2f4f28ff2a7104a0175b75a88a46130776c0477c29e2cd6dc986f
data/.gitignore CHANGED
@@ -6,3 +6,9 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /log/*.log
10
+ /db/*.sqlite3
11
+ .idea
12
+ gemfiles/*.lock
13
+ coverage/
14
+ spec/dummy/log/*.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require rails_helper
@@ -0,0 +1,29 @@
1
+ ---
2
+ dist: trusty
3
+ language: ruby
4
+ sudo: false
5
+ cache: bundler
6
+ rvm:
7
+ - 2.4.5
8
+ - 2.5.8
9
+ - 2.6.5
10
+ - 2.7.1
11
+ - ruby-head
12
+ gemfile:
13
+ - gemfiles/rails_4.gemfile
14
+ - gemfiles/rails_5.gemfile
15
+ - gemfiles/rails_6.gemfile
16
+ matrix:
17
+ fast_finish: false
18
+ exclude:
19
+ - gemfile: gemfiles/rails_4.gemfile
20
+ rvm: 2.7.1
21
+ - gemfile: gemfiles/rails_5.gemfile
22
+ rvm: 2.4.5
23
+ - gemfile: gemfiles/rails_6.gemfile
24
+ rvm: 2.4.5
25
+ allow_failures:
26
+ - rvm: ruby-head
27
+ script:
28
+ - RAILS_ENV=test bundle exec rake db:create
29
+ - bundle exec rspec
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise 'rails-4' do
4
+ gem 'rails', '~> 4.2'
5
+ end
6
+
7
+ appraise 'rails-5' do
8
+ gem 'rails', '~> 5.2'
9
+ end
10
+
11
+ appraise 'rails-6' do
12
+ gem 'rails', '~> 6.0'
13
+ end
@@ -4,6 +4,32 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4
4
 
5
5
  Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.0.0] - Unreleased
8
+ ### Added
9
+ - A Rspec test suit
10
+ - Dummy Rails app to be used by tests
11
+ - Test coverage reports via Coveralls/SimpleCov
12
+ - A coverage report badge to the README
13
+ - Unit tests being run on push with TravisCI
14
+ - Appraisal to test Rails 4.2, 5.2, and 6.0
15
+ - A dependency on the mysql2 gem
16
+
17
+ ### Changed
18
+ - Existing tests to be run with Rspec
19
+
20
+ ### Removed
21
+ - Support for the mysql gem
22
+ - Support for Rails < 4.2
23
+
24
+ ## [0.1.4] - Unreleased
25
+ ### Fixed
26
+ - Fixed bug in `mysql_adapter` where optional arguments being passed to `type_to_sql` would cause
27
+ an unexpected error
28
+
29
+ ## [0.1.3] - 2020-08-19
30
+ ### Changed
31
+ - refactor mysql adapter
32
+
7
33
  ## [0.1.2] - 2020-08-18
8
34
  ### Changed
9
35
  - fixed frozen string to reassign value instead of append
@@ -19,4 +45,9 @@ Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0
19
45
  ### Changed
20
46
  - Renamed the gem from `enum_column3` to `activerecord-mysql-enum`
21
47
 
22
- [0.1.0]: https://github.com/Invoca/activerecord-mysql-enum/tree/0.1.0
48
+ [1.0.0]: https://github.com/Invoca/activerecord-mysql-enum/compare/v0.1.4...v1.0.0
49
+ [0.1.4]: https://github.com/Invoca/activerecord-mysql-enum/compare/v0.1.3...v0.1.4
50
+ [0.1.3]: https://github.com/Invoca/activerecord-mysql-enum/compare/v0.1.2...v0.1.3
51
+ [0.1.2]: https://github.com/Invoca/activerecord-mysql-enum/compare/v0.1.1...v0.1.2
52
+ [0.1.1]: https://github.com/Invoca/activerecord-mysql-enum/compare/v0.1.0...v0.1.1
53
+ [0.1.0]: https://github.com/Invoca/activerecord-mysql-enum/tree/v0.1.0
data/Gemfile CHANGED
@@ -5,8 +5,11 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in active_table_set.gemspec
6
6
  gemspec
7
7
 
8
- gem 'bundler', '~> 1.8'
8
+ gem 'appraisal'
9
+ gem 'coveralls', require: false
9
10
  gem 'pry'
10
11
  gem 'pry-byebug'
11
12
  gem 'rake', '~> 13.0'
12
13
  gem 'rails', '~> 4.2'
14
+ gem 'rspec'
15
+ gem 'rspec-rails', '~> 3.0'
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activerecord-mysql-enum (0.1.2)
4
+ activerecord-mysql-enum (1.0.0.pre.2)
5
5
  activerecord (>= 4.2, < 7)
6
+ mysql2 (= 0.4.5)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
@@ -41,17 +42,30 @@ GEM
41
42
  minitest (~> 5.1)
42
43
  thread_safe (~> 0.3, >= 0.3.4)
43
44
  tzinfo (~> 1.1)
45
+ appraisal (2.3.0)
46
+ bundler
47
+ rake
48
+ thor (>= 0.14.0)
44
49
  arel (6.0.4)
45
50
  builder (3.2.4)
46
51
  byebug (11.1.3)
47
52
  coderay (1.1.3)
48
53
  concurrent-ruby (1.1.7)
54
+ coveralls (0.8.23)
55
+ json (>= 1.8, < 3)
56
+ simplecov (~> 0.16.1)
57
+ term-ansicolor (~> 1.3)
58
+ thor (>= 0.19.4, < 2.0)
59
+ tins (~> 1.6)
49
60
  crass (1.0.6)
61
+ diff-lcs (1.4.4)
62
+ docile (1.3.2)
50
63
  erubis (2.7.0)
51
64
  globalid (0.4.2)
52
65
  activesupport (>= 4.2.0)
53
66
  i18n (0.9.5)
54
67
  concurrent-ruby (~> 1.0)
68
+ json (2.3.1)
55
69
  loofah (2.6.0)
56
70
  crass (~> 1.0.2)
57
71
  nokogiri (>= 1.5.9)
@@ -61,6 +75,7 @@ GEM
61
75
  mini_mime (1.0.2)
62
76
  mini_portile2 (2.4.0)
63
77
  minitest (5.14.1)
78
+ mysql2 (0.4.5)
64
79
  nokogiri (1.10.10)
65
80
  mini_portile2 (~> 2.4.0)
66
81
  pry (0.13.1)
@@ -97,6 +112,32 @@ GEM
97
112
  rake (>= 0.8.7)
98
113
  thor (>= 0.18.1, < 2.0)
99
114
  rake (13.0.1)
115
+ rspec (3.9.0)
116
+ rspec-core (~> 3.9.0)
117
+ rspec-expectations (~> 3.9.0)
118
+ rspec-mocks (~> 3.9.0)
119
+ rspec-core (3.9.2)
120
+ rspec-support (~> 3.9.3)
121
+ rspec-expectations (3.9.2)
122
+ diff-lcs (>= 1.2.0, < 2.0)
123
+ rspec-support (~> 3.9.0)
124
+ rspec-mocks (3.9.1)
125
+ diff-lcs (>= 1.2.0, < 2.0)
126
+ rspec-support (~> 3.9.0)
127
+ rspec-rails (3.9.1)
128
+ actionpack (>= 3.0)
129
+ activesupport (>= 3.0)
130
+ railties (>= 3.0)
131
+ rspec-core (~> 3.9.0)
132
+ rspec-expectations (~> 3.9.0)
133
+ rspec-mocks (~> 3.9.0)
134
+ rspec-support (~> 3.9.0)
135
+ rspec-support (3.9.3)
136
+ simplecov (0.16.1)
137
+ docile (~> 1.1)
138
+ json (>= 1.8, < 3)
139
+ simplecov-html (~> 0.10.0)
140
+ simplecov-html (0.10.2)
100
141
  sprockets (3.7.2)
101
142
  concurrent-ruby (~> 1.0)
102
143
  rack (> 1, < 3)
@@ -104,8 +145,13 @@ GEM
104
145
  actionpack (>= 4.0)
105
146
  activesupport (>= 4.0)
106
147
  sprockets (>= 3.0.0)
148
+ sync (0.5.0)
149
+ term-ansicolor (1.7.1)
150
+ tins (~> 1.0)
107
151
  thor (1.0.1)
108
152
  thread_safe (0.3.6)
153
+ tins (1.25.0)
154
+ sync
109
155
  tzinfo (1.2.7)
110
156
  thread_safe (~> 0.1)
111
157
 
@@ -114,11 +160,14 @@ PLATFORMS
114
160
 
115
161
  DEPENDENCIES
116
162
  activerecord-mysql-enum!
117
- bundler (~> 1.8)
163
+ appraisal
164
+ coveralls
118
165
  pry
119
166
  pry-byebug
120
167
  rails (~> 4.2)
121
168
  rake (~> 13.0)
169
+ rspec
170
+ rspec-rails (~> 3.0)
122
171
 
123
172
  BUNDLED WITH
124
173
  1.17.3
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # ActiveRecord::Mysql::Enum
1
+ # ActiveRecord::Mysql::Enum [![Coverage Status](https://coveralls.io/repos/github/Invoca/activerecord-mysql-enum/badge.svg?branch=master)](https://coveralls.io/github/Invoca/activerecord-mysql-enum?branch=master)
2
2
 
3
3
  This gem is an extension to ActiveRecord which enables native support of
4
4
  enumerations in the database schema using the ENUM type in MySQL. Forked
@@ -7,17 +7,15 @@ which was itself a fork of a fork of Nick Pohodnya's original gem for
7
7
  Rails 3, [enum_column3](https://github.com/electronick/enum_column).
8
8
 
9
9
  ## Support
10
- Currently this has been manually tested with Rails version 4 and 5, and works with scaffolding.
10
+ Currently this is tested with Rails version 4.2, 5.2, and 6.0.
11
11
 
12
12
  **Supported adapters:**
13
- - mysql
14
13
  - mysql2
15
- - jdbcmysql (by Nilesh Trivedi)
16
14
 
17
15
  ## Installation
18
16
  In your `Gemfile` add the following snippet
19
17
  ```ruby
20
- gem 'activerecord-mysql-enum', '~> 0.1', require: 'active_record/mysql/enum'
18
+ gem 'activerecord-mysql-enum', '~> 1.0', require: 'active_record/mysql/enum'
21
19
  ```
22
20
 
23
21
  ## Usage
@@ -50,17 +48,3 @@ irb(1)> Enumeration.columns_hash['color'].limit
50
48
  irb(2)> @enumeration.column_for_attribute(:color).limit
51
49
  => [:red, :blue, :green, :yellow]
52
50
  ```
53
-
54
- ### Form Helpers
55
- You can use enum_select helper to generate input for enumerated attribute as:
56
- ```ruby
57
- <%= enum_select(@enumeration, 'severity')%>
58
- ```
59
-
60
- Or using `form_for`:
61
- ```ruby
62
- <%= form_for @enumeration do |f| %>
63
- <%= f.label :severity %>
64
- <%= f.enum_select :severity %>
65
- <% end %>
66
- ```
data/Rakefile CHANGED
@@ -1,4 +1,17 @@
1
- #!/usr/bin/env rake
2
- # frozen_string_literal: true
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+ require File.expand_path('../spec/dummy/config/application', __FILE__)
4
+ Rails.application.config.root = "spec/dummy"
3
5
 
4
6
  Bundler::GemHelper.install_tasks
7
+ Rails.application.load_tasks
8
+
9
+ task default: :rspec
10
+
11
+ desc "run rspec unit tests"
12
+ begin
13
+ require 'rspec/core/rake_task'
14
+ RSpec::Core::RakeTask.new(:rspec) do |rspec_task|
15
+ # rspec_task.exclude_pattern = 'spec/e2e/**/*_spec.rb'
16
+ end
17
+ end
@@ -0,0 +1 @@
1
+ {}
@@ -29,4 +29,5 @@ Gem::Specification.new do |spec|
29
29
  spec.require_paths = ["lib"]
30
30
 
31
31
  spec.add_dependency 'activerecord', '>= 4.2', '< 7'
32
+ spec.add_dependency 'mysql2', '0.4.5'
32
33
  end
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "coveralls", require: false
7
+ gem "pry"
8
+ gem "pry-byebug"
9
+ gem "rake", "~> 13.0"
10
+ gem "rails", "~> 4.2"
11
+ gem "rspec"
12
+ gem "rspec-rails", "~> 3.0"
13
+
14
+ gemspec path: "../"
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "coveralls", require: false
7
+ gem "pry"
8
+ gem "pry-byebug"
9
+ gem "rake", "~> 13.0"
10
+ gem "rails", "~> 5.2"
11
+ gem "rspec"
12
+ gem "rspec-rails", "~> 3.0"
13
+
14
+ gemspec path: "../"
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "coveralls", require: false
7
+ gem "pry"
8
+ gem "pry-byebug"
9
+ gem "rake", "~> 13.0"
10
+ gem "rails", "~> 6.0"
11
+ gem "rspec"
12
+ gem "rspec-rails", "~> 3.0"
13
+
14
+ gemspec path: "../"
@@ -9,16 +9,11 @@ if defined?(::Rails::Railtie)
9
9
  ActiveSupport.on_load :active_record do
10
10
  require 'active_record/mysql/enum/mysql_adapter'
11
11
  require 'active_record/mysql/enum/enum_type'
12
- require 'active_record/mysql/enum/enum_adapter'
13
12
  require 'active_record/mysql/enum/enum_column_adapter'
14
13
  require 'active_record/mysql/enum/schema_definitions'
15
14
  require 'active_record/mysql/enum/quoting'
16
15
  require 'active_record/mysql/enum/validations'
17
16
  end
18
-
19
- ActiveSupport.on_load :action_view do
20
- require 'active_record/mysql/enum/active_record_helper'
21
- end
22
17
  end
23
18
  end
24
19
  end
@@ -3,111 +3,58 @@
3
3
  # This module provides all the column helper methods to deal with the
4
4
  # values and adds the common type management code for the adapters.
5
5
 
6
-
7
- # try rails 3.1, then rails 3.2+, mysql column adapters
8
- column_class = if defined? ActiveRecord::ConnectionAdapters::Mysql2Column
9
- ActiveRecord::ConnectionAdapters::Mysql2Column
10
- elsif defined? ActiveRecord::ConnectionAdapters::MysqlColumn
11
- ActiveRecord::ConnectionAdapters::MysqlColumn
12
- elsif defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column
13
- ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column
14
- elsif defined? ActiveRecord::ConnectionAdapters::MysqlAdapter::Column
15
- ActiveRecord::ConnectionAdapters::MysqlAdapter::Column
16
- elsif defined? ActiveRecord::ConnectionAdapters::MySQL::Column
17
- ActiveRecord::ConnectionAdapters::MySQL::Column
18
- end
19
-
20
- if column_class
21
- column_class.class_eval do
22
-
23
- if instance_methods.include?(:extract_default)
24
- alias __extract_default_enum extract_default
25
- def extract_default
26
- if type == :enum
27
- if @default == '' || @default.nil?
28
- @default = nil
6
+ module ActiveRecord
7
+ module Mysql
8
+ module Enum
9
+
10
+ class << self
11
+ def mysql_column_adapter
12
+ if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column
13
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column
14
+ elsif defined? ActiveRecord::ConnectionAdapters::MySQL::Column
15
+ ActiveRecord::ConnectionAdapters::MySQL::Column
29
16
  else
30
- @default = @default.intern
17
+ raise "could not find MySQL::Column or equivalent connection adapter"
31
18
  end
32
19
  end
33
- __extract_default_enum
34
20
  end
35
- end
36
21
 
37
- def __enum_type_cast(value)
38
- if type == :enum
39
- self.class.value_to_symbol(value)
40
- else
41
- __type_cast_enum(value)
42
- end
43
- end
22
+ ActiveRecordColumnWithEnums = Enum.mysql_column_adapter
44
23
 
45
- if instance_methods.include?(:type_cast_from_database)
46
- alias __type_cast_enum type_cast_from_database
47
- # Convert to a symbol.
48
- def type_cast_from_database(value)
49
- __enum_type_cast(value)
50
- end
51
- elsif instance_methods.include?(:type_cast)
52
- alias __type_cast_enum type_cast
53
- def type_cast(value)
54
- __enum_type_cast(value)
55
- end
56
- end
24
+ module EnumColumnAdapter
25
+ def initialize(*)
26
+ super
57
27
 
58
- # Deprecated in Rails 4.1
59
- if instance_methods.include?(:type_cast_code)
60
- alias __type_cast_code_enum type_cast_code
61
- # Code to convert to a symbol.
62
- def type_cast_code(var_name)
63
- if type == :enum
64
- "#{self.class.name}.value_to_symbol(#{var_name})"
65
- else
66
- __type_cast_code_enum(var_name)
28
+ if type == :enum
29
+ @default = if @default.present?
30
+ @default.to_sym
31
+ end
32
+ end
67
33
  end
68
- end
69
- end
70
34
 
71
- class << self
72
- # Safely convert the value to a symbol.
73
- def value_to_symbol(value)
74
- case value
75
- when Symbol
76
- value
77
- when String
78
- value.empty? ? nil : value.intern
79
- else
80
- nil
35
+ # Convert to a symbol.
36
+ def type_cast_from_database(value)
37
+ if type == :enum
38
+ EnumColumnAdapter.value_to_symbol(value)
39
+ else
40
+ super
41
+ end
81
42
  end
82
- end
83
- end
84
-
85
- private
86
43
 
87
- # Deprecated in Rails 4.2
88
- if private_instance_methods.include?(:simplified_type)
89
- alias __simplified_type_enum simplified_type
90
- # The enum simple type.
91
- def simplified_type(field_type)
92
- if field_type =~ /enum/i
93
- :enum
94
- else
95
- __simplified_type_enum(field_type)
44
+ class << self
45
+ # Safely convert the value to a symbol.
46
+ def value_to_symbol(value)
47
+ case value
48
+ when Symbol
49
+ value
50
+ when String
51
+ value.to_sym if value.present?
52
+ end
53
+ end
96
54
  end
97
55
  end
98
- end
99
56
 
100
- # Deprecated in Rails 4.2
101
- if private_instance_methods.include?(:extract_limit)
102
- alias __extract_limit_enum extract_limit
103
- def extract_limit(sql_type)
104
- if sql_type =~ /^enum/i
105
- sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.intern }
106
- else
107
- __extract_limit_enum(sql_type)
108
- end
109
- end
57
+ ActiveRecordColumnWithEnums.prepend EnumColumnAdapter
110
58
  end
111
-
112
59
  end
113
60
  end
@@ -1,18 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- adapter_class = if defined? ActiveRecord::ConnectionAdapters::MySQLJdbcConnection
4
- ActiveRecord::ConnectionAdapters::MySQLJdbcConnection
5
- # elsif defined? ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
6
- # ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
7
- elsif defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
8
- ActiveRecord::ConnectionAdapters::Mysql2Adapter
9
- elsif defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
10
- ActiveRecord::ConnectionAdapters::MysqlAdapter
11
- end
12
-
13
3
  module ActiveRecord
14
4
  module Mysql
15
5
  module Enum
6
+
7
+ class << self
8
+ def mysql_adapter
9
+ defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter or raise "Could not find MySQL connection adapter"
10
+
11
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter
12
+ end
13
+ end
14
+
15
+ ActiveRecordMysqlAdapter = Enum.mysql_adapter
16
+
16
17
  module MysqlAdapter
17
18
  def native_database_types #:nodoc
18
19
  types = super
@@ -28,29 +29,37 @@ module ActiveRecord
28
29
  #
29
30
  # will generate enum('a', 'b', 'c') for :limit => [:a, :b, :c]
30
31
  if Rails::VERSION::MAJOR < 5
31
- def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = nil, **_options) # :nodoc:
32
+ def type_to_sql(type, limit = nil, *args)
32
33
  if type.to_s == 'enum'
33
- native = native_database_types[type]
34
- column_type_sql = (native || {})[:name] || 'enum'
34
+ column_type_sql =
35
+ if (native_database_type = native_database_types[type])
36
+ native_database_type[:name]
37
+ else
38
+ 'enum'
39
+ end
35
40
 
36
- column_type_sql += "(#{limit.map { |v| quote(v) }.join(',')})"
41
+ quoted_values = limit.map { |v| quote(v) }.join(',')
37
42
 
38
- column_type_sql
43
+ "#{column_type_sql}(#{quoted_values})"
39
44
  else
40
- super(type, limit, precision, scale, unsigned)
45
+ super
41
46
  end
42
47
  end
43
48
  else
44
- def type_to_sql(type, limit: nil, precision: nil, scale: nil, unsigned: nil, **_options) # :nodoc:
49
+ def type_to_sql(type, limit: nil, **_options) # :nodoc:
45
50
  if type.to_s == 'enum'
46
- native = native_database_types[type]
47
- column_type_sql = (native || {})[:name] || 'enum'
51
+ column_type_sql =
52
+ if (native_database_type = native_database_types[type])
53
+ native_database_type[:name]
54
+ else
55
+ 'enum'
56
+ end
48
57
 
49
- column_type_sql += "(#{limit.map { |v| quote(v) }.join(',')})"
58
+ quoted_values = limit.map { |v| quote(v) }.join(',')
50
59
 
51
- column_type_sql
60
+ "#{column_type_sql}(#{quoted_values})"
52
61
  else
53
- super(type, limit: limit, precision: precision, scale: scale, unsigned: unsigned)
62
+ super
54
63
  end
55
64
  end
56
65
  end
@@ -67,10 +76,8 @@ module ActiveRecord
67
76
  end
68
77
  end
69
78
  end
79
+
80
+ ActiveRecordMysqlAdapter.prepend ActiveRecord::Mysql::Enum::MysqlAdapter
70
81
  end
71
82
  end
72
83
  end
73
-
74
- if adapter_class
75
- adapter_class.prepend(ActiveRecord::Mysql::Enum::MysqlAdapter)
76
- end
@@ -3,7 +3,7 @@
3
3
  module ActiveRecord
4
4
  module Mysql
5
5
  module Enum
6
- VERSION = "0.1.2"
6
+ VERSION = "1.0.0.pre.2"
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1 @@
1
+ require 'active_record/mysql/enum'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-mysql-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0.pre.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Pohodnya
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-08-18 00:00:00.000000000 Z
12
+ date: 2020-09-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -31,6 +31,20 @@ dependencies:
31
31
  - - "<"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '7'
34
+ - !ruby/object:Gem::Dependency
35
+ name: mysql2
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.4.5
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.5
34
48
  description: Enable enum type for the MySQL Adapter in ActiveRecord
35
49
  email:
36
50
  - development@invoca.com
@@ -41,17 +55,23 @@ files:
41
55
  - ".dependabot/config.yml"
42
56
  - ".github/workflows/gem_release.yml"
43
57
  - ".gitignore"
58
+ - ".rspec"
59
+ - ".travis.yml"
60
+ - Appraisals
44
61
  - CHANGELOG.md
45
62
  - Gemfile
46
63
  - Gemfile.lock
47
64
  - LICENSE
48
65
  - README.md
49
66
  - Rakefile
67
+ - app/assets/config/manifest.js
50
68
  - enum_column.gemspec
69
+ - gemfiles/.bundle/config
70
+ - gemfiles/rails_4.gemfile
71
+ - gemfiles/rails_5.gemfile
72
+ - gemfiles/rails_6.gemfile
51
73
  - init.rb
52
74
  - lib/active_record/mysql/enum.rb
53
- - lib/active_record/mysql/enum/active_record_helper.rb
54
- - lib/active_record/mysql/enum/enum_adapter.rb
55
75
  - lib/active_record/mysql/enum/enum_column_adapter.rb
56
76
  - lib/active_record/mysql/enum/enum_type.rb
57
77
  - lib/active_record/mysql/enum/mysql_adapter.rb
@@ -59,6 +79,7 @@ files:
59
79
  - lib/active_record/mysql/enum/schema_definitions.rb
60
80
  - lib/active_record/mysql/enum/validations.rb
61
81
  - lib/active_record/mysql/enum/version.rb
82
+ - lib/activerecord-mysql-enum.rb
62
83
  - rails/init.rb
63
84
  homepage: http://github.com/Invoca/activerecord-mysql-enum
64
85
  licenses: []
@@ -76,9 +97,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
97
  version: '0'
77
98
  required_rubygems_version: !ruby/object:Gem::Requirement
78
99
  requirements:
79
- - - ">="
100
+ - - ">"
80
101
  - !ruby/object:Gem::Version
81
- version: '0'
102
+ version: 1.3.1
82
103
  requirements: []
83
104
  rubygems_version: 3.0.1
84
105
  signing_key:
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- if defined?(Rails::Generator)
4
- module Rails
5
- module Generator
6
- class GeneratedAttribute
7
- def field_type_with_enumerated_attribute
8
- return (@field_type = :enum_select) if type == :enum
9
- field_type_without_enumerated_attribute
10
- end
11
- alias_method_chain :field_type, :enumerated_attribute
12
- end
13
- end
14
- end
15
- end
16
-
17
- if defined?(ActionView::Base)
18
- module ActionView
19
- module Helpers
20
-
21
- #form_options_helper.rb
22
- module FormOptionsHelper
23
- #def select
24
- def enum_select(object, method, options={}, html_options={})
25
- InstanceTag.new(object, method, self, options.delete(:object)).to_enum_select_tag(options, html_options)
26
- end
27
- end
28
-
29
- class InstanceTag
30
- def to_enum_select_tag(options, html_options={})
31
- if self.object.respond_to?(method_name.to_sym)
32
- column = self.object.column_for_attribute(method_name)
33
- if (value = self.object.__send__(method_name.to_sym))
34
- options[:selected] ||= value.to_s
35
- else
36
- options[:include_blank] = column.null if options[:include_blank].nil?
37
- end
38
- end
39
- to_select_tag(column.limit, options, html_options)
40
- end
41
-
42
- #initialize record_name, method, self
43
- if respond_to?(:to_tag)
44
- def to_tag_with_enumerated_attribute(options={})
45
- #look for an enum
46
- if (column_type == :enum && self.object.class.respond_to?(method_name.to_sym))
47
- to_enum_select_tag(options)
48
- else
49
- to_tag_without_enumerated_attribute(options)
50
- end
51
- end
52
- alias_method_chain :to_tag, :enumerated_attribute
53
- end
54
-
55
- end
56
-
57
- class FormBuilder
58
- def enum_select(method, options={}, html_options={})
59
- @template.enum_select(@object_name, method, objectify_options(options), @default_options.merge(html_options))
60
- end
61
- end
62
-
63
- end
64
- end
65
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # This module provides all the column helper methods to deal with the
4
- # values and adds the common type management code for the adapters.
5
-
6
- adapter_class = if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
7
- ActiveRecord::ConnectionAdapters::Mysql2Adapter
8
- elsif defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
9
- ActiveRecord::ConnectionAdapters::MysqlAdapter
10
- end
11
-
12
- if adapter_class
13
- adapter_class.class_eval do
14
-
15
- protected
16
- if instance_methods.include?(:initialize_type_map)
17
- def initialize_type_map_with_enum_types(m)
18
- initialize_type_map_without_enum_types(m)
19
- m.register_type(%r(enum)i) do |sql_type|
20
- limit = sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.intern }
21
- ActiveRecord::Type::Enum.new(limit: limit)
22
- end
23
- end
24
- alias_method_chain :initialize_type_map, :enum_types
25
- end
26
- end
27
- end