This repository has been archived by the owner on Oct 19, 2018. It is now read-only.
add to_key
method to object
#244
Labels
Milestone
Components can be provided a key, via the special
key
parameter that ensures when rendering lists of components the ordering does not change, and speeds up the rendering of the list.typically you can use some data associated with the values being displayed (like
record.id
above) or the ruby object_id, or some other unique identifier depending on the contents of the list item.For HyperModels (and probably other places too) this can be difficult. The above example for instance only works if all the records are saved (i.e. have ids). You can't use
record.object_id
since there is no guarantee that on the next rendering the record instance will be the same object_id, so you have userecord.backing_record.object_id
which is gross and relies on knowing the inards of HyperModel.The solution is to add a
to_key
method to the base rubyObject
class and to have the key parameter automatically haveto_key
applied to it. The defaultto_key
method will returnself.object_id
, but this can be overwritten as needed. I.e. ActiveRecord::Base can override asself.backing_record.object_id
.Now we can write
DisplayRecord(record: record, key: record)
shorter, sweeter, and works.Also in keeping with POLS, strings, symbols, booleans and number objects will return themselves.
Produces
run it!
As well as adding the method to the classes per above, you also have to add these lines here
The text was updated successfully, but these errors were encountered: