Skip to content

Commit 7e9ee53

Browse files
committed
feat: Add support for ActiveModel::Attributes instance and class
1 parent ce1097d commit 7e9ee53

File tree

8 files changed

+68
-8
lines changed

8 files changed

+68
-8
lines changed

gemfiles/mongoid_7.0.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ source "https://rubygems.org"
55
gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main
66
gem "bigdecimal"
77
gem "fakefs", "~> 1.2"
8-
gem "mongoid", "~> 7.0.0"
98
gem "nokogiri", "~> 1.10"
109
gem "pry"
1110
gem "rspec", "~> 3.9"
1211
gem "rubocop", "~> 1.20"
1312
gem "rubocop-rspec", "~> 2.4"
13+
gem "mongoid", "~> 7.0.0"
1414

1515
gemspec path: "../"

gemfiles/mongoid_8.0.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ source "https://rubygems.org"
55
gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main
66
gem "bigdecimal"
77
gem "fakefs", "~> 1.2"
8-
gem "mongoid", "~> 8.0.0"
98
gem "nokogiri", "~> 1.10"
109
gem "pry"
1110
gem "rspec", "~> 3.9"
1211
gem "rubocop", "~> 1.20"
1312
gem "rubocop-rspec", "~> 2.4"
13+
gem "mongoid", "~> 8.0.0"
1414

1515
gemspec path: "../"

gemfiles/rails_6.1.gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
source "https://rubygems.org"
44

5-
gem "activerecord-jdbcsqlite3-adapter", "~> 61.0", platform: :jruby
65
gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main
76
gem "bigdecimal"
87
gem "fakefs", "~> 1.2"
98
gem "nokogiri", "~> 1.10"
109
gem "pry"
11-
gem "rails", "~> 6.1.0"
1210
gem "rspec", "~> 3.9"
1311
gem "rubocop", "~> 1.20"
1412
gem "rubocop-rspec", "~> 2.4"
13+
gem "activerecord-jdbcsqlite3-adapter", "~> 61.0", platform: :jruby
14+
gem "rails", "~> 6.1.0"
1515
gem "sqlite3", "~> 1.4", platform: :mri
1616

1717
gemspec path: "../"

gemfiles/rails_7.0.gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
source "https://rubygems.org"
44

5-
gem "activerecord-jdbcsqlite3-adapter", "~> 70.0", platform: :jruby
65
gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main
76
gem "bigdecimal"
87
gem "fakefs", "~> 1.2"
98
gem "nokogiri", "~> 1.10"
109
gem "pry"
11-
gem "rails", "~> 7.0.0"
1210
gem "rspec", "~> 3.9"
1311
gem "rubocop", "~> 1.20"
1412
gem "rubocop-rspec", "~> 2.4"
13+
gem "activerecord-jdbcsqlite3-adapter", "~> 70.0", platform: :jruby
14+
gem "rails", "~> 7.0.0"
1515
gem "sqlite3", "~> 1.4", platform: :mri
1616

1717
gemspec path: "../"

gemfiles/sequel_5.0.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ source "https://rubygems.org"
55
gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main
66
gem "bigdecimal"
77
gem "fakefs", "~> 1.2"
8-
gem "jdbc-sqlite3", platform: :jruby
98
gem "nokogiri", "~> 1.10"
109
gem "pry"
1110
gem "rspec", "~> 3.9"
1211
gem "rubocop", "~> 1.20"
1312
gem "rubocop-rspec", "~> 2.4"
13+
gem "jdbc-sqlite3", platform: :jruby
1414
gem "sequel", "~> 5.0"
1515
gem "sqlite3", "~> 1.4", platform: :mri
1616

lib/amazing_print/ext/active_record.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def cast_with_active_record(object, type)
2020

2121
if object.is_a?(::ActiveRecord::Base)
2222
cast = :active_record_instance
23+
elsif object.is_a?(Class) && object.include?(::ActiveModel::Attributes)
24+
cast = :active_model_class
25+
elsif object.is_a?(::ActiveModel::Attributes)
26+
cast = :active_model_instance
2327
elsif object.is_a?(::ActiveModel::Errors)
2428
cast = :active_model_error
2529
elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
@@ -71,6 +75,32 @@ def awesome_active_record_class(object)
7175
[awesome_simple("class #{object} < #{object.superclass}", :class), awesome_hash(data)].join(' ')
7276
end
7377

78+
# Format ActiveModel instance object.
79+
#------------------------------------------------------------------------------
80+
def awesome_active_model_instance(object)
81+
return object.inspect unless defined?(::ActiveSupport::OrderedHash)
82+
return awesome_object(object) if @options[:raw]
83+
84+
data = object.attributes.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
85+
hash[c.first.to_sym] = c.last
86+
end
87+
88+
[awesome_simple(object.to_s, :active_model_instance), awesome_hash(data)].join(' ')
89+
end
90+
91+
# Format ActiveModel class object.
92+
#------------------------------------------------------------------------------
93+
def awesome_active_model_class(object)
94+
return object.inspect unless defined?(::ActiveSupport::OrderedHash)
95+
return awesome_class(object) if @options[:raw]
96+
97+
data = object.attribute_types.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
98+
hash[c.first.to_sym] = c.last.type
99+
end
100+
101+
[awesome_simple("class #{object} < #{object.superclass}", :class), awesome_hash(data)].join(' ')
102+
end
103+
74104
# Format ActiveModel error object.
75105
#------------------------------------------------------------------------------
76106
def awesome_active_model_error(object)

spec/active_record_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ def attributes
4040
{ 'name' => name }
4141
end
4242
end
43+
44+
class ActiveModelModel
45+
include ::ActiveModel::Model
46+
include ::ActiveModel::Attributes
47+
attribute :name, :string
48+
attribute :rank, :integer, default: 0
49+
end
4350
end

spec/ext/active_model_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,34 @@
33
require 'spec_helper'
44
require 'active_record_helper'
55

6-
RSpec.describe 'ActiveModel::Errors formatting', skip: -> { !ExtVerifier.has_rails? }.call do
6+
RSpec.describe 'ActiveModel formatting', skip: -> { !ExtVerifier.has_rails? }.call do
77
before do
88
@ap = AmazingPrint::Inspector.new(plain: true)
99
end
1010

11+
describe 'ActiveModel class' do
12+
it 'prints the class' do
13+
expect(@ap.awesome(ActiveModelModel)).to eq <<~PRINT.strip
14+
class ActiveModelModel < Object {
15+
:name => :string,
16+
:rank => :integer
17+
}
18+
PRINT
19+
end
20+
end
21+
22+
describe 'ActiveModel instance' do
23+
it 'prints the instance' do
24+
model = ActiveModelModel.new(name: 'John', rank: 1)
25+
expect(@ap.awesome(model)).to be_similar_to <<~PRINT.strip
26+
#<ActiveModelModel:placeholder_id> {
27+
:name => "John",
28+
:rank => 1
29+
}
30+
PRINT
31+
end
32+
end
33+
1134
it 'formats active_model_errors properly' do
1235
model = TableFreeModel.new
1336
model.errors.add(:name, "can't be blank")

0 commit comments

Comments
 (0)