her icon indicating copy to clipboard operation
her copied to clipboard

Calling inspect with associated objects results in recursion and unnecessarily long display output.

Open jayhancock opened this issue 8 years ago • 0 comments

The #inspect method uses #attributes, and #attributes includes associations. Also, the associated object includes the parent in it's #attributes.

When using pry, it blows up because it uses inspect on object's results by default. Or any inspect on complex objects.

Example:

class Foo
  include Her::Model
  has_many :bars
  has_many :beers
end

class Bar
  include Her::Model
end

f = Foo.find(some_id)
f.inspect =>
#<Foo(foo/some_id) attribute_foo=1 bars=[
#<Bar(bars) attribute_bar=0 foo=#<Foo(foo/some_id) attribute_foo=1 
bars=[...] 
beers=[(full rendering of beers, including foo= but not bars again)]>, 
...
]
beers=[same extended recursion of parent multiple times with lots of bars and [...] for recursed beers]
>

Currently, the #<Foo... is repeated, including all of it's non-bars attributes. bars= is replaced with a [...] but only due to ruby.

This blows up the output size when you have multiple associations and multiple data points in those outputs. Each subobject repeats the parent with all siblings and only it's child is not repeated.

This results in a huge output and load times for a relatively complicated set of objects.

I think we should maintain a stack during inspect and detect the recursion. I will submit a PR from code I've been working with soon.

jayhancock avatar Jun 08 '16 20:06 jayhancock