Skip to content

Commit

Permalink
Rails 6.1 Compatibility (#365)
Browse files Browse the repository at this point in the history
* silence deprecation warning from the rename of `Module#parent` to `Module#module_parent`

Rails 6 deprecated `Module#parent` and friends in rails/rails#34051, replacing them with a `module_*`-prefix naming scheme. This checks to see if `module_parent` is defined, using it if it's available.

* fix tests with hacky patch

* comments

* revert formatting changes

* Fully support Rails 6.1 in tests

* forgot to exclude several old versions of Ruby

* fix indentation in blocks

Big thanks to @andrew-newell

Co-authored-by: Alex Robbin <agrobbin@gmail.com>
  • Loading branch information
andrew-newell and agrobbin authored Feb 16, 2021
1 parent 0fbb1cd commit 5bf9f82
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ gemfile:
- gemfiles/Gemfile-rails.5.1.x
- gemfiles/Gemfile-rails.5.2.x
- gemfiles/Gemfile-rails.6.0.x
- gemfiles/Gemfile-rails.6.1.x
- gemfiles/Gemfile-rails.edge
before_install:
- gem install bundler -v '< 2'
Expand All @@ -26,14 +27,20 @@ matrix:
exclude:
- rvm: 2.2
gemfile: gemfiles/Gemfile-rails.6.0.x
- rvm: 2.2
gemfile: gemfiles/Gemfile-rails.6.1.x
- rvm: 2.2
gemfile: gemfiles/Gemfile-rails.edge
- rvm: 2.3
gemfile: gemfiles/Gemfile-rails.6.0.x
- rvm: 2.3
gemfile: gemfiles/Gemfile-rails.6.1.x
- rvm: 2.3
gemfile: gemfiles/Gemfile-rails.edge
- rvm: 2.4
gemfile: gemfiles/Gemfile-rails.6.0.x
- rvm: 2.4
gemfile: gemfiles/Gemfile-rails.6.1.x
- rvm: 2.4
gemfile: gemfiles/Gemfile-rails.edge
cache: bundler
21 changes: 9 additions & 12 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ if ENV["edge"]
gem "activerecord", :github => "rails/rails"
end


group :development, :test do
gem 'minitest', '~> 5.14.0'
end

group :development do
gem 'minitest', '5.10.1'
gem 'mocha'
gem "rake"
gem "yard"

platforms :ruby_21 do
gem "activerecord", "< 5.0"
gem "activesupport", "< 5.0"
end

platforms :ruby do
gem "sqlite3"
gem "activerecord", "< 7.0"
gem "activesupport", "< 7.0"
gem "sqlite3", '~> 1.4'
gem "redcarpet"

if RUBY_VERSION > "2.1.0"
Expand All @@ -30,8 +31,4 @@ group :development do
gem "activerecord-jdbcsqlite3-adapter"
gem "jruby-openssl", :require => false # Silence openssl warnings.
end
end

group :test do
gem 'minitest', '5.10.1'
end
end
21 changes: 21 additions & 0 deletions gemfiles/Gemfile-rails.6.1.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
source "http://rubygems.org"

gemspec :path => ".."

gem "activerecord", "~> 6.1.1"

group :development do
gem 'mocha'
gem "rake"
gem "yard"

platforms :ruby do
gem "sqlite3", '~> 1.4'
gem "redcarpet"
end

platforms :jruby do
gem "activerecord-jdbcsqlite3-adapter"
gem "jruby-openssl", :require => false # Silence openssl warnings.
end
end
8 changes: 7 additions & 1 deletion lib/rails_erd/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ def initialize(models = [], options = {})
# Returns the domain model name, which is the name of your Rails
# application or +nil+ outside of Rails.
def name
defined? Rails and Rails.application and Rails.application.class.parent.name
return unless defined?(Rails) && Rails.application

if Rails.application.class.respond_to?(:module_parent)
Rails.application.class.module_parent.name
else
Rails.application.class.parent.name
end
end

# Returns all entities of your domain model.
Expand Down
13 changes: 13 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
ActiveSupport::TestCase.test_order = :random
end

# Patch to make Rails 6.1 work.
module Kernel
# class_eval on an object acts like singleton_class.class_eval.
def class_eval(*args, &block)
singleton_class.class_eval(*args, &block)
end
end

class ActiveSupport::TestCase
include RailsERD

Expand Down Expand Up @@ -171,6 +179,11 @@ def name_to_object_symbol_pairs(name)

parts[1..-1].inject([[Object, parts.first.to_sym]]) do |pairs,string|
last_parent, last_child = pairs.last
# Fixes for Rails 6. No idea if this is actually correct as I can't decipher what the heck is going on in this
# code.
if last_child == :ActiveRecord || last_child == :primary
break []
end

break pairs unless last_parent.const_defined?(last_child)

Expand Down
21 changes: 10 additions & 11 deletions test/unit/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

class AttributeTest < ActiveSupport::TestCase
def with_native_limit(type, new_limit)
ActiveRecord::Base.connection.class_eval do
ActiveRecord::Base.connection.singleton_class.class_eval do
undef :native_database_types
define_method :native_database_types do
define_method(:native_database_types) do
super().tap do |types|
types[type][:limit] = new_limit
end
end
end
yield
ensure
ActiveRecord::Base.connection.class_eval do
ActiveRecord::Base.connection.singleton_class.class_eval do
undef :native_database_types
define_method :native_database_types do
define_method(:native_database_types) do
super()
end
end
Expand Down Expand Up @@ -266,14 +266,14 @@ def create_attribute(model, name)
end

test "limit should return nil for oddball column types that misuse the limit attribute" do
create_model "Business", :location => :integer
attribute = create_attribute(Business, "location")
attribute.column.class_eval do
define_method :limit do
create_model "Business", :location => :integer do
define_singleton_method :limit do
# https://github.com/voormedia/rails-erd/issues/21
{ :srid => 4326, :type => "point", :geographic => true }
end
end

attribute = create_attribute(Business, "location")
assert_nil attribute.limit
end

Expand Down Expand Up @@ -306,13 +306,12 @@ def create_attribute(model, name)
end

test "scale should return nil for oddball column types that misuse the scale attribute" do
create_model "Kobold", :size => :integer
attribute = create_attribute(Kobold, "size")
attribute.column.class_eval do
create_model "Kobold", :size => :integer do
define_method :scale do
1..5
end
end
attribute = create_attribute(Kobold, "size")
assert_nil attribute.scale
end
end

0 comments on commit 5bf9f82

Please sign in to comment.