ranked-model icon indicating copy to clipboard operation
ranked-model copied to clipboard

Rails 5 depreciation warning for `attribute_changed?` method

Open jwilsjustin opened this issue 6 years ago • 8 comments

https://github.com/mixonic/ranked-model/blob/ef7690276723e40b87c1337cb4e86a3db0eb1902/lib/ranked-model/ranker.rb#L105-L107

If a method called in a after_* callback changes the rank, then you will get a deprecation warning:

DEPRECATION WARNING: The behavior of attribute_changed? inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after save returned (e.g. the opposite of what it returns now). To maintain the current behavior, use saved_change_to_attribute? instead.

jwilsjustin avatar Aug 17 '17 19:08 jwilsjustin

Maybe check if a newer rails version is in use using #respond_to? ?

def rank_changed?
  return instance.send "saved_change_to_#{ranker.column}?" if instance.respond_to?("saved_change_to_#{ranker.column}?".to_sym)
  instance.send "#{ranker.column}_changed?"
end

jwilsjustin avatar Aug 17 '17 19:08 jwilsjustin

@jwilsjustin, could you check if the latest version of the gem still has this problem? :)

brendon avatar Oct 17 '18 23:10 brendon

@brendon It reproduces on 0.4.4 (https://github.com/mixonic/ranked-model/blob/master/lib/ranked-model/ranker.rb#L114)

razvan-sv avatar Oct 10 '19 12:10 razvan-sv

Just looking quickly, the only place this is called is in:

before_save :handle_ranking

Would you mind generating a failing test case showing the problem as I don't think we use after_ callbacks in this gem.

It could be related to a cascading change where your model in particular is calling save again in an after_ callback, thus the before callback is nested in an after callback. Not sure what we can do about that though.

brendon avatar Oct 10 '19 20:10 brendon

I'll look more. I can't find who's generating some deprecations and after commenting our ranked model they didn't show up. Didn't spent too much time on it.

razvan-sv avatar Oct 11 '19 13:10 razvan-sv

Thanks @razvan-sv, I suspect it'll be as a described above.

brendon avatar Oct 13 '19 22:10 brendon

Any plan to fix this in an upcoming release?

emilkarl avatar Aug 20 '20 07:08 emilkarl

So far no one has created a PR with a failing test case showing that the issue lies within ranked-model. I'd be happy to look at a fix if someone does this. Or if they want to go further and do a complete PR with a test and fix that would be even better! :D

brendon avatar Aug 23 '20 21:08 brendon