In my opinion, normal OO design principles apply:
- If the code is really a set of utilities that doesn't need access to object state, I would consider putting it in a module to be called separately. For instance, if the code is all mapping utilities, create a module
Maps
, and access the methods like: Maps::driving_directions
.
- If the code needs state and is used or could be used in every controller, put the code in ApplicationController.
- If the code needs state and is used in a subset of all controllers that are closely and logically related (i.e. all about maps) then create a base class (
class MapController < ApplicationController
) and put the shared code there.
- If the code needs state and is used in a subset of all controllers that are not very closely related, put it in a module and include it in necessary controllers.
In your case, the methods need state (params
), so the choice depends on the logical relationship between the controllers that need it.
In addition:
Also:
- Use partials when possible for repeated code and either place in a common 'partials' directory or include via a specific path.
- Stick to a RESTful approach when possible (for methods) and if you find yourself creating a lot of non-RESTful methods consider extracting them to their own controller.
For almost all cases, depending on a library or metaprogramming for a deprecation is overkill. Just add a comment to the rdoc and call the Kernel#warn
method. For example:
class Foo
# <b>DEPRECATED:</b> Please use <tt>useful</tt> instead.
def useless
warn "[DEPRECATION] `useless` is deprecated. Please use `useful` instead."
useful
end
def useful
# ...
end
end
If you're using Yard instead of rdoc, your doc comment should look like this:
# @deprecated Please use {#useful} instead
Lastly, if you adhere to tomdoc, make your comment look like this:
# Deprecated: Please use `useful` instead
Deprecated: Indicates that the method is deprecated and will be removed in a future version. You SHOULD use this to document methods that were Public but will be removed at the next major version.
Also, don't forget to remove the deprecated method in some future (and properly semver'd) release. Don't make the same mistakes that the Java libraries did.
Best Solution
I think the popular way is to use statement modifiers only if it is a one-liner. In all other cases, use the normal if style prevalent in C, Java etc.