Hi --
I am in the process of subclassing some things in a current Rails project and would like some feedback on how I'm approaching it... what I have works but I'm not sure if there is a better way to approach this.
I am currently subclassing controllers like so:
class LookupsController < ApplicationController # GET /lookups def index end
#----------------------------------------------------------------- # the rest of these methods are called from subclassed controllers #-----------------------------------------------------------------
# GET /{controller_name} # GET /{controller_name}.xml def index_list render 'lookups/index' and return if controller_name == 'lookups' eval('@' + controller_name + ' = ' + model_name + '.find(:all)') respond_to do |format| format.html { render 'lookups/list' } format.xml { render :xml => eval('@' + controller_name + '.to_xml') } end end
This is all sort of at first glance, so I could well be overlooking some pitfalls, but why don't you just say @items or something instead of all the eval'ing? Also, instead of model_name, you could have your helper method return a model class. That way, you could do:
@items = model.find(:all)
# ... and so on ... end
class StatesController < LookupsController def index index_list end end
and the shared view template looks like this;
<h1><%= pretty_controller_name %></h1> <table> <thead> <tr> <td id="col_actions">Actions</td> <td id="col_name">Name</td> </tr> </thead> <tbody> <% for item in eval('@' + controller_name) %> <tr class="<%= cycle("even","odd") %>"> <td> <span class="itemActions"> <%= link_to(image_tag('item_delete.gif'), eval(model_name + '_path(:id => item)'),
At the very least I would use send there:
send("#{model_name}_path", :id => item)
But it's nicer if you can encapsulated that kind of calculation in the controller or a helper method.
I'll leave it at that, pending feedback on what I've missed
David