ruby on rails - Inherit from parent controller or use concerns? -
i have 5 controllers share common code. best let them inherit parent controller, or use concerns? example:
class postscontroller < applicationcontroller before_action :authenticate, :set_project layout 'projects' end class commentscontroller < applicationcontroller before_action :authenticate, :set_project layout 'projects' end # 3 other controllers, etc... i let controllers inherit 1 controller declares before_actions , layout, or stuff common code concern.
what's criteria 1 choice or other? defined?
my rule of thumb is:
if controllers share same namespace in url (for example /projects/... or /admin/...), use inheritance projects::basecontroller or admin::basecontroller.
if share methods or declarations , not share namespace, use mixins.
and prefer duplicated code. because code in place easier understand mixin meaningless name. have name concern covers authentication and layout?
Comments
Post a Comment