From 7e9ee534e749e94ef73700d47646096423a24da2 Mon Sep 17 00:00:00 2001 From: Joel Moss Date: Thu, 31 Oct 2024 17:18:49 +0000 Subject: [PATCH] feat: Add support for ActiveModel::Attributes instance and class --- gemfiles/mongoid_7.0.gemfile | 2 +- gemfiles/mongoid_8.0.gemfile | 2 +- gemfiles/rails_6.1.gemfile | 4 ++-- gemfiles/rails_7.0.gemfile | 4 ++-- gemfiles/sequel_5.0.gemfile | 2 +- lib/amazing_print/ext/active_record.rb | 30 ++++++++++++++++++++++++++ spec/active_record_helper.rb | 7 ++++++ spec/ext/active_model_spec.rb | 25 ++++++++++++++++++++- 8 files changed, 68 insertions(+), 8 deletions(-) diff --git a/gemfiles/mongoid_7.0.gemfile b/gemfiles/mongoid_7.0.gemfile index dd3f51b..b1a4e84 100644 --- a/gemfiles/mongoid_7.0.gemfile +++ b/gemfiles/mongoid_7.0.gemfile @@ -5,11 +5,11 @@ source "https://rubygems.org" gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main gem "bigdecimal" gem "fakefs", "~> 1.2" -gem "mongoid", "~> 7.0.0" gem "nokogiri", "~> 1.10" gem "pry" gem "rspec", "~> 3.9" gem "rubocop", "~> 1.20" gem "rubocop-rspec", "~> 2.4" +gem "mongoid", "~> 7.0.0" gemspec path: "../" diff --git a/gemfiles/mongoid_8.0.gemfile b/gemfiles/mongoid_8.0.gemfile index d968d85..26ade9a 100644 --- a/gemfiles/mongoid_8.0.gemfile +++ b/gemfiles/mongoid_8.0.gemfile @@ -5,11 +5,11 @@ source "https://rubygems.org" gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main gem "bigdecimal" gem "fakefs", "~> 1.2" -gem "mongoid", "~> 8.0.0" gem "nokogiri", "~> 1.10" gem "pry" gem "rspec", "~> 3.9" gem "rubocop", "~> 1.20" gem "rubocop-rspec", "~> 2.4" +gem "mongoid", "~> 8.0.0" gemspec path: "../" diff --git a/gemfiles/rails_6.1.gemfile b/gemfiles/rails_6.1.gemfile index 95326c7..7093bee 100644 --- a/gemfiles/rails_6.1.gemfile +++ b/gemfiles/rails_6.1.gemfile @@ -2,16 +2,16 @@ source "https://rubygems.org" -gem "activerecord-jdbcsqlite3-adapter", "~> 61.0", platform: :jruby gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main gem "bigdecimal" gem "fakefs", "~> 1.2" gem "nokogiri", "~> 1.10" gem "pry" -gem "rails", "~> 6.1.0" gem "rspec", "~> 3.9" gem "rubocop", "~> 1.20" gem "rubocop-rspec", "~> 2.4" +gem "activerecord-jdbcsqlite3-adapter", "~> 61.0", platform: :jruby +gem "rails", "~> 6.1.0" gem "sqlite3", "~> 1.4", platform: :mri gemspec path: "../" diff --git a/gemfiles/rails_7.0.gemfile b/gemfiles/rails_7.0.gemfile index bde94a0..83b9f0e 100644 --- a/gemfiles/rails_7.0.gemfile +++ b/gemfiles/rails_7.0.gemfile @@ -2,16 +2,16 @@ source "https://rubygems.org" -gem "activerecord-jdbcsqlite3-adapter", "~> 70.0", platform: :jruby gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main gem "bigdecimal" gem "fakefs", "~> 1.2" gem "nokogiri", "~> 1.10" gem "pry" -gem "rails", "~> 7.0.0" gem "rspec", "~> 3.9" gem "rubocop", "~> 1.20" gem "rubocop-rspec", "~> 2.4" +gem "activerecord-jdbcsqlite3-adapter", "~> 70.0", platform: :jruby +gem "rails", "~> 7.0.0" gem "sqlite3", "~> 1.4", platform: :mri gemspec path: "../" diff --git a/gemfiles/sequel_5.0.gemfile b/gemfiles/sequel_5.0.gemfile index a7d2dc3..b84cb84 100644 --- a/gemfiles/sequel_5.0.gemfile +++ b/gemfiles/sequel_5.0.gemfile @@ -5,12 +5,12 @@ source "https://rubygems.org" gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main gem "bigdecimal" gem "fakefs", "~> 1.2" -gem "jdbc-sqlite3", platform: :jruby gem "nokogiri", "~> 1.10" gem "pry" gem "rspec", "~> 3.9" gem "rubocop", "~> 1.20" gem "rubocop-rspec", "~> 2.4" +gem "jdbc-sqlite3", platform: :jruby gem "sequel", "~> 5.0" gem "sqlite3", "~> 1.4", platform: :mri diff --git a/lib/amazing_print/ext/active_record.rb b/lib/amazing_print/ext/active_record.rb index 082a187..17db03d 100644 --- a/lib/amazing_print/ext/active_record.rb +++ b/lib/amazing_print/ext/active_record.rb @@ -20,6 +20,10 @@ def cast_with_active_record(object, type) if object.is_a?(::ActiveRecord::Base) cast = :active_record_instance + elsif object.is_a?(Class) && object.include?(::ActiveModel::Attributes) + cast = :active_model_class + elsif object.is_a?(::ActiveModel::Attributes) + cast = :active_model_instance elsif object.is_a?(::ActiveModel::Errors) cast = :active_model_error elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base) @@ -71,6 +75,32 @@ def awesome_active_record_class(object) [awesome_simple("class #{object} < #{object.superclass}", :class), awesome_hash(data)].join(' ') end + # Format ActiveModel instance object. + #------------------------------------------------------------------------------ + def awesome_active_model_instance(object) + return object.inspect unless defined?(::ActiveSupport::OrderedHash) + return awesome_object(object) if @options[:raw] + + data = object.attributes.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash| + hash[c.first.to_sym] = c.last + end + + [awesome_simple(object.to_s, :active_model_instance), awesome_hash(data)].join(' ') + end + + # Format ActiveModel class object. + #------------------------------------------------------------------------------ + def awesome_active_model_class(object) + return object.inspect unless defined?(::ActiveSupport::OrderedHash) + return awesome_class(object) if @options[:raw] + + data = object.attribute_types.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash| + hash[c.first.to_sym] = c.last.type + end + + [awesome_simple("class #{object} < #{object.superclass}", :class), awesome_hash(data)].join(' ') + end + # Format ActiveModel error object. #------------------------------------------------------------------------------ def awesome_active_model_error(object) diff --git a/spec/active_record_helper.rb b/spec/active_record_helper.rb index 81edde2..8815671 100644 --- a/spec/active_record_helper.rb +++ b/spec/active_record_helper.rb @@ -40,4 +40,11 @@ def attributes { 'name' => name } end end + + class ActiveModelModel + include ::ActiveModel::Model + include ::ActiveModel::Attributes + attribute :name, :string + attribute :rank, :integer, default: 0 + end end diff --git a/spec/ext/active_model_spec.rb b/spec/ext/active_model_spec.rb index fd95d84..37405e9 100644 --- a/spec/ext/active_model_spec.rb +++ b/spec/ext/active_model_spec.rb @@ -3,11 +3,34 @@ require 'spec_helper' require 'active_record_helper' -RSpec.describe 'ActiveModel::Errors formatting', skip: -> { !ExtVerifier.has_rails? }.call do +RSpec.describe 'ActiveModel formatting', skip: -> { !ExtVerifier.has_rails? }.call do before do @ap = AmazingPrint::Inspector.new(plain: true) end + describe 'ActiveModel class' do + it 'prints the class' do + expect(@ap.awesome(ActiveModelModel)).to eq <<~PRINT.strip + class ActiveModelModel < Object { + :name => :string, + :rank => :integer + } + PRINT + end + end + + describe 'ActiveModel instance' do + it 'prints the instance' do + model = ActiveModelModel.new(name: 'John', rank: 1) + expect(@ap.awesome(model)).to be_similar_to <<~PRINT.strip + # { + :name => "John", + :rank => 1 + } + PRINT + end + end + it 'formats active_model_errors properly' do model = TableFreeModel.new model.errors.add(:name, "can't be blank")