Re: How to clean up this code?

Ar Chron
Fri, 28 Mar 2008 13:41:45 -0000

Patrick Doyle wrote alot:

What I always find interesting is that different people can approach
essentially the same problem from very disparate approaches.  But I''ll
weigh in with my two cents...

My categories would go in a table/model all their own. After all, they
are just another form of data for the application, and ideally, if you
needed to support another document type, it''s just an add of a data
record, and the application code stays the same. Suppose you need to add
a new doc type ''QA Testing Protocols''? Do you alter your code,
or add a
new record that looks like:

:id => 5, :text => ''QA Testing Protocols'', :min =>
20, :max => 29


Ajax: Haven''t gone there yet myself either.


I try diligently to keep code out of my views (with varying degrees of
success).  Rather than a find(:all) in your controller, could you apply
the conditions there so the view just renders the documents it receives
(strict MVC adherents would say the view should only render what it is
given).

  def index
    if params[:category]
      @category  = params[:category].to_i
      # assuming that new model for document categories
      @doc_category = DocCategory.find_by_id(@category)
      min = @doc_category.min.to_s
      max = @doc_category.max.to_s
      cond = ["document_number_prefix >= ? AND
document_number_prefix <= ?", min, max]
    else
      cond = ["0=0"]
    end
    @documents = Document.find(:all, :conditions => cond)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @documents }
    end
  end

then part of your view, at least, becomes:


  <%= render :active_scaffold => :documents %>



Lastly, and this is strictly a personal preference, I very much like
haml for my views as opposed to erb. I got very tired of div, span, and
<%= %> spam in my views... Your mileage may vary.



--
Posted via http://www.ruby-forum.com/.