Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Set an ActionController's template root on each request

Set an ActionController's template root on each request for a domain acts as account key pattern. Careful with this code though. It relies on the behavior of a private rails method and could change 50 times in the next week. (see my blog post)

class MyController < ActionController::Base
  before_filter :set_site_template_root
  def set_site_template_root
    self.class.template_root = "#{RAILS_ROOT}/#{app}/#{views}/#{current_site.domain}")
    @template.base_path = template_root
  end
end


module ActionView
  class Base
    private
    alias_method :rails_assign_method_name, :assign_method_name

    # add a domain prefix at the end
    def assign_method_name(extension, template, file_name)
      rails_method_name = rails_assign_method_name(extension, template, file_name)
      @@method_names[file_name || template] = respond_to?(:current_site) ?
        "#{rails_method_name}_#{current_site.domain}".intern :
        rails_method_name
    end
  end
end

More rails conditional layouts

In your controller:

  layout :my_layout_func

  ...

protected

  def my_layout_func
   # anything ruby will work
   ['list','index',nil].include?(action_name)? 
   'noheading':'my_layout'
 
   # or you could do
   case action_name
    when 'monkey': 'monkey_layout'
    when 'edit'  : 'form_layout'
   end
 end



Themeable Views in Ruby

class ApplicationController < ActionController::Base
  before_filter :choose_theme
  
  private
  def choose_theme
    ActionController::Base.template_root = File.join(RAILS_ROOT, 'app/views', @params['theme'])
  end
end
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS