I'm in the process of rewriting/refactoring a legacy application for a client from Rails 2.x to 3.x. As part of the refactoring I also want to step away from models/methods in our local language to a pure English code base.
This involves writing new methods for almost every feature. I solve this for methods by doing:
def english_method
# ...
end
def native_method
warn 'DEPRECATED, please use #english_method'
english_method
end
This works fine for methods, and helps me track places where the old method is still being used, without breaking any code.
For classes (models) however, I've been doing:
class NativeClass < EnglishClass
# DEPRECATED, Please use EnglishClass
end
class EnglishClass
# ...
end
This "works", whenever NativeClass
is called, the application keeps working, but I don't get any message in the log notifying me of a part of the application still calling NativeClass
.
How can I make sure every "touch" of NativeClass
actually results in a log error being written?
I tried (for no reason other than thinking "maybe this works") to do:
class NativeClass < EnglishClass
-> { ActiveSupport::Deprecation.warn 'Native model is deprecated in favor of English' }
end
But that (obviously?) didn't work. I figure the lambda would be lazy loaded every time NativeClass
is called, but my understanding of lambdas is still somewhat shallow, so I might be mistaken here.
Any clues on how to deprecate entire classes and send a warning message to my log when it's being touched?
Other "best practices" and or solutions for deprecation are welcome, but I'm not sure if it's a valid question for SO (and I don't want to risk this topic getting closed for this question).
Deprecation warnings are a common thing in our industry. They are warnings that notify us that a specific feature (e.g. a method) will be removed soon (usually in the next minor or major version) and should be replaced with something else.
Try to use like:
class NativeClass < EnglishClass
def initialize
ActiveSupport::Deprecation.warn "NativeClass is deprecated and may be removed from future releases, use EnglishClass instead.", caller
super
end
end
You might be better off just deprecating each method individually. This can easily be accomplished by using Rails' deprecate method and passing it a list of methods you want to deprecate. The list of methods defined within a class is available by passing false to public_instance_methods
class NativeClass < EnglishClass
def method1
end
def method2
end
deprecate *public_instance_methods(false)
end
Note that it needs to be declared AFTER the last public method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With