views:

225

answers:

2

by default, when i ask rails controller to do messages/index, he does

def index
 respond_to{|fmt| fmt.html}
end

and shows app/views/messages/index.html.erb

there is a customer which wants his instance of the platform to display views differently (and changes cannot be done with css only).

solution i think of would be

  1. create directory app/views/#{customername}, which would have same structure as app/views, but would only have views which have to override default ones.

  2. setting an array constant containing list of views which have to be overriden (if not, they should load the default views)

    CUSTOM_VIEWS["messages"]=["index","show","edit"]
    

    somewhere in the customer-specific config file

  3. in all controller actions do something like

    def index
     respond_to do |fmt| 
      fmt.html do
       if CUSTOM_VIEWS[params[:controller]].include?(params[:action])
        #override default app/views/messages/index.html.erb with app/views/customername/messages/index.html.erb 
        render "#{customername}/#{params[:controller]}/#{params[:action]}"
       end
      end
     end
    end
    

or is there better/faster solution/plugin to do that?

+1  A: 

This should help: http://railscasts.com/episodes/125-dynamic-layouts

LymanZerga
+1  A: 

i believe "view_paths" along with "prepend_view_path" can be an answer to my question

for example

http://www.axehomeyg.com/2009/06/10/view-path-manipulation-for-rails-with-aop/

upd:

solved with simple add to application_controller

def override_views
 if APP_CONFIG['pr_name']!=nil
      ActionController::Base.view_paths=[RAILS_ROOT+"/app/custom_views/"+APP_CONFIG['pr_name'],RAILS_ROOT+"/app/views/"]
 end 
end

where APP_CONFIG['pr_name'] is specific product name.

basically what it does is loading custom view from app/custom_views/customername/ if it exists for specific controller action, if not it loads default view from app/views/

Pavel K.