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

previous_changes don't work with 1.1.1 and Rails 5.1 #55

Open
rivsc opened this issue Jun 27, 2017 · 3 comments
Open

previous_changes don't work with 1.1.1 and Rails 5.1 #55

rivsc opened this issue Jun 27, 2017 · 3 comments

Comments

@rivsc
Copy link

rivsc commented Jun 27, 2017

ActiveModel::Dirty#previous_changes (http://api.rubyonrails.org/classes/ActiveModel/Dirty.html#method-i-previous_changes) don't return attributes out of ar_typed_store field.

Model :

class Product < ActiveRecord::Base
  typed_store :ar_typed_store do |s|
    s.string :attr_in_ar_typed_store
  end
end

To reproduce that issue :

a = Product.find(x)
a.attr_in_ar_typed_store = 'test'
a.save #=> true
puts a.previous_changes

Work on 1.1.1 with Rails 4.2 (ar_typed_store appears and attr_in_ar_typed_store too) :

{"ar_typed_store"=>[{"attr_in_ar_typed_store"...},{"attr_in_ar_typed_store"...}], "attr_in_ar_typed_store"=>[nil, 'test'], updated_at"=>[Tue, 27 Jun 2017 09:53:32 UTC +00:00, Tue, 27 Jun 2017 09:56:28 UTC +00:00]}

Don't work on 1.1.1 with Rails 5.1.1 (ar_typed_store appears but not attr_in_ar_typed_store)

# {"ar_typed_store"=>[{"attr_in_ar_typed_store"...},{"attr_in_ar_typed_store"...}], updated_at"=>[Tue, 27 Jun 2017 09:53:32 UTC +00:00, Tue, 27 Jun 2017 09:56:28 UTC +00:00]}

EDIT : works in rails 4.2 with Activerecord-Typedstore 1.1.1

@Edouard-chin
Copy link
Contributor

Edouard-chin commented Jun 30, 2017

Thanks for opening the issue. I dug on this, and it's not going to be simple to put back the original behaviour.

The behaviour changed because of rails/rails@136fc65 which was introduce in rails 5.0
Model#previous_changes was previously defined in ActiveModel and was just returning the @previously_changed hash. This gem injects store attributes inside that hash.

On rails >= 5.0, previous_changes is delegated to AttributeMutationTracker#changes, the implementation returns only attributes that AR is aware of.

Vanilla rails store method never behave that way, and I was asking myself if this could be something we could patch upstream, as it's pretty useful.

Otherwise the only way I could think of would be to monkey patch AttributesMutationTracker#changes

FYI @rafaelfranca

@rafaelfranca
Copy link
Collaborator

Yes, we can try to implement this upstream but consider it a new feature not a bug fix. Not sure if it is possible

@rivsc
Copy link
Author

rivsc commented Jul 3, 2017

Thanks for replies.

I had looked at the typed_store and rails code for a few days. But I don't think it's possible without monkey patch rails. And I'm not sure it's a good idea... :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants