ruby - Overriding Render in Rails 3 - Request for design review / code refactor -
i'm looking extensible way inject functionality controller.
module social module request def self.included( base ) base.alias_method_chain :render, :social base.alias_method_chain :process_action, :social end def process_action_with_social(method_name, *args) @action = method_name process_action_without_social(method_name, *args) end def render_with_social(action = nil, options = {}, &blk) if @action == "new" # social end render_without_social end end end class requestcontroller < applicationcontroller include social::request def new @request = recommendationrequest.new end end this code above working, i'm pretty sure it's not optimal ruby. i'm going creating different modules each controller, bunch of them, actually. code unique each controller, render_with_social.
so questions are:
- is there way pull other 2 methods out, can not repeat myself? (sorry, first time i've used modules in ruby).
- is there way without having put include social::request in requestcontroller @ all? (i want able remove these modules, , have controller continue work if nothing had happened)
- if wind having different suites of these metrics::request, scoring::request etc, best way approach having single location specify modules mixed in controllers?
thanks all.
Comments
Post a Comment