questions about inheritance in Rails

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.
:slight_smile:

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">
&nbsp;
     <%= 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 :slight_smile:

David

Hi --

David wrote:

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)

Thanks... good point. :slight_smile: I was so focused on translating the existing
code that I missed that.

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.

In other words, wrap the route method invocation with a helper method to
simplify the view code?

Yes -- something like:

   def model_path(*args)
     send("#{model_name}_path", *args)
   end

at which point you could do:

   <%= link to "Click here", model_path :id => item %>

There may be yet further ways to simplify it all but initially at
least I'd go for pushing all the low-level-ish stuff out of the views
and see what happens.

(Punting on the JS question... :slight_smile:

David