Skip to content

Instantly share code, notes, and snippets.

@kneath
Created October 18, 2012 21:26
Show Gist options
  • Select an option

  • Save kneath/3914835 to your computer and use it in GitHub Desktop.

Select an option

Save kneath/3914835 to your computer and use it in GitHub Desktop.
Given a controller in need of refactoring into multiple controllers, how can I redirect to new controller actions based upon a feature flag?
# Given a controller in need of refactoring into multiple controllers, how
# can I redirect to new controller actions based upon a feature flag?
#
# The current code "works" but doesn't exercise the controller action.
#
# Rails 2.3 or 3.0 methods accepted
class BetterController
def overview
# .. a small amount of auth/permissions code
end
end
class PoorlyNamedController
def show
if feature_flag?
render :template => 'better/overview', :layout => 'new_layout'
return
end
# ... tons of code here in a horrible method that needs to be split
# into multiple methods across many controllers
end
end
@joeyw
Copy link
Copy Markdown

joeyw commented Oct 19, 2012

This works with 2.3: http://www.railsonwave.com/2008/10/25/how-to-call-a-controller-s-action-from-a-different-controller

I had to change Inflector to ActiveSupport::Inflector for it to work for me though.

@kneath
Copy link
Copy Markdown
Author

kneath commented Oct 19, 2012

Man that's reallly close. Still it's trying to call the same action name (not the action name I'm sending it). I added the action name into the .process call in request, response, options[:action] but still wants the same name.

Thanks a ton though, this is getting very close!

@joeyw
Copy link
Copy Markdown

joeyw commented Oct 19, 2012

  def internal_redirect_to(options={})
    params.merge!(options)
    request.env['action_controller.request.path_parameters']['controller'] = params[:controller]
    request.env['action_controller.request.path_parameters']['action'] = params[:action]
    (c = ActionController.const_get(ActiveSupport::Inflector.classify("#{params[:controller]}_controller")).new).process(request,response)
    c.instance_variables.each{|v| self.instance_variable_set(v,c.instance_variable_get(v))} 
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment