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 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/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3299fa7 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,14 @@ +# 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 +### Added +- Backwards compatibility with Rails 4 + +### 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 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fe2d151 --- /dev/null +++ b/Gemfile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +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/README.md b/README.md new file mode 100644 index 0000000..878a3d3 --- /dev/null +++ b/README.md @@ -0,0 +1,66 @@ +# 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 [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. + +**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 %> - 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 diff --git a/enum_column.gemspec b/enum_column.gemspec index 772ce95..6d2e430 100644 --- a/enum_column.gemspec +++ b/enum_column.gemspec @@ -1,17 +1,32 @@ -# -*- 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", __dir__) +$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 = 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' + # 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/activerecord-mysql-enum", + '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.add_dependency 'activerecord', '>= 4.2', '< 6' end 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 new file mode 100644 index 0000000..c7201a2 --- /dev/null +++ b/lib/active_record/mysql/enum.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +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 '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 'active_record/mysql/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 98% rename from lib/enum/active_record_helper.rb rename to lib/active_record/mysql/enum/active_record_helper.rb index 0f3e3a5..693603a 100644 --- a/lib/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/enum/enum_adapter.rb b/lib/active_record/mysql/enum/enum_adapter.rb similarity index 96% rename from lib/enum/enum_adapter.rb rename to lib/active_record/mysql/enum/enum_adapter.rb index b7b6b0c..e6b4479 100644 --- a/lib/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/enum/enum_column_adapter.rb b/lib/active_record/mysql/enum/enum_column_adapter.rb similarity index 99% rename from lib/enum/enum_column_adapter.rb rename to lib/active_record/mysql/enum/enum_column_adapter.rb index 4f95271..f0f8f95 100644 --- a/lib/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/enum/enum_type.rb b/lib/active_record/mysql/enum/enum_type.rb similarity index 94% rename from lib/enum/enum_type.rb rename to lib/active_record/mysql/enum/enum_type.rb index 9ae0678..f9b3656 100644 --- a/lib/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 new file mode 100644 index 0000000..d0606d8 --- /dev/null +++ b/lib/active_record/mysql/enum/mysql_adapter.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +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] + if Rails::VERSION::MAJOR < 5 + 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' + + column_type_sql << "(#{limit.map { |v| quote(v) }.join(',')})" + + 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, **_options) # :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 + + 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 + end + end + end +end + +if adapter_class + adapter_class.prepend(ActiveRecord::Mysql::Enum::MysqlAdapter) +end diff --git a/lib/active_record/mysql/enum/quoting.rb b/lib/active_record/mysql/enum/quoting.rb new file mode 100644 index 0000000..fb83b4a --- /dev/null +++ b/lib/active_record/mysql/enum/quoting.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module ActiveRecord + module ConnectionAdapters # :nodoc: + module Quoting + alias __quote_enum quote + + # Quote a symbol as a normal string. This will support quoting of + # enumerated values. + 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 + end +end diff --git a/lib/enum/schema_definitions.rb b/lib/active_record/mysql/enum/schema_definitions.rb similarity index 89% rename from lib/enum/schema_definitions.rb rename to lib/active_record/mysql/enum/schema_definitions.rb index 9872606..fc393f3 100644 --- a/lib/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/enum/validations.rb b/lib/active_record/mysql/enum/validations.rb similarity index 95% rename from lib/enum/validations.rb rename to lib/active_record/mysql/enum/validations.rb index 4dd3c92..7d5b76c 100644 --- a/lib/enum/validations.rb +++ b/lib/active_record/mysql/enum/validations.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module ActiveRecord module Validations @@ -13,18 +14,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/lib/active_record/mysql/enum/version.rb b/lib/active_record/mysql/enum/version.rb new file mode 100644 index 0000000..813c19f --- /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.pre.1" + end + end +end 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/quoting.rb b/lib/enum/quoting.rb deleted file mode 100644 index 1b6c1f9..0000000 --- a/lib/enum/quoting.rb +++ /dev/null @@ -1,17 +0,0 @@ -module ActiveRecord - module ConnectionAdapters # :nodoc: - module Quoting - alias __quote_enum quote - - # 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) - end - end - end - end -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 diff --git a/rails/init.rb b/rails/init.rb index f970053..22816c6 100644 --- a/rails/init.rb +++ b/rails/init.rb @@ -1 +1,3 @@ -require 'enum_column' \ No newline at end of file +# 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.