djlewis
2008-May-26 20:49 UTC
Handling multiple models DRYly-- using eval, but better way?
I have a bunch of models (about 30) that are all handled roughly the
same way despite having different fields, text, etc (there''s a big
data structure to drive all that). I''m trying to be DRY and handle
them generically. The best way I''ve come up with is occasional use of
eval. That leads to controller code like this to, say, add a bunch of
records to the db table...
tbl = q.code # the particular model''s id
tblp = tbl.pluralize
tblc = tbl.camelize
@user_records = eval( "@user.#{tblp}")
if @user_records.empty?
stuff.each do |sh|
rcd = eval( "#{tblc}.new")
# code here to create new rcd from sh
eval( "@user.#{tblp} << rcd")
end
end
and this, in the corresponding view...
<% form_for @page.code.to_s + "[]", :url => {:action
=> :question, :id => @page.code} do |form| %>
<% @user_records.each do |rcd| %>
<% eval "@#{@page.code}= rcd" %>
<% @columns.each_column do |col| %>
<%= "<p> #{qfield( form, col)}-- column = #{col.code} /
#{col.type} / #{col.text.to_s}</p>" %>
<% end %>
<% end %>
<% end %>
where qfield is a helper to construct the appropriate html for the
column according to its type and other characteristics.
My question -- is there a better way? Rails seems to want an actual
assignment to the @model variable to drive the view, so just pointing
at it with a more generic variable won''t work (I think I tried this,
but maybe I didn''t do it right). That''s the reason behind the
third
line of the view.
And is there a way to treat the model''s class dynamically without
eval, in -- rcd = eval( "#{tblc}.new")
Thanks. --David.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
David A. Black
2008-May-26 21:01 UTC
Re: Handling multiple models DRYly-- using eval, but better way?
Hi -- On Mon, 26 May 2008, djlewis wrote:> > I have a bunch of models (about 30) that are all handled roughly the > same way despite having different fields, text, etc (there''s a big > data structure to drive all that). I''m trying to be DRY and handle > them generically. The best way I''ve come up with is occasional use of > eval. That leads to controller code like this to, say, add a bunch of > records to the db table... > > tbl = q.code # the particular model''s id > tblp = tbl.pluralize > tblc = tbl.camelize > @user_records = eval( "@user.#{tblp}")@user_records = @user.send(tblp)> if @user_records.empty? > stuff.each do |sh| > rcd = eval( "#{tblc}.new")rcd = tblc.constantize.new> # code here to create new rcd from sh > eval( "@user.#{tblp} << rcd")@user.send(tblp) << rcd> end > end > > and this, in the corresponding view... > > <% form_for @page.code.to_s + "[]", :url => {:action > => :question, :id => @page.code} do |form| %> > <% @user_records.each do |rcd| %> > <% eval "@#{@page.code}= rcd" %><% instance_variable_set("@#{@page.code}", rcd) %>> <% @columns.each_column do |col| %> > <%= "<p> #{qfield( form, col)}-- column = #{col.code} / > #{col.type} / #{col.text.to_s}</p>" %> > <% end %> > <% end %> > <% end %> > > where qfield is a helper to construct the appropriate html for the > column according to its type and other characteristics. > > My question -- is there a better way? Rails seems to want an actual > assignment to the @model variable to drive the view, so just pointing > at it with a more generic variable won''t work (I think I tried this, > but maybe I didn''t do it right). That''s the reason behind the third > line of the view. > > And is there a way to treat the model''s class dynamically without > eval, in -- rcd = eval( "#{tblc}.new")See above for alternatives to all of your eval uses (untested). The methods I''ve used are all Ruby methods with the exception of constantize, which is from ActiveSupport. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 23-26 London (Skills Matter) See http://www.rubypal.com for details and updates! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---