Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Support rails 5.1 #13

Merged
merged 15 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.gem
.bundle
Gemfile*.lock
gemfiles/*.lock
pkg/*
spec/database.yml
spec/vendor
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

31 changes: 28 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
language: ruby
rvm:
- 1.9.3
- "2.0"
- "2.1"
- "2.2"
- "2.3"
- "2.4"
- "2.5"
gemfile:
- gemfiles/activerecord_4.1.gemfile
- gemfiles/activerecord_4.2.gemfile
- gemfiles/activerecord_5.0.gemfile
- gemfiles/activerecord_5.1.gemfile
env:
- DB=mysql
- DB=sqlite
matrix:
allow_failures:
# Allow 2.5 to fail until Travis CI bug is fixed. See:
# * https://github.com/travis-ci/travis-ci/issues/8978
# * https://github.com/travis-ci/travis-ci/issues/8969
- rvm: "2.5"
exclude:
- rvm: "2.0"
gemfile: gemfiles/activerecord_5.0.gemfile
- rvm: "2.0"
gemfile: gemfiles/activerecord_5.1.gemfile
- rvm: "2.1"
gemfile: gemfiles/activerecord_5.0.gemfile
- rvm: "2.1"
gemfile: gemfiles/activerecord_5.1.gemfile
before_script:
- "mysql -e 'create database native_enum_test;' >/dev/null"
- "cp spec/{.travis.,}database.yml"
script: bundle exec rake spec:rails_all
- "bundle exec rake db:prepare"
script: bundle exec rake spec
sudo: false
15 changes: 15 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
appraise "activerecord-4.1" do
gem "activerecord", "~> 4.1.0"
end

appraise "activerecord-4.2" do
gem "activerecord", "~> 4.2.0"
end

appraise "activerecord-5.0" do
gem "activerecord", "~> 5.0.0"
end

appraise "activerecord-5.1" do
gem "activerecord", "~> 5.1.0"
end
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Unreleased

* [Support Rails 5.1](https://github.com/iangreenleaf/native_enum/pull/13)
* Drop support for Ruby 1.9.3
* Drop support for Rails < 4.1.0

# 1.0.0

* First version
44 changes: 27 additions & 17 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,36 @@ require 'rake'
desc 'Default: run all unit tests.'
task :default => :"spec:all"

require 'active_record'

# For more info on DatabaseTasks, see:
# https://github.com/rails/rails/blob/v5.0.7/activerecord/lib/active_record/tasks/database_tasks.rb
namespace :db do
task :load_config do
db_configs = YAML.load_file('spec/database.yml')
ActiveRecord::Tasks::DatabaseTasks.tap do |db_tasks|
ActiveRecord::Base.configurations = db_configs
db_tasks.database_configuration = db_configs
db_tasks.db_dir = 'db'
db_tasks.root = File.dirname(__FILE__)
end
end

desc 'Prepare the databases.'
task :prepare do
task prepare: :load_config do
unless File.exist? DB_CONFIG
cp "#{config_file}.tmpl", DB_CONFIG
cp "#{DB_CONFIG}.tmpl", DB_CONFIG
end

ActiveRecord::Tasks::DatabaseTasks.tap do |db_tasks|
db_tasks.create_current('mysql')
db_tasks.create_current('sqlite')
end
#TODO would be nice to create the DBs here
end

desc "Drop all databases created for testing"
task drop_all: :load_config do
ActiveRecord::Tasks::DatabaseTasks.drop_all
end
end

Expand All @@ -30,23 +53,10 @@ desc 'Run the test suite for all DBs.'
namespace :spec do
task :all do
db_config = YAML::load(IO.read(DB_CONFIG))
db_config.each do |db,config|
db_config.each do |db, config|
ENV["DB"] = db
Rake::Task["spec"].reenable
Rake::Task["spec"].invoke
end
end

desc 'Run the test suite for all supported versions of rails and all DBs'
task :rails_all do
STDOUT.sync = true
versions = Dir.glob(GEMFILES)
versions.each do |gemfile|
puts "Running specs for Gemfile: #{gemfile}"
Bundler.with_clean_env do
sh "bundle install --gemfile '#{gemfile}' --path 'vendor/#{File.extname(gemfile).slice(1..-1)}'"
sh "BUNDLE_GEMFILE='#{gemfile}' bundle exec rake spec:all"
end
end
end
end
7 changes: 7 additions & 0 deletions gemfiles/activerecord_4.1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "activerecord", "~> 4.1.0"

gemspec path: "../"
7 changes: 7 additions & 0 deletions gemfiles/activerecord_4.2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "activerecord", "~> 4.2.0"

gemspec path: "../"
7 changes: 7 additions & 0 deletions gemfiles/activerecord_5.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "activerecord", "~> 5.0.0"

gemspec path: "../"
7 changes: 7 additions & 0 deletions gemfiles/activerecord_5.1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "activerecord", "~> 5.1.0"

gemspec path: "../"
38 changes: 31 additions & 7 deletions lib/connection_adapters/mysql2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,50 @@

module ActiveRecord
module ConnectionAdapters
existing_class = defined?( Mysql2Adapter ) ? Mysql2Adapter : AbstractMysqlAdapter
existing_class = defined?(Mysql2Adapter) ? Mysql2Adapter : AbstractMysqlAdapter

existing_class.class_eval do
def native_database_types_with_enum
native_database_types_without_enum.merge( :enum => { :name => "enum" }, :set => { :name => "set" } )
native_database_types_without_enum.merge({
:enum => { :name => "enum" },
:set => { :name => "set" }
})
end
alias_method :native_database_types_without_enum, :native_database_types
alias_method :native_database_types, :native_database_types_with_enum

def type_to_sql_with_enum type, limit=nil, *args
if type.to_s == "enum" || type.to_s == "set"
"#{type}(#{quoted_comma_list limit})"
else
type_to_sql_without_enum type, limit, *args


if ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 1
def type_to_sql_with_enum(type, limit: nil, **args)
if type.to_s == "enum" || type.to_s == "set"
list = limit
if limit.is_a?(Hash)
list = limit[:limit]
end
"#{type}(#{quoted_comma_list(list)})"
else
type_to_sql_without_enum(type, limit: limit, **args)
end
end
else
def type_to_sql_with_enum(type, limit=nil, *args)
if type.to_s == "enum" || type.to_s == "set"
list = limit
if limit.is_a?(Hash)
list = limit[:limit]
end
"#{type}(#{quoted_comma_list(list)})"
else
type_to_sql_without_enum(type, limit, *args)
end
end
end
alias_method :type_to_sql_without_enum, :type_to_sql
alias_method :type_to_sql, :type_to_sql_with_enum

private

def quoted_comma_list list
list.to_a.map{|n| "'#{n}'"}.join(",")
end
Expand Down
17 changes: 13 additions & 4 deletions lib/connection_adapters/sqlite3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
module ActiveRecord
module ConnectionAdapters
class SQLite3Adapter < (defined?(SQLiteAdapter) ? SQLiteAdapter : AbstractAdapter)
def type_to_sql_with_enum type, limit=nil, *args
if type.to_s == "enum" || type.to_s == "set"
type, limit = :string, nil
if ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 1
def type_to_sql_with_enum(type, limit: nil, **args)
if type.to_s == "enum" || type.to_s == "set"
type, limit = :string, nil
end
type_to_sql_without_enum(type, limit: limit, **args)
end
else
def type_to_sql_with_enum(type, limit=nil, *args)
if type.to_s == "enum" || type.to_s == "set"
type, limit = :string, nil
end
type_to_sql_without_enum(type, limit, *args)
end
type_to_sql_without_enum type, limit, *args
end
alias_method :type_to_sql_without_enum, :type_to_sql
alias_method :type_to_sql, :type_to_sql_with_enum
Expand Down
9 changes: 7 additions & 2 deletions lib/native_enum/activerecord_enum_post42.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module ConnectionAdapters
if defined?(AbstractMysqlAdapter)
class AbstractMysqlAdapter
protected
def initialize_type_map_with_enum(m)
def initialize_type_map_with_enum(m = type_map)
initialize_without_enum(m)
register_enum_type(m, %r(^enum)i)
register_set_type(m, %r(^set)i)
Expand Down Expand Up @@ -55,9 +55,14 @@ def initialize(options = {})
@limit = options[:limit]
end

def type_cast_from_database(value)
# Deserialize value from the database
#
# See: https://github.com/rails/rails/blob/v5.0.7/activemodel/lib/active_model/type/value.rb#L15-L23
def deserialize(value)
value.split(",")
end
# deserialize used to be called type_cast_from_database before v5
alias_method :type_cast_from_database, :deserialize
end
end
end
3 changes: 2 additions & 1 deletion native_enum.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Gem::Specification.new do |s|
s.add_development_dependency "bundler"
s.add_development_dependency "mysql2", "~> 0.3.11"
s.add_development_dependency "sqlite3", "~>1.3.4"
s.add_development_dependency "rspec", "~> 3.1.0"
s.add_development_dependency "rspec", "~> 3"
s.add_development_dependency "appraisal"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
4 changes: 0 additions & 4 deletions spec/Gemfile.rails_3_0

This file was deleted.

4 changes: 0 additions & 4 deletions spec/Gemfile.rails_3_1

This file was deleted.

4 changes: 0 additions & 4 deletions spec/Gemfile.rails_3_2

This file was deleted.

4 changes: 0 additions & 4 deletions spec/Gemfile.rails_4_0

This file was deleted.

4 changes: 0 additions & 4 deletions spec/Gemfile.rails_4_1

This file was deleted.

4 changes: 0 additions & 4 deletions spec/Gemfile.rails_4_2

This file was deleted.

2 changes: 1 addition & 1 deletion spec/enum_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe "ENUM datatype" do
RSpec.describe "ENUM datatype" do

describe "schema dump", :db_support => true do
before { load_schema "enum_old" }
Expand Down
2 changes: 1 addition & 1 deletion spec/set_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe "SET datatype" do
RSpec.describe "SET datatype" do

describe "schema dump", :db_support => true do
before { load_schema "set_old" }
Expand Down
Loading