1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

ActiveRecord awesome #joins: now shows the #select'ed columns

This commit is contained in:
Adriano Mitre 2015-12-01 20:38:15 -02:00
parent 1a9d732dfa
commit f07a052aef
4 changed files with 40 additions and 8 deletions

View file

@ -1,4 +1,5 @@
## master (unreleased) ## master (unreleased)
- ActiveRecord: #joins now show the columns #select'ed [@adrianomitre] - [#211]
- Handles NoMethodError for IRB implicit `ai` [@jtnegrotto] - [#212] - Handles NoMethodError for IRB implicit `ai` [@jtnegrotto] - [#212]
## 1.7.0 ## 1.7.0

View file

@ -42,13 +42,17 @@ module AwesomePrint
return object.inspect if !defined?(::ActiveSupport::OrderedHash) return object.inspect if !defined?(::ActiveSupport::OrderedHash)
return awesome_object(object) if @options[:raw] return awesome_object(object) if @options[:raw]
data = object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name| data = if object.class.column_names != object.attributes.keys
object.attributes
else
object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
if object.has_attribute?(name) || object.new_record? if object.has_attribute?(name) || object.new_record?
value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name) value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
hash[name.to_sym] = value hash[name.to_sym] = value
end end
hash hash
end end
end
"#{object} " << awesome_hash(data) "#{object} " << awesome_hash(data)
end end

View file

@ -18,7 +18,13 @@ if ExtVerifier.has_rails?
t.datetime :created_at t.datetime :created_at
end end
# Create models ActiveRecord::Migration.create_table :emails do |t|
class User < ActiveRecord::Base; end t.references :user
class SubUser < User; end t.string :email_address
end
# Create models
class User < ActiveRecord::Base; has_many :emails; end
class SubUser < User; end
class Email < ActiveRecord::Base; belongs_to :user; end
end end

View file

@ -98,6 +98,27 @@ RSpec.describe "AwesomePrint/ActiveRecord", skip: ->{ !ExtVerifier.has_rails? }.
end end
end end
describe 'Linked records (joins)' do
before do
@ap = AwesomePrint::Inspector.new(:plain => true)
end
it 'should show the entire record' do
e = Email.create(:email_address => 'foo@bar.com')
u = User.last
u.emails << e
email_record = User.joins(:emails).select('users.id, emails.email_address').last
out = @ap.awesome(email_record)
raw_object_string = <<-EOS.strip
#<User:placeholder_id> {
"id" => #{u.id},
"email_address" => "#{e.email_address}"
}
EOS
expect(out).to be_similar_to(raw_object_string)
end
end
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
describe "ActiveRecord instance (raw)" do describe "ActiveRecord instance (raw)" do
before do before do