The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Specific or default templates in Rails

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Maxim Kulkin

Posts: 58
Nickname: hapk
Registered: Sep, 2006

Maxim Kulkin is developer in Selectosa Systems.
Specific or default templates in Rails Posted: Jan 10, 2009 12:45 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Maxim Kulkin.
Original Post: Specific or default templates in Rails
Feed Title: Software development
Feed URL: http://maximkulkin.blogspot.com/feeds/posts/full?alt=rss
Feed Description: Software development
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Maxim Kulkin
Latest Posts From Software development

Advertisement
I want to develop admin controller to do simple CRUD operations on not-so-complex models. I want it to be DRY and to allow adding features to all of them easily. This is my second attempt, the first one was way too ugly: it used inheritance from admin's base controller and descendants passed customization parameters to base class; also, have used custom "render" methods to find template for specific controller or, if the specific template is missing, fall back to general template.

And then it broke. Yeah, this could be fixed, but the whole idea seemed bad.

I just finished the second attempt and it looks promising.

First, it includes only one controller and uses routes to determine the actual model to operate on. Every route includes additional parameter that is passed to controller that contains name of model:
map.resources :users, :controller => 'items',
    :requirements => { :item_type => 'User' }
map.resources :regions, :controller => 'items',
    :requirements => { :item_type => 'Region' }

Second, the most troublesome was to make it work like this: I have default views in app/views/items... I wanted to have subdirectories with name of particular model that would contain overrides to default templates:
app/views/items
    index.html.erb
    new.html.erb
    edit.html.erb
   _item.html.erb
    _form.html.erb
app/views/items/users
    _item.html.erb
    _form.html.erb
app/views/items/regions
    _form.html.erb

Regions doesn't need custom 'item' partial because default one (which outputs only item.to_s) fits well.

Obviously, controller's view_paths should be manipulated to search first in concrete subfolders and then in default place. But the problem was that it always tried to prepend controller name and so find it in corresponding subfolder. It's not very nice being forced to create additional directories like this:
app/views/items
app/views/items/users/items
    _item.html.erb

Well, after some digging the way to do it elegantly I found this solution:

1. Redefine controller's controller_path to return nil. After this all templates are searched in app/views/ instead of app/views/items, so it needs to be fixed, so
2. Redefine controller's view_paths to base in app/views/items.
3. Based on current item type prepend corresponding view path.

The result is as follows:
class ItemsController < ApplicationController
  def self.controller_path
    nil
  end
  self.view_paths = [ RAILS_ROOT + '/app/views/items' ]

  before_filter :setup_model

  # ... action code

  private

  def setup_model
    @model  = params[:item_type].constantize
    prepend_view_path(RAILS_ROOT + '/app/views/items/' + params[:item_type].underscore)
 end
end

Happy coding!

Read: Specific or default templates in Rails

Topic: Standalone web applications using OCaml + Ocsigen, benchmarked against Rails Previous Topic   Next Topic Topic: Proposals begin accepted for Ignite Phoenix III

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use