westonganger / active_snapshot

Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation
MIT License
103 stars 16 forks source link

has_snapshot_children and has_one on model fails if nil #10

Closed newbenji closed 2 years ago

newbenji commented 2 years ago

if you have in your has_snapshot_children

  something:          {
        record:        instance.something,
        delete_method: ->(item) { item.release! }
      },

and for your model

has_one :something

if fails if instance.something = nil for for rails has_one is 0 or 1 item. so should not fail on nil

newbenji commented 2 years ago

solved it this way for now

but not sure how the restore will react record: instance.something ? instance.something : {},

westonganger commented 2 years ago

Yeah your totally right. Nil should be an accepted value. I can add this soon or will accept a PR if your willing

westonganger commented 2 years ago

This is now fixed in 76b6bd9

westonganger commented 2 years ago

v0.2.1 is now released which contains this fix.

newbenji commented 2 years ago

Actually still get the error with the new version

2: from /home/benji/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/active_snapshot-0.2.1/lib/active_snapshot/models/concerns/snapshots_concern.rb:26:in `create_snapshot!'
         1: from /home/benji/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/active_snapshot-0.2.1/lib/active_snapshot/models/concerns/snapshots_concern.rb:26:in `each'
/home/benji/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/active_snapshot-0.2.1/lib/active_snapshot/models/concerns/snapshots_concern.rb:27:in `block in create_snapshot!': undefined method `each' for nil:NilClass (NoMethodError)

this is my json for that part

      ,
      SystemLimit:          {
        # record: instance.SystemLimit ? instance.SystemLimit : {},
        record: instance.SystemLimit,
      },

The commented out line is the line working for me

westonganger commented 2 years ago

Sorry about that I have now updated the code, tests and released v0.2.2. Please update and that should fix your issue.