Skip to content

Commit

Permalink
ActiveRecord awesome #joins: now shows the #select'ed columns
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianomitre committed Aug 1, 2016
1 parent 65dd183 commit 7ab9462
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## master (unreleased)
- ActiveRecord: #joins now show the columns #select'ed [@adrianomitre] - [#211]
- Handles NoMethodError for IRB implicit `ai` [@jtnegrotto] - [#212]

## 1.7.0
Expand Down
18 changes: 11 additions & 7 deletions lib/awesome_print/ext/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ def awesome_active_record_instance(object)
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
return awesome_object(object) if @options[:raw]

data = object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
if object.has_attribute?(name) || object.new_record?
value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
hash[name.to_sym] = value
end
hash
end
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?
value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
hash[name.to_sym] = value
end
hash
end
end
"#{object} " << awesome_hash(data)
end

Expand Down
8 changes: 7 additions & 1 deletion spec/active_record_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ def self.env
t.datetime :created_at
end

ActiveRecord::Migration.create_table :emails do |t|
t.references :user
t.string :email_address
end

# Create models
class User < ActiveRecord::Base; end
class User < ActiveRecord::Base; has_many :emails; end
class SubUser < User; end
class Email < ActiveRecord::Base; belongs_to :user; end
end
20 changes: 20 additions & 0 deletions spec/ext/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@
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')
User.last.emails << e
email_record = User.joins(:emails).select('emails.email_address').last
out = @ap.awesome(email_record)
raw_object_string = <<-EOS.strip
#<User:placeholder_id> {
"id" => nil,
"email_address" => "#{e.email_address}"
}
EOS
expect(out).to be_similar_to(raw_object_string)
end
end

#------------------------------------------------------------------------------
describe "ActiveRecord instance (raw)" do
before do
Expand Down

0 comments on commit 7ab9462

Please sign in to comment.