From 4d1717cee5757faca176e5d64b776b47396f6476 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 14:18:05 +0200 Subject: [PATCH 1/7] 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 2/7] 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 3/7] 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 4/7] 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 5/7] 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 6/7] 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 01263dd19adc26acdd3304a3a61bc3048dff3fa0 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Mon, 17 Aug 2020 18:30:44 +0200 Subject: [PATCH 7/7] 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'