Alex Rivera | Logout

Rails Custom Deprecation Notices

Asked 2010-02-17T20:42:53.643
25

Is there a way to create custom deprecation notices for methods and/or associations in my application that I plan on removing and want to log their usage? I have a relationship in one of my models that I don't want to use moving forward and plan to refactor the code at a later time. I would like to create a notice in my development log every time that method is called.

I have seen deprecation notices in Ruby/Rails when using certain methods, and figure there has to be an easy way to do this.

Something like...

irb(main):001:0> 1.id
(irb):1: warning: Object#id will be deprecated; use Object#object_id
=> 3
Edit
Report

1 Answer

6

Maybe:

def old_relationship
  warn "[DEPRECATION] old_relationship is deprecated."
  @old_relationship
end

def old_relationship=(object)
  warn "[DEPRECATION] old_relationship is deprecated."
  @old_relationship = object
end

Something along those lines for a relationship.

answered 2010-02-17T20:48:54.373

Your Answer