Skip to content

Commit 5bf9f82

Browse files
andrew-newellagrobbin
andauthored
Rails 6.1 Compatibility (#365)
* 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>
1 parent 0fbb1cd commit 5bf9f82

File tree

6 files changed

+67
-24
lines changed

6 files changed

+67
-24
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ gemfile:
1212
- gemfiles/Gemfile-rails.5.1.x
1313
- gemfiles/Gemfile-rails.5.2.x
1414
- gemfiles/Gemfile-rails.6.0.x
15+
- gemfiles/Gemfile-rails.6.1.x
1516
- gemfiles/Gemfile-rails.edge
1617
before_install:
1718
- gem install bundler -v '< 2'
@@ -26,14 +27,20 @@ matrix:
2627
exclude:
2728
- rvm: 2.2
2829
gemfile: gemfiles/Gemfile-rails.6.0.x
30+
- rvm: 2.2
31+
gemfile: gemfiles/Gemfile-rails.6.1.x
2932
- rvm: 2.2
3033
gemfile: gemfiles/Gemfile-rails.edge
3134
- rvm: 2.3
3235
gemfile: gemfiles/Gemfile-rails.6.0.x
36+
- rvm: 2.3
37+
gemfile: gemfiles/Gemfile-rails.6.1.x
3338
- rvm: 2.3
3439
gemfile: gemfiles/Gemfile-rails.edge
3540
- rvm: 2.4
3641
gemfile: gemfiles/Gemfile-rails.6.0.x
42+
- rvm: 2.4
43+
gemfile: gemfiles/Gemfile-rails.6.1.x
3744
- rvm: 2.4
3845
gemfile: gemfiles/Gemfile-rails.edge
3946
cache: bundler

Gemfile

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ if ENV["edge"]
66
gem "activerecord", :github => "rails/rails"
77
end
88

9+
10+
group :development, :test do
11+
gem 'minitest', '~> 5.14.0'
12+
end
13+
914
group :development do
10-
gem 'minitest', '5.10.1'
1115
gem 'mocha'
1216
gem "rake"
1317
gem "yard"
1418

15-
platforms :ruby_21 do
16-
gem "activerecord", "< 5.0"
17-
gem "activesupport", "< 5.0"
18-
end
19-
2019
platforms :ruby do
21-
gem "sqlite3"
20+
gem "activerecord", "< 7.0"
21+
gem "activesupport", "< 7.0"
22+
gem "sqlite3", '~> 1.4'
2223
gem "redcarpet"
2324

2425
if RUBY_VERSION > "2.1.0"
@@ -30,8 +31,4 @@ group :development do
3031
gem "activerecord-jdbcsqlite3-adapter"
3132
gem "jruby-openssl", :require => false # Silence openssl warnings.
3233
end
33-
end
34-
35-
group :test do
36-
gem 'minitest', '5.10.1'
37-
end
34+
end

gemfiles/Gemfile-rails.6.1.x

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
source "http://rubygems.org"
2+
3+
gemspec :path => ".."
4+
5+
gem "activerecord", "~> 6.1.1"
6+
7+
group :development do
8+
gem 'mocha'
9+
gem "rake"
10+
gem "yard"
11+
12+
platforms :ruby do
13+
gem "sqlite3", '~> 1.4'
14+
gem "redcarpet"
15+
end
16+
17+
platforms :jruby do
18+
gem "activerecord-jdbcsqlite3-adapter"
19+
gem "jruby-openssl", :require => false # Silence openssl warnings.
20+
end
21+
end

lib/rails_erd/domain.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ def initialize(models = [], options = {})
4949
# Returns the domain model name, which is the name of your Rails
5050
# application or +nil+ outside of Rails.
5151
def name
52-
defined? Rails and Rails.application and Rails.application.class.parent.name
52+
return unless defined?(Rails) && Rails.application
53+
54+
if Rails.application.class.respond_to?(:module_parent)
55+
Rails.application.class.module_parent.name
56+
else
57+
Rails.application.class.parent.name
58+
end
5359
end
5460

5561
# Returns all entities of your domain model.

test/test_helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
ActiveSupport::TestCase.test_order = :random
1717
end
1818

19+
# Patch to make Rails 6.1 work.
20+
module Kernel
21+
# class_eval on an object acts like singleton_class.class_eval.
22+
def class_eval(*args, &block)
23+
singleton_class.class_eval(*args, &block)
24+
end
25+
end
26+
1927
class ActiveSupport::TestCase
2028
include RailsERD
2129

@@ -171,6 +179,11 @@ def name_to_object_symbol_pairs(name)
171179

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

175188
break pairs unless last_parent.const_defined?(last_child)
176189

test/unit/attribute_test.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
class AttributeTest < ActiveSupport::TestCase
55
def with_native_limit(type, new_limit)
6-
ActiveRecord::Base.connection.class_eval do
6+
ActiveRecord::Base.connection.singleton_class.class_eval do
77
undef :native_database_types
8-
define_method :native_database_types do
8+
define_method(:native_database_types) do
99
super().tap do |types|
1010
types[type][:limit] = new_limit
1111
end
1212
end
1313
end
1414
yield
1515
ensure
16-
ActiveRecord::Base.connection.class_eval do
16+
ActiveRecord::Base.connection.singleton_class.class_eval do
1717
undef :native_database_types
18-
define_method :native_database_types do
18+
define_method(:native_database_types) do
1919
super()
2020
end
2121
end
@@ -266,14 +266,14 @@ def create_attribute(model, name)
266266
end
267267

268268
test "limit should return nil for oddball column types that misuse the limit attribute" do
269-
create_model "Business", :location => :integer
270-
attribute = create_attribute(Business, "location")
271-
attribute.column.class_eval do
272-
define_method :limit do
269+
create_model "Business", :location => :integer do
270+
define_singleton_method :limit do
273271
# https://github.com/voormedia/rails-erd/issues/21
274272
{ :srid => 4326, :type => "point", :geographic => true }
275273
end
276274
end
275+
276+
attribute = create_attribute(Business, "location")
277277
assert_nil attribute.limit
278278
end
279279

@@ -306,13 +306,12 @@ def create_attribute(model, name)
306306
end
307307

308308
test "scale should return nil for oddball column types that misuse the scale attribute" do
309-
create_model "Kobold", :size => :integer
310-
attribute = create_attribute(Kobold, "size")
311-
attribute.column.class_eval do
309+
create_model "Kobold", :size => :integer do
312310
define_method :scale do
313311
1..5
314312
end
315313
end
314+
attribute = create_attribute(Kobold, "size")
316315
assert_nil attribute.scale
317316
end
318317
end

0 commit comments

Comments
 (0)