From 56e7af0057abb351cca12106854f040d5f4884c3 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Fri, 14 Aug 2020 18:26:30 +0200 Subject: [PATCH 01/15] initial rename of the gem to activerecord-mysql-enum --- enum_column.gemspec | 50 ++++++++++++++++++------- lib/active_record/mysql/enum/version.rb | 9 +++++ 2 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 lib/active_record/mysql/enum/version.rb diff --git a/enum_column.gemspec b/enum_column.gemspec index 772ce95..edebc07 100644 --- a/enum_column.gemspec +++ b/enum_column.gemspec @@ -1,17 +1,39 @@ -# -*- encoding: utf-8 -*- -# stub: enum_column3 0.1.4 ruby lib +# frozen_string_literal: true -Gem::Specification.new do |s| - s.name = "enum_column3" - s.version = "5.1.1" +lib = File.expand_path("../lib", __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require "active_record/mysql/enum/version" - s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.require_paths = ["lib"] - s.authors = ["Nick Pohodnya"] - s.date = "2015-08-19" - s.files = ["LICENSE", "README.txt", "init.rb", "lib/enum/active_record_helper.rb", "lib/enum/enum_adapter.rb", "lib/enum/mysql_adapter.rb", "lib/enum/quoting.rb", "lib/enum/schema_definitions.rb", "lib/enum/schema_statements.rb", "lib/enum/validations.rb", "lib/enum_column.rb", "lib/enum_column3.rb", "test/db/schema.rb", "test/enum_controller_test.rb", "test/enum_mysql_test.rb", "test/fixtures/enum_controller.rb", "test/fixtures/enumeration.rb", "test/test_helper.rb"] - s.homepage = "http://github.com/electronick/enum_column" - s.rubygems_version = "2.4.8" - s.summary = "Enable enum type for MySQL db." - s.test_files = ["test/test_helper.rb", "test/db/schema.rb", "test/fixtures/enumeration.rb", "test/fixtures/enum_controller.rb", "test/enum_controller_test.rb", "test/enum_mysql_test.rb"] +Gem::Specification.new do |spec| + spec.name = "activerecord-mysql-enum" + spec.version = ActiveRecord::Mysql::Enum::VERSION + spec.authors = ["Nick Pohodnya", "Invoca Development"] + spec.email = ["development@invoca.com"] + + spec.summary = "Enable enum type for the MySQL Adapter in ActiveRecord" + spec.description = "Enable enum type for the MySQL Adapter in ActiveRecord" + spec.homepage = "http://github.com/Invoca/activerecord-mysql-enum" + + # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' + # to allow pushing to a single host or delete this section to allow pushing to any host. + spec.metadata = { + "source_code_uri" => "https://github.com/Invoca/process_settings", + 'allowed_push_host' => "https://rubygems.org" + } + + # Specify which files should be added to the gem when it is released. + # The `git ls-files -z` loads the files in the RubyGem that have been added into git. + spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do + `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } + end + spec.require_paths = ["lib"] + + spec.test_files = [ + "test/test_helper.rb", + "test/db/schema.rb", + "test/fixtures/enumeration.rb", + "test/fixtures/enum_controller.rb", + "test/enum_controller_test.rb", + "test/enum_mysql_test.rb" + ] end diff --git a/lib/active_record/mysql/enum/version.rb b/lib/active_record/mysql/enum/version.rb new file mode 100644 index 0000000..47584a9 --- /dev/null +++ b/lib/active_record/mysql/enum/version.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module ActiveRecord + module Mysql + module Enum + VERSION = "0.1.0" + end + end +end From 9ec9d1d87be3afb404fbedda38eb3fa4620308bb Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Fri, 14 Aug 2020 19:05:54 +0200 Subject: [PATCH 02/15] file restructuring for new name --- lib/active_record/mysql/enum.rb | 25 ++++++++ .../mysql}/enum/active_record_helper.rb | 0 .../mysql}/enum/enum_adapter.rb | 0 .../mysql}/enum/enum_column_adapter.rb | 0 .../mysql}/enum/enum_type.rb | 0 lib/active_record/mysql/enum/mysql_adapter.rb | 57 +++++++++++++++++++ lib/{ => active_record/mysql}/enum/quoting.rb | 0 .../mysql}/enum/schema_definitions.rb | 0 .../mysql}/enum/validations.rb | 0 lib/enum/mysql_adapter.rb | 56 ------------------ lib/enum_column.rb | 19 ------- lib/enum_column3.rb | 1 - 12 files changed, 82 insertions(+), 76 deletions(-) create mode 100644 lib/active_record/mysql/enum.rb rename lib/{ => active_record/mysql}/enum/active_record_helper.rb (100%) rename lib/{ => active_record/mysql}/enum/enum_adapter.rb (100%) rename lib/{ => active_record/mysql}/enum/enum_column_adapter.rb (100%) rename lib/{ => active_record/mysql}/enum/enum_type.rb (100%) create mode 100644 lib/active_record/mysql/enum/mysql_adapter.rb rename lib/{ => active_record/mysql}/enum/quoting.rb (100%) rename lib/{ => active_record/mysql}/enum/schema_definitions.rb (100%) rename lib/{ => active_record/mysql}/enum/validations.rb (100%) delete mode 100644 lib/enum/mysql_adapter.rb delete mode 100644 lib/enum_column.rb delete mode 100644 lib/enum_column3.rb diff --git a/lib/active_record/mysql/enum.rb b/lib/active_record/mysql/enum.rb new file mode 100644 index 0000000..6e9e26e --- /dev/null +++ b/lib/active_record/mysql/enum.rb @@ -0,0 +1,25 @@ +if defined?(::Rails::Railtie) + module ActiveRecord + module Mysql + module Enum + class Railtie < Rails::Railtie + initializer 'active_record-mysql-enum.initialize', :after => 'active_record.initialize_database' do |app| + ActiveSupport.on_load :active_record do + require 'enum/mysql_adapter' + require 'enum/enum_type' + require 'enum/enum_adapter' + require 'enum/enum_column_adapter' + require 'enum/schema_definitions' + require 'enum/quoting' + require 'enum/validations' + end + + ActiveSupport.on_load :action_view do + require 'enum/active_record_helper' + end + end + end + end + end + end +end diff --git a/lib/enum/active_record_helper.rb b/lib/active_record/mysql/enum/active_record_helper.rb similarity index 100% rename from lib/enum/active_record_helper.rb rename to lib/active_record/mysql/enum/active_record_helper.rb diff --git a/lib/enum/enum_adapter.rb b/lib/active_record/mysql/enum/enum_adapter.rb similarity index 100% rename from lib/enum/enum_adapter.rb rename to lib/active_record/mysql/enum/enum_adapter.rb diff --git a/lib/enum/enum_column_adapter.rb b/lib/active_record/mysql/enum/enum_column_adapter.rb similarity index 100% rename from lib/enum/enum_column_adapter.rb rename to lib/active_record/mysql/enum/enum_column_adapter.rb diff --git a/lib/enum/enum_type.rb b/lib/active_record/mysql/enum/enum_type.rb similarity index 100% rename from lib/enum/enum_type.rb rename to lib/active_record/mysql/enum/enum_type.rb diff --git a/lib/active_record/mysql/enum/mysql_adapter.rb b/lib/active_record/mysql/enum/mysql_adapter.rb new file mode 100644 index 0000000..f514929 --- /dev/null +++ b/lib/active_record/mysql/enum/mysql_adapter.rb @@ -0,0 +1,57 @@ +adapter_class = if defined? ActiveRecord::ConnectionAdapters::MySQLJdbcConnection + ActiveRecord::ConnectionAdapters::MySQLJdbcConnection +# elsif defined? ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter +# ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter +elsif defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter + ActiveRecord::ConnectionAdapters::Mysql2Adapter +elsif defined? ActiveRecord::ConnectionAdapters::MysqlAdapter + ActiveRecord::ConnectionAdapters::MysqlAdapter +end + +module ActiveRecord + module Mysql + module Enum + module MysqlAdapter + def native_database_types #:nodoc + types = super + types[:enum] = { :name => "enum" } + types + end + + # Add enumeration support for schema statement creation. This + # will have to be adapted for every adapter if the type requires + # anything by a list of allowed values. The overrides the standard + # type_to_sql method and chains back to the default. This could + # be done on a per adapter basis, but is generalized here. + # + # will generate enum('a', 'b', 'c') for :limit => [:a, :b, :c] + def type_to_sql(type, limit: nil, precision: nil, scale: nil, unsigned: nil, **) # :nodoc: + if type.to_s == 'enum' + native = native_database_types[type] + column_type_sql = (native || {})[:name] || 'enum' + + column_type_sql << "(#{limit.map { |v| quote(v) }.join(',')})" + + column_type_sql + else + super(type, limit: limit, precision: precision, scale: scale, unsigned: unsigned) + end + end + + private + def initialize_type_map(m = type_map) + super + + m.register_type(%r(enum)i) do |sql_type| + limit = sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.intern } + ActiveRecord::Type::Enum.new(limit: limit) + end + end + end + end + end +end + +if adapter_class + adapter_class.prepend(ActiveRecord::Mysql::Enum::MysqlAdapter) +end diff --git a/lib/enum/quoting.rb b/lib/active_record/mysql/enum/quoting.rb similarity index 100% rename from lib/enum/quoting.rb rename to lib/active_record/mysql/enum/quoting.rb diff --git a/lib/enum/schema_definitions.rb b/lib/active_record/mysql/enum/schema_definitions.rb similarity index 100% rename from lib/enum/schema_definitions.rb rename to lib/active_record/mysql/enum/schema_definitions.rb diff --git a/lib/enum/validations.rb b/lib/active_record/mysql/enum/validations.rb similarity index 100% rename from lib/enum/validations.rb rename to lib/active_record/mysql/enum/validations.rb diff --git a/lib/enum/mysql_adapter.rb b/lib/enum/mysql_adapter.rb deleted file mode 100644 index d7729f5..0000000 --- a/lib/enum/mysql_adapter.rb +++ /dev/null @@ -1,56 +0,0 @@ -adapter_class = if defined? ActiveRecord::ConnectionAdapters::MySQLJdbcConnection - ActiveRecord::ConnectionAdapters::MySQLJdbcConnection -# elsif defined? ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter -# ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter -elsif defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter - ActiveRecord::ConnectionAdapters::Mysql2Adapter -elsif defined? ActiveRecord::ConnectionAdapters::MysqlAdapter - ActiveRecord::ConnectionAdapters::MysqlAdapter -end - -module EnumColumn - module ConnectionAdapters - module EnumAdapter - - def native_database_types #:nodoc - types = super - types[:enum] = { :name => "enum" } - types - end - - # Add enumeration support for schema statement creation. This - # will have to be adapted for every adapter if the type requires - # anything by a list of allowed values. The overrides the standard - # type_to_sql method and chains back to the default. This could - # be done on a per adapter basis, but is generalized here. - # - # will generate enum('a', 'b', 'c') for :limit => [:a, :b, :c] - def type_to_sql(type, limit: nil, precision: nil, scale: nil, unsigned: nil, **) # :nodoc: - if type.to_s == 'enum' - native = native_database_types[type] - column_type_sql = (native || {})[:name] || 'enum' - - column_type_sql << "(#{limit.map { |v| quote(v) }.join(',')})" - - column_type_sql - else - super(type, limit: limit, precision: precision, scale: scale, unsigned: unsigned) - end - end - - private - def initialize_type_map(m = type_map) - super - - m.register_type(%r(enum)i) do |sql_type| - limit = sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.intern } - ActiveRecord::Type::Enum.new(limit: limit) - end - end - end - end -end - -if adapter_class - adapter_class.prepend(EnumColumn::ConnectionAdapters::EnumAdapter) -end diff --git a/lib/enum_column.rb b/lib/enum_column.rb deleted file mode 100644 index fe343b3..0000000 --- a/lib/enum_column.rb +++ /dev/null @@ -1,19 +0,0 @@ -if defined?(::Rails::Railtie) - class EnumColumnRailtie < Rails::Railtie - initializer 'enum_column.initialize', :after => 'active_record.initialize_database' do |app| - ActiveSupport.on_load :active_record do - require 'enum/mysql_adapter' - require 'enum/enum_type' - require 'enum/enum_adapter' - require 'enum/enum_column_adapter' - require 'enum/schema_definitions' - require 'enum/quoting' - require 'enum/validations' - end - - ActiveSupport.on_load :action_view do - require 'enum/active_record_helper' - end - end - end -end diff --git a/lib/enum_column3.rb b/lib/enum_column3.rb deleted file mode 100644 index f970053..0000000 --- a/lib/enum_column3.rb +++ /dev/null @@ -1 +0,0 @@ -require 'enum_column' \ No newline at end of file From 9f4faf0feee82b04235c9bd9a0b4c9de6473a648 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 14:01:00 +0200 Subject: [PATCH 03/15] update file references --- enum_column.gemspec | 2 +- lib/active_record/mysql/enum.rb | 16 ++++++++-------- lib/active_record/mysql/enum/validations.rb | 8 ++++---- rails/init.rb | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/enum_column.gemspec b/enum_column.gemspec index edebc07..a355f37 100644 --- a/enum_column.gemspec +++ b/enum_column.gemspec @@ -17,7 +17,7 @@ Gem::Specification.new do |spec| # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' # to allow pushing to a single host or delete this section to allow pushing to any host. spec.metadata = { - "source_code_uri" => "https://github.com/Invoca/process_settings", + "source_code_uri" => "https://github.com/Invoca/activerecord-mysql-enum", 'allowed_push_host' => "https://rubygems.org" } diff --git a/lib/active_record/mysql/enum.rb b/lib/active_record/mysql/enum.rb index 6e9e26e..dfdae89 100644 --- a/lib/active_record/mysql/enum.rb +++ b/lib/active_record/mysql/enum.rb @@ -5,17 +5,17 @@ module Enum class Railtie < Rails::Railtie initializer 'active_record-mysql-enum.initialize', :after => 'active_record.initialize_database' do |app| ActiveSupport.on_load :active_record do - require 'enum/mysql_adapter' - require 'enum/enum_type' - require 'enum/enum_adapter' - require 'enum/enum_column_adapter' - require 'enum/schema_definitions' - require 'enum/quoting' - require 'enum/validations' + require 'active_record/mysql/enum/mysql_adapter' + require 'active_record/mysql/enum/enum_type' + require 'active_record/mysql/enum/enum_adapter' + require 'active_record/mysql/enum/enum_column_adapter' + require 'active_record/mysql/enum/schema_definitions' + require 'active_record/mysql/enum/quoting' + require 'active_record/mysql/enum/validations' end ActiveSupport.on_load :action_view do - require 'enum/active_record_helper' + require 'active_record/mysql/enum/active_record_helper' end end end diff --git a/lib/active_record/mysql/enum/validations.rb b/lib/active_record/mysql/enum/validations.rb index 4dd3c92..5ba6f94 100644 --- a/lib/active_record/mysql/enum/validations.rb +++ b/lib/active_record/mysql/enum/validations.rb @@ -13,18 +13,18 @@ def validates_columns(*column_names) column_names.each do |name| col = cols[name.to_s] raise ArgumentError, "Cannot find column #{name}" unless col - + # test for nullability validates_presence_of(name) if !col.null - + # Test various known types. case col.type when :enum validates_inclusion_of name, :in => col.limit, :allow_nil => true - + when :integer, :float validates_numericality_of name, :allow_nil => true - + when :string if col.limit validates_length_of name, :maximum => col.limit, :allow_nil => true diff --git a/rails/init.rb b/rails/init.rb index f970053..038ce47 100644 --- a/rails/init.rb +++ b/rails/init.rb @@ -1 +1 @@ -require 'enum_column' \ No newline at end of file +require 'active_record/mysql/enum' From 09eb9947d2d4e707dec87125d4dc84c159ab0f52 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 14:13:21 +0200 Subject: [PATCH 04/15] update readme for the project --- README.md | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.txt | 60 -------------------------------------------------- 2 files changed, 64 insertions(+), 60 deletions(-) create mode 100644 README.md delete mode 100644 README.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..dc75f20 --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +# ActiveRecord::Mysql::Enum + +This gem is an extension to ActiveRecord which enables native support of +enumerations in the database schema using the ENUM type in MySQL. Forked +and revitalized from the early work that Nick Pohodnya did with [enum_column3](https://github.com/jewlr/enum_column). + +## Support +Currently this has been manually tested with Rails version 4 and 5, and works with scaffolding. + +**Supported adapters:** +- mysql +- mysql2 +- jdbcmysql (by Nilesh Trivedi) + +## Installation +In your `Gemfile` add the following snippet +```ruby +gem 'activerecord-mysql-enum', '~> 0.1', require: 'active_support/mysql/enum' +``` + +## Usage +### Schema Definitions +When defining an enum in your schema, specify the constraint as a limit: +```ruby +create_table :enumerations, :force => true do |t| + t.column :severity, :enum, :limit => [:low, :medium, :high, :critical], :default => :medium + t.column :color, :enum, :limit => [:red, :blue, :green, :yellow] +end +``` + +### Model Validations +You can then automatically validate this column using: +```ruby +validates_columns :severity, :color +``` + +### Setting/Getting Values +All enumerated values will be given as symbols. +```ruby +@e = Enumeration.new +@e.severity = :medium +``` + +You can always use the column reflection to get the list of possible values from the database column. +```ruby +irb(1)> Enumeration.columns_hash['color'].limit +=> [:red, :blue, :green, :yellow] +irb(2)> @enumeration.column_for_attribute(:color).limit +=> [:red, :blue, :green, :yellow] +``` + +### Form Helpers +You can use enum_select helper to generate input for enumerated attribute as: +```ruby +<%= enum_select(@enumeration, 'severity')%> +``` + +Or using `form_for`: +```ruby +<%= form_for @enumeration do |f| %> + <%= f.label :severity %> + <%= f.enum_select :severity %> +<% end %> +``` diff --git a/README.txt b/README.txt deleted file mode 100644 index c55cd5a..0000000 --- a/README.txt +++ /dev/null @@ -1,60 +0,0 @@ -Overview - - This gem is an extension to ActiveRecord which enables native support of enumerations in the database schema using the ENUM type in MySQL. - Currently only MySQL is implemented. - Tested with Rails 3, for Rails 2 you should better use enum-column plugin (http://rubyforge.org/projects/enum-column/) - Works with Scaffolding. - - Supported adapters: - mysql - mysql2 - jdbcmysql (by Nilesh Trivedi) - -How to use it. - -In you Gemfile: - - gem 'enum_column3' - -In your schema: - - When you create your schema, specify the constraint as a limit: - - create_table :enumerations, :force => true do |t| - t.column :severity, :enum, :limit => [:low, :medium, :high, :critical], :default => :medium - t.column :color, :enum, :limit => [:red, :blue, :green, :yellow] - ... - end - - -In the model: - - You can then automatically validate this column using: - - validates_columns :severity, :color - - The rest will be handled for you. All enumerated values will be given as symbols. - - @e = Enumeration.new - @e.severity = :medium - -You can always use the column reflection to get the list of possible values from the database column. - - Enumeration.columns_hash['color'].limit - or - @enumeration.column_for_attribute(:color).limit - - Will yield: [:red, :blue, :green, :yellow] - - -In views: - - You can use enum_select helper to generate input for enumerated attribute as: - - <%= enum_select(@enumeration, 'severity')%> - or - <%= form_for @enumeration do |f| %> - <%= f.label :severity %> - <%= f.enum_select :severity %> - <% end %> - From eecaf6bebc51f0d885c7f9c661e3c472dec8d59a Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 14:17:26 +0200 Subject: [PATCH 05/15] add changelog for tracking changes to the project --- CHANGELOG.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6840565 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,11 @@ +# CHANGELOG for `activerecord-mysql-enum` + +Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). + +Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [0.1.0] - Unreleased +### Changed +- Renamed the gem from `enum_column3` to `activerecord-mysql-enum` + +[0.1.0]: https://github.com/Invoca/activerecord-mysql-enum/tree/0.1.0 From 4d1717cee5757faca176e5d64b776b47396f6476 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 14:18:05 +0200 Subject: [PATCH 06/15] add rails 4 compatibility --- CHANGELOG.md | 3 ++ lib/active_record/mysql/enum/mysql_adapter.rb | 32 ++++++++++++++----- lib/active_record/mysql/enum/quoting.rb | 20 +++++++++--- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6840565..3299fa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [0.1.0] - Unreleased +### Added +- Backwards compatibility with Rails 4 + ### Changed - Renamed the gem from `enum_column3` to `activerecord-mysql-enum` diff --git a/lib/active_record/mysql/enum/mysql_adapter.rb b/lib/active_record/mysql/enum/mysql_adapter.rb index f514929..7b98d61 100644 --- a/lib/active_record/mysql/enum/mysql_adapter.rb +++ b/lib/active_record/mysql/enum/mysql_adapter.rb @@ -25,19 +25,35 @@ def native_database_types #:nodoc # be done on a per adapter basis, but is generalized here. # # will generate enum('a', 'b', 'c') for :limit => [:a, :b, :c] - def type_to_sql(type, limit: nil, precision: nil, scale: nil, unsigned: nil, **) # :nodoc: - if type.to_s == 'enum' - native = native_database_types[type] - column_type_sql = (native || {})[:name] || 'enum' + if Rails::VERSION::MAJOR < 5 + def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = nil, **) # :nodoc: + if type.to_s == 'enum' + native = native_database_types[type] + column_type_sql = (native || {})[:name] || 'enum' - column_type_sql << "(#{limit.map { |v| quote(v) }.join(',')})" + column_type_sql << "(#{limit.map { |v| quote(v) }.join(',')})" - column_type_sql - else - super(type, limit: limit, precision: precision, scale: scale, unsigned: unsigned) + column_type_sql + else + super(type, limit, precision, scale, unsigned) + end + end + else + def type_to_sql(type, limit: nil, precision: nil, scale: nil, unsigned: nil, **) # :nodoc: + if type.to_s == 'enum' + native = native_database_types[type] + column_type_sql = (native || {})[:name] || 'enum' + + column_type_sql << "(#{limit.map { |v| quote(v) }.join(',')})" + + column_type_sql + else + super(type, limit: limit, precision: precision, scale: scale, unsigned: unsigned) + end end end + private def initialize_type_map(m = type_map) super diff --git a/lib/active_record/mysql/enum/quoting.rb b/lib/active_record/mysql/enum/quoting.rb index 1b6c1f9..29cb6a2 100644 --- a/lib/active_record/mysql/enum/quoting.rb +++ b/lib/active_record/mysql/enum/quoting.rb @@ -5,11 +5,21 @@ module Quoting # Quote a symbol as a normal string. This will support quoting of # enumerated values. - def quote(value) - if !value.is_a? Symbol - __quote_enum(value) - else - ActiveRecord::Base.send(:quote_bound_value, value.to_s) + if Rails::VERSION::MAJOR < 5 + def quote(value, column = nil) + if !value.is_a? Symbol + __quote_enum(value, column) + else + ActiveRecord::Base.send(:quote_bound_value, value.to_s) + end + end + else + def quote(value) + if !value.is_a? Symbol + __quote_enum(value) + else + ActiveRecord::Base.send(:quote_bound_value, value.to_s) + end end end end From cbaaf91024241a0c174da1a13f20468484b0ed53 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 14:28:36 +0200 Subject: [PATCH 07/15] setup dependencies for initial pre release --- Gemfile | 10 ++ Gemfile.lock | 124 ++++++++++++++++++++++++ enum_column.gemspec | 9 +- lib/active_record/mysql/enum/version.rb | 2 +- 4 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..4dda7da --- /dev/null +++ b/Gemfile @@ -0,0 +1,10 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in active_table_set.gemspec +gemspec + +gem 'bundler', '~> 1.8' +gem 'pry' +gem 'pry-byebug' +gem 'rake', '~> 13.0' +gem 'rails', '~> 4.2' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..f40cdf0 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,124 @@ +PATH + remote: . + specs: + activerecord-mysql-enum (0.1.0.pre.1) + activerecord (>= 4.2, < 6) + +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.2.11.3) + actionpack (= 4.2.11.3) + actionview (= 4.2.11.3) + activejob (= 4.2.11.3) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 1.0, >= 1.0.5) + actionpack (4.2.11.3) + actionview (= 4.2.11.3) + activesupport (= 4.2.11.3) + rack (~> 1.6) + rack-test (~> 0.6.2) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (4.2.11.3) + activesupport (= 4.2.11.3) + builder (~> 3.1) + erubis (~> 2.7.0) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (4.2.11.3) + activesupport (= 4.2.11.3) + globalid (>= 0.3.0) + activemodel (4.2.11.3) + activesupport (= 4.2.11.3) + builder (~> 3.1) + activerecord (4.2.11.3) + activemodel (= 4.2.11.3) + activesupport (= 4.2.11.3) + arel (~> 6.0) + activesupport (4.2.11.3) + i18n (~> 0.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + arel (6.0.4) + builder (3.2.4) + byebug (11.1.3) + coderay (1.1.3) + concurrent-ruby (1.1.7) + crass (1.0.6) + erubis (2.7.0) + globalid (0.4.2) + activesupport (>= 4.2.0) + i18n (0.9.5) + concurrent-ruby (~> 1.0) + loofah (2.6.0) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.7.1) + mini_mime (>= 0.1.1) + method_source (1.0.0) + mini_mime (1.0.2) + mini_portile2 (2.4.0) + minitest (5.14.1) + nokogiri (1.10.10) + mini_portile2 (~> 2.4.0) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) + pry-byebug (3.9.0) + byebug (~> 11.0) + pry (~> 0.13.0) + rack (1.6.13) + rack-test (0.6.3) + rack (>= 1.0) + rails (4.2.11.3) + actionmailer (= 4.2.11.3) + actionpack (= 4.2.11.3) + actionview (= 4.2.11.3) + activejob (= 4.2.11.3) + activemodel (= 4.2.11.3) + activerecord (= 4.2.11.3) + activesupport (= 4.2.11.3) + bundler (>= 1.3.0, < 2.0) + railties (= 4.2.11.3) + sprockets-rails + rails-deprecated_sanitizer (1.0.3) + activesupport (>= 4.2.0.alpha) + rails-dom-testing (1.0.9) + activesupport (>= 4.2.0, < 5.0) + nokogiri (~> 1.6) + rails-deprecated_sanitizer (>= 1.0.1) + rails-html-sanitizer (1.3.0) + loofah (~> 2.3) + railties (4.2.11.3) + actionpack (= 4.2.11.3) + activesupport (= 4.2.11.3) + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (13.0.1) + sprockets (3.7.2) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.2.1) + actionpack (>= 4.0) + activesupport (>= 4.0) + sprockets (>= 3.0.0) + thor (1.0.1) + thread_safe (0.3.6) + tzinfo (1.2.7) + thread_safe (~> 0.1) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord-mysql-enum! + bundler (~> 1.8) + pry + pry-byebug + rails (~> 4.2) + rake (~> 13.0) + +BUNDLED WITH + 1.17.3 diff --git a/enum_column.gemspec b/enum_column.gemspec index a355f37..fa641b7 100644 --- a/enum_column.gemspec +++ b/enum_column.gemspec @@ -28,12 +28,5 @@ Gem::Specification.new do |spec| end spec.require_paths = ["lib"] - spec.test_files = [ - "test/test_helper.rb", - "test/db/schema.rb", - "test/fixtures/enumeration.rb", - "test/fixtures/enum_controller.rb", - "test/enum_controller_test.rb", - "test/enum_mysql_test.rb" - ] + spec.add_dependency 'activerecord', '>= 4.2', '< 6' end diff --git a/lib/active_record/mysql/enum/version.rb b/lib/active_record/mysql/enum/version.rb index 47584a9..813c19f 100644 --- a/lib/active_record/mysql/enum/version.rb +++ b/lib/active_record/mysql/enum/version.rb @@ -3,7 +3,7 @@ module ActiveRecord module Mysql module Enum - VERSION = "0.1.0" + VERSION = "0.1.0.pre.1" end end end From 719a24ac01882121a95b8539e18f220b7a87d8c5 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 14:29:43 +0200 Subject: [PATCH 08/15] add Rakefile --- Rakefile | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Rakefile diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..80ea6be --- /dev/null +++ b/Rakefile @@ -0,0 +1,4 @@ +#!/usr/bin/env rake +# frozen_string_literal: true + +Bundler::GemHelper.install_tasks From 37e2c2248a16a63d7bcb9beed0cc03758c654539 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 14:30:13 +0200 Subject: [PATCH 09/15] add frozen_string_literal comment to all files --- Gemfile | 2 ++ init.rb | 2 ++ lib/active_record/mysql/enum.rb | 2 ++ lib/active_record/mysql/enum/active_record_helper.rb | 2 ++ lib/active_record/mysql/enum/enum_adapter.rb | 2 ++ lib/active_record/mysql/enum/enum_column_adapter.rb | 2 ++ lib/active_record/mysql/enum/enum_type.rb | 2 ++ lib/active_record/mysql/enum/mysql_adapter.rb | 2 ++ lib/active_record/mysql/enum/quoting.rb | 2 ++ lib/active_record/mysql/enum/schema_definitions.rb | 1 + lib/active_record/mysql/enum/validations.rb | 1 + rails/init.rb | 2 ++ test/db/schema.rb | 1 + test/enum_controller_test.rb | 2 ++ test/enum_mysql_test.rb | 2 ++ test/fixtures/enum_controller.rb | 2 ++ test/fixtures/enumeration.rb | 1 + test/test_helper.rb | 2 ++ 18 files changed, 32 insertions(+) diff --git a/Gemfile b/Gemfile index 4dda7da..fe2d151 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' # Specify your gem's dependencies in active_table_set.gemspec diff --git a/init.rb b/init.rb index 09d8ec6..ecbc085 100644 --- a/init.rb +++ b/init.rb @@ -1 +1,3 @@ +# frozen_string_literal: true + require File.dirname(__FILE__) + "/rails/init.rb" \ No newline at end of file diff --git a/lib/active_record/mysql/enum.rb b/lib/active_record/mysql/enum.rb index dfdae89..c7201a2 100644 --- a/lib/active_record/mysql/enum.rb +++ b/lib/active_record/mysql/enum.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + if defined?(::Rails::Railtie) module ActiveRecord module Mysql diff --git a/lib/active_record/mysql/enum/active_record_helper.rb b/lib/active_record/mysql/enum/active_record_helper.rb index 0f3e3a5..693603a 100644 --- a/lib/active_record/mysql/enum/active_record_helper.rb +++ b/lib/active_record/mysql/enum/active_record_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + if defined?(Rails::Generator) module Rails module Generator diff --git a/lib/active_record/mysql/enum/enum_adapter.rb b/lib/active_record/mysql/enum/enum_adapter.rb index b7b6b0c..e6b4479 100644 --- a/lib/active_record/mysql/enum/enum_adapter.rb +++ b/lib/active_record/mysql/enum/enum_adapter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This module provides all the column helper methods to deal with the # values and adds the common type management code for the adapters. diff --git a/lib/active_record/mysql/enum/enum_column_adapter.rb b/lib/active_record/mysql/enum/enum_column_adapter.rb index 4f95271..f0f8f95 100644 --- a/lib/active_record/mysql/enum/enum_column_adapter.rb +++ b/lib/active_record/mysql/enum/enum_column_adapter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This module provides all the column helper methods to deal with the # values and adds the common type management code for the adapters. diff --git a/lib/active_record/mysql/enum/enum_type.rb b/lib/active_record/mysql/enum/enum_type.rb index 9ae0678..f9b3656 100644 --- a/lib/active_record/mysql/enum/enum_type.rb +++ b/lib/active_record/mysql/enum/enum_type.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + if defined? ActiveRecord::Type::Value module ActiveRecord module Type diff --git a/lib/active_record/mysql/enum/mysql_adapter.rb b/lib/active_record/mysql/enum/mysql_adapter.rb index 7b98d61..3da6280 100644 --- a/lib/active_record/mysql/enum/mysql_adapter.rb +++ b/lib/active_record/mysql/enum/mysql_adapter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + adapter_class = if defined? ActiveRecord::ConnectionAdapters::MySQLJdbcConnection ActiveRecord::ConnectionAdapters::MySQLJdbcConnection # elsif defined? ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter diff --git a/lib/active_record/mysql/enum/quoting.rb b/lib/active_record/mysql/enum/quoting.rb index 29cb6a2..fb83b4a 100644 --- a/lib/active_record/mysql/enum/quoting.rb +++ b/lib/active_record/mysql/enum/quoting.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ActiveRecord module ConnectionAdapters # :nodoc: module Quoting diff --git a/lib/active_record/mysql/enum/schema_definitions.rb b/lib/active_record/mysql/enum/schema_definitions.rb index 9872606..fc393f3 100644 --- a/lib/active_record/mysql/enum/schema_definitions.rb +++ b/lib/active_record/mysql/enum/schema_definitions.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module ActiveRecord module ConnectionAdapters diff --git a/lib/active_record/mysql/enum/validations.rb b/lib/active_record/mysql/enum/validations.rb index 5ba6f94..7d5b76c 100644 --- a/lib/active_record/mysql/enum/validations.rb +++ b/lib/active_record/mysql/enum/validations.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module ActiveRecord module Validations diff --git a/rails/init.rb b/rails/init.rb index 038ce47..22816c6 100644 --- a/rails/init.rb +++ b/rails/init.rb @@ -1 +1,3 @@ +# frozen_string_literal: true + require 'active_record/mysql/enum' diff --git a/test/db/schema.rb b/test/db/schema.rb index 96ba17c..b782cea 100644 --- a/test/db/schema.rb +++ b/test/db/schema.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true ActiveRecord::Schema.define do create_table :enumerations, :force => true do |t| diff --git a/test/enum_controller_test.rb b/test/enum_controller_test.rb index 98ca6e5..572661f 100644 --- a/test/enum_controller_test.rb +++ b/test/enum_controller_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require File.dirname(__FILE__) + '/test_helper' require 'fixtures/enumeration' require 'fixtures/enum_controller' diff --git a/test/enum_mysql_test.rb b/test/enum_mysql_test.rb index 7065f68..083c531 100644 --- a/test/enum_mysql_test.rb +++ b/test/enum_mysql_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require File.dirname(__FILE__) + '/test_helper' require 'fixtures/enumeration' diff --git a/test/fixtures/enum_controller.rb b/test/fixtures/enum_controller.rb index 9be9d2f..84ab764 100644 --- a/test/fixtures/enum_controller.rb +++ b/test/fixtures/enum_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class EnumController < ActionController::Base layout false diff --git a/test/fixtures/enumeration.rb b/test/fixtures/enumeration.rb index a9b7d10..47bc345 100644 --- a/test/fixtures/enumeration.rb +++ b/test/fixtures/enumeration.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true class Enumeration < ActiveRecord::Base validates_columns :color, :severity, :string_field, :int_field diff --git a/test/test_helper.rb b/test/test_helper.rb index 2be11a1..d457d84 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # ============================================================================= # Include the files required to test Engines. From 55096ef564599ea693519bda4e7f07936aa9050d Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 14:31:36 +0200 Subject: [PATCH 10/15] setup .gitignore for the project --- .gitignore | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9106b2a --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ From 317edbdad789d57c8a9cb04d9aeb9166f3ceff60 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 14:34:52 +0200 Subject: [PATCH 11/15] add basic automation for release and dependency updates --- .dependabot/config.yml | 10 ++++++++ .github/workflows/gem_release.yml | 38 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 .dependabot/config.yml create mode 100644 .github/workflows/gem_release.yml diff --git a/.dependabot/config.yml b/.dependabot/config.yml new file mode 100644 index 0000000..349c669 --- /dev/null +++ b/.dependabot/config.yml @@ -0,0 +1,10 @@ +--- +version: 1 +update_configs: +- package_manager: "ruby:bundler" + directory: "/" + update_schedule: "live" + version_requirement_updates: "off" + commit_message: + prefix: "No-Jira" + include_scope: true diff --git a/.github/workflows/gem_release.yml b/.github/workflows/gem_release.yml new file mode 100644 index 0000000..07ff6d8 --- /dev/null +++ b/.github/workflows/gem_release.yml @@ -0,0 +1,38 @@ +--- +on: + push: + tags: + - 'v*' + - '!v*.pre*' + +name: Create Release + +jobs: + build: + name: Create Release + runs-on: ubuntu-latest + steps: + - name: Get version from tag + id: tag_name + shell: bash + run: | + echo ::set-output name=current_version::${GITHUB_REF#refs/tags/v} + - name: Checkout code + uses: actions/checkout@v2 + - name: Get Changelog Entry + id: changelog_reader + uses: mindsers/changelog-reader-action@v1 + with: + version: ${{ steps.tag_name.outputs.current_version }} + path: ./CHANGELOG.md + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GEM_RELEASE_GIT_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + body: ${{ steps.changelog_reader.outputs.log_entry }} + draft: false + prerelease: false From cff57d84e22515590415272d4747567d1dd4c42d Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 18:27:20 +0200 Subject: [PATCH 12/15] update readme wording --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dc75f20..878a3d3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ This gem is an extension to ActiveRecord which enables native support of enumerations in the database schema using the ENUM type in MySQL. Forked -and revitalized from the early work that Nick Pohodnya did with [enum_column3](https://github.com/jewlr/enum_column). +and revitalized from [enum_column3](https://github.com/jewlr/enum_column) +which was itself a fork of a fork of Nick Pohodnya's original gem for +Rails 3, [enum_column3](https://github.com/electronick/enum_column). ## Support Currently this has been manually tested with Rails version 4 and 5, and works with scaffolding. From 132e48db4f3e2306165a9b472879cade5153eccc Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 18:27:33 +0200 Subject: [PATCH 13/15] clean up gemspec definition --- enum_column.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/enum_column.gemspec b/enum_column.gemspec index a355f37..ad6f130 100644 --- a/enum_column.gemspec +++ b/enum_column.gemspec @@ -1,6 +1,6 @@ # frozen_string_literal: true -lib = File.expand_path("../lib", __FILE__) +lib = File.expand_path("lib", __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "active_record/mysql/enum/version" @@ -11,7 +11,7 @@ Gem::Specification.new do |spec| spec.email = ["development@invoca.com"] spec.summary = "Enable enum type for the MySQL Adapter in ActiveRecord" - spec.description = "Enable enum type for the MySQL Adapter in ActiveRecord" + spec.description = spec.summary spec.homepage = "http://github.com/Invoca/activerecord-mysql-enum" # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' From cd4f4ce92e6f467552e60e7da2a86a83e580cacd Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 18:28:02 +0200 Subject: [PATCH 14/15] update to newer regex syntax --- lib/active_record/mysql/enum/mysql_adapter.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/active_record/mysql/enum/mysql_adapter.rb b/lib/active_record/mysql/enum/mysql_adapter.rb index f514929..88585f2 100644 --- a/lib/active_record/mysql/enum/mysql_adapter.rb +++ b/lib/active_record/mysql/enum/mysql_adapter.rb @@ -39,11 +39,12 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, unsigned: nil, **) end private + def initialize_type_map(m = type_map) super - m.register_type(%r(enum)i) do |sql_type| - limit = sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.intern } + m.register_type(/enum/i) do |sql_type| + limit = sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.to_sym } ActiveRecord::Type::Enum.new(limit: limit) end end From 01263dd19adc26acdd3304a3a61bc3048dff3fa0 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 18:30:44 +0200 Subject: [PATCH 15/15] explicitly ignore options in mysql adaptor --- lib/active_record/mysql/enum/mysql_adapter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/active_record/mysql/enum/mysql_adapter.rb b/lib/active_record/mysql/enum/mysql_adapter.rb index 3da6280..acf3ada 100644 --- a/lib/active_record/mysql/enum/mysql_adapter.rb +++ b/lib/active_record/mysql/enum/mysql_adapter.rb @@ -28,7 +28,7 @@ def native_database_types #:nodoc # # will generate enum('a', 'b', 'c') for :limit => [:a, :b, :c] if Rails::VERSION::MAJOR < 5 - def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = nil, **) # :nodoc: + def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = nil, **_options) # :nodoc: if type.to_s == 'enum' native = native_database_types[type] column_type_sql = (native || {})[:name] || 'enum' @@ -41,7 +41,7 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = nil, end end else - def type_to_sql(type, limit: nil, precision: nil, scale: nil, unsigned: nil, **) # :nodoc: + def type_to_sql(type, limit: nil, precision: nil, scale: nil, unsigned: nil, **_options) # :nodoc: if type.to_s == 'enum' native = native_database_types[type] column_type_sql = (native || {})[:name] || 'enum'