Thursday, January 22, 2009

Group controllers/views in RubyonRails

Hi guys,

It has really been a long time since i blogged about something from ruby on rails corner.. and so here is one.

Recently, have been asked to merge to rails application in one. Meaning to merge the code base so that they can become an single application but to maintain the readability i thought of keeping the code base some how segregated. So initially the idea was of making a plugin of application which is to be merged with main application and with the use of rails-engine plugin that would have been easy(at least that i thought of) but see the dilemma i was not even able to run the demo plugin(with the use of rails-engine) and as time was constraint too i thought of dropping the idea and doing some thing easy which can do the work and also in less time.

Then came the idea of grouping controller, views, helpers, layout, model etc... all in different folders and i was able to segregate controller, views but not models(anyone with solution/thought on it please comment).

Static view of segregated 'controllers'


Static view of segregated 'helpers'


Static view of segregated 'views'


Now that i have shown you the file positions.. what is the code which does the trick!! The answer is, when one creates a folder inside controller's folder in rails application, it makes a module of it and one should make the controllers belong to this module other wise those controllers will not be available.

class AuditTool::BaseController < ApplicationController

end

this acts as application controller(which is derived for the main application-controller) to the segregated controllers and other controllers could be derived from this base controller, like this


class AuditTool::BatchesController < AuditTool::BaseController
layout "/audit_tool/layouts/main"
def index
# line(s) of code.................
end
def list
# line(s) of code ...............
end
end


and helpers looks like this

module AuditTool::BatchesHelper
def function1(processed_on,label = "earlier")
# line(s) of code ...............
end
def function2(processed_on,label = "earlier")
# line(s) of code ...............
end
end


and getting spoon feeding the head section of layout goes like this

<%=stylesheet_link_tag("audit_tool/style")-%>
<%=stylesheet_link_tag("audit_tool/style_new")-%>
<%=javascript_include_tag :defaults-%>
<%=javascript_include_tag "audit_tool/slider.js"-%>
<%=javascript_include_tag "audit_tool/custom_1.js"-%>

link to controller/action of segregated controller is written like this

<%=link_to "Report Audit", {:controller => "audit_tool/reports" , :action=> "index"} -%>

link to main controller/action from inside of segregated views is written like this

<%=link_to "Got to main app", {:controller => '/admin', :action => 'dashboard' }%>


the post has started looking long, i thing i should stop it here only....
anybody with thoughts on this with any prospective do comment ( after all knowledge is sharable)

2 comments:

  1. arre janab, isme se yeh real time controller and actionnames to hata dete...
    kyon public kar rahe ho office ka data....

    ReplyDelete
  2. can any person really make out of this??
    mujhe nahin lagta... soch samjh kar he daala tha

    ReplyDelete

Sky diving! What it feels like.

Writing a new post after almost a year :) Penning it down here.. so that can relive that moment when i did the jump from 10000 feet up in...