Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for ActiveModel::Attributes instance and class #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gemfiles/mongoid_7.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
2 changes: 1 addition & 1 deletion gemfiles/mongoid_8.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
4 changes: 2 additions & 2 deletions gemfiles/rails_6.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
4 changes: 2 additions & 2 deletions gemfiles/rails_7.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
2 changes: 1 addition & 1 deletion gemfiles/sequel_5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions lib/amazing_print/ext/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions spec/active_record_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 24 additions & 1 deletion spec/ext/active_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#<ActiveModelModel:placeholder_id> {
: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")
Expand Down