views:

106

answers:

3

I have a controller having more than 1000 lines of code.

Right not I am doing Code review for this controller. I arrange my methods according to the module. Now I realise that my controller is not easy to maintain and so I want to something like following

class UsersController < ApplicationController  
  #Code to require files here 
  #before filter code will goes here 

  #############Here i want to call that partial like things. following is just pseudo #########
    history module
    account module
    calendar module
    shipment module
    payment module
 ####################################################################

end #end of class

this help me so much to maintained the code as when i change history module i am sure that my account module is unchanged.I know CVS but i prefer 50 copies of each module instead 200 copies of my users_controller.rb itself.

P.S. :- I would like Affirmative answer.please don't answer like, you should use different controller for different module.....bla...bla...bla... as it's not possible for me to do so.

EDIT:- My Versions Are

rails -v
  Rails 2.3.4
ruby -v
  ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-linux]
A: 

hi! I tried the following method and I have it running, maybe it suits for you:

app/user_controller.rb

require 'index.rb'
class UsersController < ApplicationController  
  # some other code
end

app/index.rb

class UsersController < ApplicationController

def index
  @users = User.all
end

end

MY ENV: rails 3 beta4

Meduza
+4  A: 

This should work for you:

app/controllers/some_controller.rb

class SomeController < ApplicationController
  include MyCustomMethods
end

lib/my_custom_methods.rb

module MyCustomMethods
  def custom
    render :text => "Rendered from a method included from a module!"
  end
end

config/routes.rb

# For rails 3:
match '/cool' => "some#custom"

# For rails 2:
map.cool 'cool', :controller => "some", :action => "custom"

Fire up your app and hit http://localhost:3000/cool, and you'll get your custom method included from a module.

Chris
This is interesting. However, if i branch out in my lib directory, how do i include that module? For instance instead of having `lib/my_custom_methods.rb` if i have `lib/custom/methods.rb` how would i reference the latter in my controller?
Shripad K
You can create subdirectories in the lib folder and store your modules in those just fine. The stipulation is that if you have `lib/custom/methods.rb`, Rails will expect that file to define the module `Custom::Methods`. Apart from that, the creation and referencing of the module are exactly the same.
Chris
+1  A: 

Assuming from you pseudocode that you are referring to Ruby modules, and not something else, just put all your requires/modules in a separate module and include that or have your UsersController inherit from a base class if you are reusing those files. In the first case you can think of a module as a mix-in and it is designed for exactly the modularity you want.

module AllMyStuff
  include History
  include Account
  ...
end

class UsersController < ApplicationController
  include AllMyStuff

  def new
  end
  ...
end

or you can inherit from a base controller,in this case its probably a reasonable solution.

def BaseController < ActionController
  include history
  include account
end

def UsersController < BaseController
  # modules available to this controller by inheritance
  def new
  end
  ...
end
Jed Schneider