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

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

Merged
merged 1 commit into from
Aug 2, 2016
Merged
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
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
21 changes: 21 additions & 0 deletions spec/ext/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@
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
before do
Expand Down