DmitryTsepelev / store_model

Work with JSON-backed attributes as ActiveRecord-ish models
MIT License
1.09k stars 90 forks source link

Add parent tracking for array attributes #40

Open DmitryTsepelev opened 5 years ago

DmitryTsepelev commented 5 years ago

More details to be added, reference is here

ivandenysov commented 4 years ago

using #parent inside of array attribute worked for me. Is this issue outdated? 🤔

DmitryTsepelev commented 4 years ago

Hi @ivan-denysov! If I recall correctly, the issue was related to updates (from this comment):

... direct StoreModel's Array mutation, it won't update a parent until one of the accessor methods gets called

In other words, it might not work if you take and array and add the element to it, something like:

config = Configuration.new
product.configurations << config
config.parent # => nil
product.configurations
config.parent # => Product
jessevdp commented 1 year ago

Hey,

Has anything changed regarding this issue?

I just ran into a situation where I'll be needing a reference to the parent of a StoreModel in an array.

(I have a large JSON document with references between arrays. And want to create easy access to these references. For example:)

{
  foos: [
    { id: '...', bar_id: '' }
  ],
  bars: [
    { id: '...' }
  ]
}

configuration.foos.first.bar

If this behavior still isn't correct for nested store models in arrays I might be able to free up an afternoon and take a crack at fixing this.


Semi related: Is there any documentation on the #parent behavior of a StoreModel? I'm mostly wondering how to "set" the parent in situations like a test, so I could mock out the parent object, or at least supply a very small test double to play the parent role.