Skip to content

Commit

Permalink
Merge pull request #68 from amazing-print/rails-6.1
Browse files Browse the repository at this point in the history
Add support for Rails 6.1
  • Loading branch information
paddor authored Jan 25, 2021
2 parents 238aa29 + 936a3a5 commit b2b53e9
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 33 deletions.
19 changes: 6 additions & 13 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ appraise 'rails-6.0' do
gem 'sqlite3', platform: :mri
end

appraise 'rails-6.1' do
gem 'activerecord-jdbcsqlite3-adapter', '~> 61.0', platform: :jruby
gem 'rails', '~> 6.1.0'
gem 'sqlite3', platform: :mri
end

appraise 'mongoid-4.0' do
# https://github.com/rails/rails/issues/34822#issuecomment-570670516
gem 'bigdecimal', '~> 1.4', platforms: :mri
Expand Down Expand Up @@ -54,16 +60,3 @@ appraise 'sequel-5.0' do
gem 'sequel', '~> 5.0'
gem 'sqlite3', platform: :mri
end

# appraise 'ripple' do
# gem 'tzinfo'
# gem 'ripple'
# end
#
# appraise 'nobrainer' do
# gem 'nobrainer'
#
# # When activesupport 5 was released, it required ruby 2.2.2 as a minimum.
# # Locking this down to 4.2.6 allows our Ruby 1.9 tests to keep working.
# gem 'activesupport', '4.2.6', :platforms => :ruby_19
# end
9 changes: 9 additions & 0 deletions gemfiles/rails_6.1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "activerecord-jdbcsqlite3-adapter", "~> 61.0", platform: :jruby
gem "rails", "~> 6.1.0"
gem "sqlite3", platform: :mri

gemspec path: "../"
16 changes: 4 additions & 12 deletions lib/amazing_print/ext/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,11 @@ def awesome_active_model_error(object)
return object.inspect unless defined?(::ActiveSupport::OrderedHash)
return awesome_object(object) if @options[:raw]

object_dump = object.marshal_dump.first
data = if object_dump.class.try(:column_names) != object_dump.attributes.keys
object_dump.attributes
else
object_dump.class.column_names.each_with_object(::ActiveSupport::OrderedHash.new) do |name, hash|
if object_dump.has_attribute?(name) || object_dump.new_record?
value = object_dump.respond_to?(name) ? object_dump.send(name) : object_dump.read_attribute(name)
hash[name.to_sym] = value
end
end
end
data = object.instance_variable_get('@base')
.attributes
.merge(details: object.details.to_h,
messages: object.messages.to_h.transform_values(&:to_a))

data.merge!({ details: object.details, messages: object.messages })
[object.to_s, awesome_hash(data)].join(' ')
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/ext/action_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
end

it 'should format as an object' do
expect(inspector.send(:awesome, parameters)).to eq(
'<ActionController::Parameters {"post"=>{"id"=>1, "content"=>"Some"}} permitted: false>'
expect(inspector.send(:awesome, parameters)).to match(
/\A#?<ActionController::Parameters {"post"=>{"id"=>1, "content"=>"Some"}} permitted: false>\z/
)
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/ext/action_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

require 'spec_helper'

RSpec.describe 'AmazingPrint ActionView extensions', skip: -> { !ExtVerifier.has_rails? }.call do
RSpec.describe 'AmazingPrint ActionView extensions',
skip: -> { !ExtVerifier.has_rails? || ActiveRecord::VERSION::STRING >= '6.1' }.call do
before do
@view = ActionView::Base.new
end
Expand Down
12 changes: 8 additions & 4 deletions spec/ext/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@
out = @ap.awesome(@diana)

raw_object_string =
if activerecord_6_0?
if activerecord_6_1?
ActiveRecordData.raw_6_1_diana
elsif activerecord_6_0?
ActiveRecordData.raw_6_0_diana
elsif activerecord_5_2?
ActiveRecordData.raw_5_2_diana
Expand All @@ -154,7 +156,7 @@
end
end

if RUBY_PLATFORM == 'java'
if RUBY_PLATFORM == 'java' && !activerecord_6_1?
raw_object_string.gsub!(
'ActiveRecord::ConnectionAdapters::SQLite3Adapter::SQLite3Integer',
'ArJdbc::SQLite3::SQLite3Integer'
Expand All @@ -168,7 +170,9 @@
out = @ap.awesome([@diana, @laura])

raw_object_string =
if activerecord_6_0?
if activerecord_6_1?
ActiveRecordData.raw_6_1_multi
elsif activerecord_6_0?
ActiveRecordData.raw_6_0_multi
elsif activerecord_5_2?
ActiveRecordData.raw_5_2_multi
Expand All @@ -194,7 +198,7 @@
end
end

if RUBY_PLATFORM == 'java'
if RUBY_PLATFORM == 'java' && !activerecord_6_1?
raw_object_string.gsub!(
'ActiveRecord::ConnectionAdapters::SQLite3Adapter::SQLite3Integer',
'ArJdbc::SQLite3::SQLite3Integer'
Expand Down
11 changes: 10 additions & 1 deletion spec/ext/active_support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
require 'spec_helper'

RSpec.describe 'AmazingPrint::ActiveSupport', skip: -> { !ExtVerifier.has_rails? }.call do
let(:expected_ar_time_str) do
if activerecord_6_1?
'15:30:45.000000000'
else
'15:30:45'
end
end

before do
@ap = AmazingPrint::Inspector.new
end

it 'should format ActiveSupport::TimeWithZone as regular Time' do
Time.zone = 'Eastern Time (US & Canada)'
time = Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone
expect(@ap.send(:awesome, time)).to eq("\e[0;32mSat, 10 Feb 2007 15:30:45 EST -05:00\e[0m")
expect(@ap.send(:awesome, time))
.to eq("\e[0;32mSat, 10 Feb 2007 #{expected_ar_time_str} EST -05:00\e[0m")
end

it 'should format HashWithIndifferentAccess as regular Hash' do
Expand Down
109 changes: 109 additions & 0 deletions spec/support/active_record_data/6_1_diana.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#<User:placeholder_id
@_start_transaction_state = nil,
@association_cache = {},
@destroyed = false,
@marked_for_destruction = false,
@new_record = true,
@previously_new_record = false,
@primary_key = "id",
@readonly = false,
@strict_loading = false,
attr_accessor :attributes = #<ActiveModel::AttributeSet:placeholder_id
@attributes = {
"admin" => #<ActiveModel::Attribute::FromUser:placeholder_id
@original_attribute = #<ActiveModel::Attribute::FromDatabase:placeholder_id
@original_attribute = nil,
attr_reader :name = "admin",
attr_reader :type = #<ActiveModel::Type::Boolean:placeholder_id
attr_reader :limit = nil,
attr_reader :precision = nil,
attr_reader :scale = nil
>,
attr_reader :value_before_type_cast = nil
>,
attr_reader :name = "admin",
attr_reader :type = #<ActiveModel::Type::Boolean:placeholder_id
attr_reader :limit = nil,
attr_reader :precision = nil,
attr_reader :scale = nil
>,
attr_reader :value_before_type_cast = false
>,
"created_at" => #<ActiveModel::Attribute::FromUser:placeholder_id
@original_attribute = #<ActiveModel::Attribute::FromDatabase:placeholder_id
@original_attribute = nil,
attr_reader :name = "created_at",
attr_reader :type = #<ActiveRecord::Type::DateTime:placeholder_id
attr_reader :limit = nil,
attr_reader :precision = nil,
attr_reader :scale = nil
>,
attr_reader :value_before_type_cast = nil
>,
attr_reader :name = "created_at",
attr_reader :type = #<ActiveRecord::Type::DateTime:placeholder_id
attr_reader :limit = nil,
attr_reader :precision = nil,
attr_reader :scale = nil
>,
attr_reader :value_before_type_cast = "1992-10-10 12:30:00"
>,
"id" => #<ActiveModel::Attribute::FromDatabase:placeholder_id
@original_attribute = nil,
attr_reader :name = "id",
attr_reader :type = #<ActiveRecord::ConnectionAdapters::SQLite3Adapter::SQLite3Integer:placeholder_id
@range = -9223372036854775808...9223372036854775808,
attr_reader :limit = nil,
attr_reader :precision = nil,
attr_reader :scale = nil
>,
attr_reader :value_before_type_cast = nil
>,
"name" => #<ActiveModel::Attribute::FromUser:placeholder_id
@original_attribute = #<ActiveModel::Attribute::FromDatabase:placeholder_id
@original_attribute = nil,
attr_reader :name = "name",
attr_reader :type = #<ActiveModel::Type::String:placeholder_id
@false = "f",
@true = "t",
attr_reader :limit = nil,
attr_reader :precision = nil,
attr_reader :scale = nil
>,
attr_reader :value_before_type_cast = nil
>,
attr_reader :name = "name",
attr_reader :type = #<ActiveModel::Type::String:placeholder_id
@false = "f",
@true = "t",
attr_reader :limit = nil,
attr_reader :precision = nil,
attr_reader :scale = nil
>,
attr_reader :value_before_type_cast = "Diana"
>,
"rank" => #<ActiveModel::Attribute::FromUser:placeholder_id
@original_attribute = #<ActiveModel::Attribute::FromDatabase:placeholder_id
@original_attribute = nil,
attr_reader :name = "rank",
attr_reader :type = #<ActiveRecord::ConnectionAdapters::SQLite3Adapter::SQLite3Integer:placeholder_id
@range = -9223372036854775808...9223372036854775808,
attr_reader :limit = nil,
attr_reader :precision = nil,
attr_reader :scale = nil
>,
attr_reader :value_before_type_cast = nil
>,
attr_reader :name = "rank",
attr_reader :type = #<ActiveRecord::ConnectionAdapters::SQLite3Adapter::SQLite3Integer:placeholder_id
@range = -9223372036854775808...9223372036854775808,
attr_reader :limit = nil,
attr_reader :precision = nil,
attr_reader :scale = nil
>,
attr_reader :value_before_type_cast = 1
>
}
>,
attr_accessor :destroyed_by_association = nil
>
Loading

0 comments on commit b2b53e9

Please sign in to comment.