activerecord-mysql-enum 0.1.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c37c82f5ddedde0545503bdb4c824964037844e1
4
+ data.tar.gz: 70ba4e27acd85d71c6343dabfa6e85de651d4a9d
5
+ SHA512:
6
+ metadata.gz: 0c7b178395cd972e7f48eb34ec889468fd356ade6af3fc234fb7bcc89bdf435fa151b4baac3e5fa655194b5c1fb7ef60218d5c8d22018bee2f60e4ba85ebe184
7
+ data.tar.gz: e85ffe4570a862afb670ae6095f9c1b792a51c9c00783776c8480090b3897edf5d16a1776bc7cdeb658e435ac9c877556c6cd331be414ce30b4111cd14f91828
@@ -0,0 +1,14 @@
1
+ # CHANGELOG for `activerecord-mysql-enum`
2
+
3
+ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4
+
5
+ Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [0.1.0] - Unreleased
8
+ ### Added
9
+ - Backwards compatibility with Rails 4
10
+
11
+ ### Changed
12
+ - Renamed the gem from `enum_column3` to `activerecord-mysql-enum`
13
+
14
+ [0.1.0]: https://github.com/Invoca/activerecord-mysql-enum/tree/0.1.0
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in active_table_set.gemspec
6
+ gemspec
7
+
8
+ gem 'bundler', '~> 1.8'
9
+ gem 'pry'
10
+ gem 'pry-byebug'
11
+ gem 'rake', '~> 13.0'
12
+ gem 'rails', '~> 4.2'
@@ -0,0 +1,124 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-mysql-enum (0.1.0.pre.1)
5
+ activerecord (>= 4.2, < 6)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionmailer (4.2.11.3)
11
+ actionpack (= 4.2.11.3)
12
+ actionview (= 4.2.11.3)
13
+ activejob (= 4.2.11.3)
14
+ mail (~> 2.5, >= 2.5.4)
15
+ rails-dom-testing (~> 1.0, >= 1.0.5)
16
+ actionpack (4.2.11.3)
17
+ actionview (= 4.2.11.3)
18
+ activesupport (= 4.2.11.3)
19
+ rack (~> 1.6)
20
+ rack-test (~> 0.6.2)
21
+ rails-dom-testing (~> 1.0, >= 1.0.5)
22
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
23
+ actionview (4.2.11.3)
24
+ activesupport (= 4.2.11.3)
25
+ builder (~> 3.1)
26
+ erubis (~> 2.7.0)
27
+ rails-dom-testing (~> 1.0, >= 1.0.5)
28
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
29
+ activejob (4.2.11.3)
30
+ activesupport (= 4.2.11.3)
31
+ globalid (>= 0.3.0)
32
+ activemodel (4.2.11.3)
33
+ activesupport (= 4.2.11.3)
34
+ builder (~> 3.1)
35
+ activerecord (4.2.11.3)
36
+ activemodel (= 4.2.11.3)
37
+ activesupport (= 4.2.11.3)
38
+ arel (~> 6.0)
39
+ activesupport (4.2.11.3)
40
+ i18n (~> 0.7)
41
+ minitest (~> 5.1)
42
+ thread_safe (~> 0.3, >= 0.3.4)
43
+ tzinfo (~> 1.1)
44
+ arel (6.0.4)
45
+ builder (3.2.4)
46
+ byebug (11.1.3)
47
+ coderay (1.1.3)
48
+ concurrent-ruby (1.1.7)
49
+ crass (1.0.6)
50
+ erubis (2.7.0)
51
+ globalid (0.4.2)
52
+ activesupport (>= 4.2.0)
53
+ i18n (0.9.5)
54
+ concurrent-ruby (~> 1.0)
55
+ loofah (2.6.0)
56
+ crass (~> 1.0.2)
57
+ nokogiri (>= 1.5.9)
58
+ mail (2.7.1)
59
+ mini_mime (>= 0.1.1)
60
+ method_source (1.0.0)
61
+ mini_mime (1.0.2)
62
+ mini_portile2 (2.4.0)
63
+ minitest (5.14.1)
64
+ nokogiri (1.10.10)
65
+ mini_portile2 (~> 2.4.0)
66
+ pry (0.13.1)
67
+ coderay (~> 1.1)
68
+ method_source (~> 1.0)
69
+ pry-byebug (3.9.0)
70
+ byebug (~> 11.0)
71
+ pry (~> 0.13.0)
72
+ rack (1.6.13)
73
+ rack-test (0.6.3)
74
+ rack (>= 1.0)
75
+ rails (4.2.11.3)
76
+ actionmailer (= 4.2.11.3)
77
+ actionpack (= 4.2.11.3)
78
+ actionview (= 4.2.11.3)
79
+ activejob (= 4.2.11.3)
80
+ activemodel (= 4.2.11.3)
81
+ activerecord (= 4.2.11.3)
82
+ activesupport (= 4.2.11.3)
83
+ bundler (>= 1.3.0, < 2.0)
84
+ railties (= 4.2.11.3)
85
+ sprockets-rails
86
+ rails-deprecated_sanitizer (1.0.3)
87
+ activesupport (>= 4.2.0.alpha)
88
+ rails-dom-testing (1.0.9)
89
+ activesupport (>= 4.2.0, < 5.0)
90
+ nokogiri (~> 1.6)
91
+ rails-deprecated_sanitizer (>= 1.0.1)
92
+ rails-html-sanitizer (1.3.0)
93
+ loofah (~> 2.3)
94
+ railties (4.2.11.3)
95
+ actionpack (= 4.2.11.3)
96
+ activesupport (= 4.2.11.3)
97
+ rake (>= 0.8.7)
98
+ thor (>= 0.18.1, < 2.0)
99
+ rake (13.0.1)
100
+ sprockets (3.7.2)
101
+ concurrent-ruby (~> 1.0)
102
+ rack (> 1, < 3)
103
+ sprockets-rails (3.2.1)
104
+ actionpack (>= 4.0)
105
+ activesupport (>= 4.0)
106
+ sprockets (>= 3.0.0)
107
+ thor (1.0.1)
108
+ thread_safe (0.3.6)
109
+ tzinfo (1.2.7)
110
+ thread_safe (~> 0.1)
111
+
112
+ PLATFORMS
113
+ ruby
114
+
115
+ DEPENDENCIES
116
+ activerecord-mysql-enum!
117
+ bundler (~> 1.8)
118
+ pry
119
+ pry-byebug
120
+ rails (~> 4.2)
121
+ rake (~> 13.0)
122
+
123
+ BUNDLED WITH
124
+ 1.17.3
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-2011 Nick Pohodnya
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # ActiveRecord::Mysql::Enum
2
+
3
+ This gem is an extension to ActiveRecord which enables native support of
4
+ enumerations in the database schema using the ENUM type in MySQL. Forked
5
+ and revitalized from the early work that Nick Pohodnya did with [enum_column3](https://github.com/jewlr/enum_column).
6
+
7
+ ## Support
8
+ Currently this has been manually tested with Rails version 4 and 5, and works with scaffolding.
9
+
10
+ **Supported adapters:**
11
+ - mysql
12
+ - mysql2
13
+ - jdbcmysql (by Nilesh Trivedi)
14
+
15
+ ## Installation
16
+ In your `Gemfile` add the following snippet
17
+ ```ruby
18
+ gem 'activerecord-mysql-enum', '~> 0.1', require: 'active_support/mysql/enum'
19
+ ```
20
+
21
+ ## Usage
22
+ ### Schema Definitions
23
+ When defining an enum in your schema, specify the constraint as a limit:
24
+ ```ruby
25
+ create_table :enumerations, :force => true do |t|
26
+ t.column :severity, :enum, :limit => [:low, :medium, :high, :critical], :default => :medium
27
+ t.column :color, :enum, :limit => [:red, :blue, :green, :yellow]
28
+ end
29
+ ```
30
+
31
+ ### Model Validations
32
+ You can then automatically validate this column using:
33
+ ```ruby
34
+ validates_columns :severity, :color
35
+ ```
36
+
37
+ ### Setting/Getting Values
38
+ All enumerated values will be given as symbols.
39
+ ```ruby
40
+ @e = Enumeration.new
41
+ @e.severity = :medium
42
+ ```
43
+
44
+ You can always use the column reflection to get the list of possible values from the database column.
45
+ ```ruby
46
+ irb(1)> Enumeration.columns_hash['color'].limit
47
+ => [:red, :blue, :green, :yellow]
48
+ irb(2)> @enumeration.column_for_attribute(:color).limit
49
+ => [:red, :blue, :green, :yellow]
50
+ ```
51
+
52
+ ### Form Helpers
53
+ You can use enum_select helper to generate input for enumerated attribute as:
54
+ ```ruby
55
+ <%= enum_select(@enumeration, 'severity')%>
56
+ ```
57
+
58
+ Or using `form_for`:
59
+ ```ruby
60
+ <%= form_for @enumeration do |f| %>
61
+ <%= f.label :severity %>
62
+ <%= f.enum_select :severity %>
63
+ <% end %>
64
+ ```
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env rake
2
+ # frozen_string_literal: true
3
+
4
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "active_record/mysql/enum/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "activerecord-mysql-enum"
9
+ spec.version = ActiveRecord::Mysql::Enum::VERSION
10
+ spec.authors = ["Nick Pohodnya", "Invoca Development"]
11
+ spec.email = ["development@invoca.com"]
12
+
13
+ spec.summary = "Enable enum type for the MySQL Adapter in ActiveRecord"
14
+ spec.description = "Enable enum type for the MySQL Adapter in ActiveRecord"
15
+ spec.homepage = "http://github.com/Invoca/activerecord-mysql-enum"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ spec.metadata = {
20
+ "source_code_uri" => "https://github.com/Invoca/activerecord-mysql-enum",
21
+ 'allowed_push_host' => "https://rubygems.org"
22
+ }
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency 'activerecord', '>= 4.2', '< 6'
32
+ end
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.dirname(__FILE__) + "/rails/init.rb"
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ if defined?(::Rails::Railtie)
4
+ module ActiveRecord
5
+ module Mysql
6
+ module Enum
7
+ class Railtie < Rails::Railtie
8
+ initializer 'active_record-mysql-enum.initialize', :after => 'active_record.initialize_database' do |app|
9
+ ActiveSupport.on_load :active_record do
10
+ require 'active_record/mysql/enum/mysql_adapter'
11
+ require 'active_record/mysql/enum/enum_type'
12
+ require 'active_record/mysql/enum/enum_adapter'
13
+ require 'active_record/mysql/enum/enum_column_adapter'
14
+ require 'active_record/mysql/enum/schema_definitions'
15
+ require 'active_record/mysql/enum/quoting'
16
+ require 'active_record/mysql/enum/validations'
17
+ end
18
+
19
+ ActiveSupport.on_load :action_view do
20
+ require 'active_record/mysql/enum/active_record_helper'
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,65 @@
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
@@ -0,0 +1,27 @@
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
@@ -0,0 +1,113 @@
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
+
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
29
+ else
30
+ @default = @default.intern
31
+ end
32
+ end
33
+ __extract_default_enum
34
+ end
35
+ end
36
+
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
44
+
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
57
+
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)
67
+ end
68
+ end
69
+ end
70
+
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
81
+ end
82
+ end
83
+ end
84
+
85
+ private
86
+
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)
96
+ end
97
+ end
98
+ end
99
+
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
110
+ end
111
+
112
+ end
113
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ if defined? ActiveRecord::Type::Value
4
+ module ActiveRecord
5
+ module Type
6
+ class Enum < Value # :nodoc:
7
+ def type
8
+ :enum
9
+ end
10
+
11
+ def type_cast_for_database(value)
12
+ if value.nil? || value == ''
13
+ nil
14
+ else
15
+ value.to_s
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def cast_value(value)
22
+ if value.nil? || value == ''
23
+ nil
24
+ else
25
+ value.to_sym
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
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
+ module ActiveRecord
14
+ module Mysql
15
+ module Enum
16
+ module MysqlAdapter
17
+ def native_database_types #:nodoc
18
+ types = super
19
+ types[:enum] = { :name => "enum" }
20
+ types
21
+ end
22
+
23
+ # Add enumeration support for schema statement creation. This
24
+ # will have to be adapted for every adapter if the type requires
25
+ # anything by a list of allowed values. The overrides the standard
26
+ # type_to_sql method and chains back to the default. This could
27
+ # be done on a per adapter basis, but is generalized here.
28
+ #
29
+ # will generate enum('a', 'b', 'c') for :limit => [:a, :b, :c]
30
+ if Rails::VERSION::MAJOR < 5
31
+ def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = nil, **) # :nodoc:
32
+ if type.to_s == 'enum'
33
+ native = native_database_types[type]
34
+ column_type_sql = (native || {})[:name] || 'enum'
35
+
36
+ column_type_sql << "(#{limit.map { |v| quote(v) }.join(',')})"
37
+
38
+ column_type_sql
39
+ else
40
+ super(type, limit, precision, scale, unsigned)
41
+ end
42
+ end
43
+ else
44
+ def type_to_sql(type, limit: nil, precision: nil, scale: nil, unsigned: nil, **) # :nodoc:
45
+ if type.to_s == 'enum'
46
+ native = native_database_types[type]
47
+ column_type_sql = (native || {})[:name] || 'enum'
48
+
49
+ column_type_sql << "(#{limit.map { |v| quote(v) }.join(',')})"
50
+
51
+ column_type_sql
52
+ else
53
+ super(type, limit: limit, precision: precision, scale: scale, unsigned: unsigned)
54
+ end
55
+ end
56
+ end
57
+
58
+
59
+ private
60
+ def initialize_type_map(m = type_map)
61
+ super
62
+
63
+ m.register_type(%r(enum)i) do |sql_type|
64
+ limit = sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.intern }
65
+ ActiveRecord::Type::Enum.new(limit: limit)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ if adapter_class
74
+ adapter_class.prepend(ActiveRecord::Mysql::Enum::MysqlAdapter)
75
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters # :nodoc:
5
+ module Quoting
6
+ alias __quote_enum quote
7
+
8
+ # Quote a symbol as a normal string. This will support quoting of
9
+ # enumerated values.
10
+ if Rails::VERSION::MAJOR < 5
11
+ def quote(value, column = nil)
12
+ if !value.is_a? Symbol
13
+ __quote_enum(value, column)
14
+ else
15
+ ActiveRecord::Base.send(:quote_bound_value, value.to_s)
16
+ end
17
+ end
18
+ else
19
+ def quote(value)
20
+ if !value.is_a? Symbol
21
+ __quote_enum(value)
22
+ else
23
+ ActiveRecord::Base.send(:quote_bound_value, value.to_s)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ class TableDefinition
6
+ def enum(*args)
7
+ options = args.extract_options!
8
+ column_names = args
9
+ column_names.each { |name| column(name, 'enum', options) }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Validations
5
+ module ClassMethods
6
+ # Automatically validates the column against the schema definition
7
+ # for nullability, format, and enumerations. Handles integers, floats,
8
+ # enumerations, and string limits.
9
+ #
10
+ # Usage: validates_columns :severity, :name
11
+ def validates_columns(*column_names)
12
+ begin
13
+ cols = columns_hash
14
+ column_names.each do |name|
15
+ col = cols[name.to_s]
16
+ raise ArgumentError, "Cannot find column #{name}" unless col
17
+
18
+ # test for nullability
19
+ validates_presence_of(name) if !col.null
20
+
21
+ # Test various known types.
22
+ case col.type
23
+ when :enum
24
+ validates_inclusion_of name, :in => col.limit, :allow_nil => true
25
+
26
+ when :integer, :float
27
+ validates_numericality_of name, :allow_nil => true
28
+
29
+ when :string
30
+ if col.limit
31
+ validates_length_of name, :maximum => col.limit, :allow_nil => true
32
+ end
33
+ end
34
+ end
35
+ rescue ActiveRecord::StatementInvalid=>e
36
+ raise e unless e.message.include?("42S02") # swallow the exception if its for a missing table
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Mysql
5
+ module Enum
6
+ VERSION = "0.1.0.pre.1"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record/mysql/enum'
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-mysql-enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre.1
5
+ platform: ruby
6
+ authors:
7
+ - Nick Pohodnya
8
+ - Invoca Development
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2020-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '4.2'
21
+ - - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: '6'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: '4.2'
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '6'
34
+ description: Enable enum type for the MySQL Adapter in ActiveRecord
35
+ email:
36
+ - development@invoca.com
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - CHANGELOG.md
42
+ - Gemfile
43
+ - Gemfile.lock
44
+ - LICENSE
45
+ - README.md
46
+ - Rakefile
47
+ - enum_column.gemspec
48
+ - init.rb
49
+ - lib/active_record/mysql/enum.rb
50
+ - lib/active_record/mysql/enum/active_record_helper.rb
51
+ - lib/active_record/mysql/enum/enum_adapter.rb
52
+ - lib/active_record/mysql/enum/enum_column_adapter.rb
53
+ - lib/active_record/mysql/enum/enum_type.rb
54
+ - lib/active_record/mysql/enum/mysql_adapter.rb
55
+ - lib/active_record/mysql/enum/quoting.rb
56
+ - lib/active_record/mysql/enum/schema_definitions.rb
57
+ - lib/active_record/mysql/enum/validations.rb
58
+ - lib/active_record/mysql/enum/version.rb
59
+ - rails/init.rb
60
+ homepage: http://github.com/Invoca/activerecord-mysql-enum
61
+ licenses: []
62
+ metadata:
63
+ source_code_uri: https://github.com/Invoca/activerecord-mysql-enum
64
+ allowed_push_host: https://rubygems.org
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">"
77
+ - !ruby/object:Gem::Version
78
+ version: 1.3.1
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.6.13
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Enable enum type for the MySQL Adapter in ActiveRecord
85
+ test_files: []