MVC Philosophy


#1

My directory browsing app shows lists of file. If the item is a folder,
show a folder icon and link to the action that shows directory contents.
If the item is a graphics file, show a thumbnail and link to a larger
view. If the item is a non-graphics file, show an appropriate icon and
stream the file if the link is clicked.

The controller creates an array of arrays of items in the current
folder, with flags indicating the type of the item. The view iterates
over the array to create the page.

But the view code looks a lot like programming code, with if-then and
case statements to handle all the possibilities. It feels like it
belongs in the controller. I could remove some code into partials and
helpers, but that’s really just distributing the ugliness around.

On the other hand, using a controller to build an array of html links
doesn’t seem right either.

Philosophically, which way should I go?


#2

Brian A. wrote:

But the view code looks a lot like programming code, with if-then and
case statements to handle all the possibilities. It feels like it
belongs in the controller. I could remove some code into partials and
helpers, but that’s really just distributing the ugliness around.

On the other hand, using a controller to build an array of html links
doesn’t seem right either.

Philosophically, which way should I go?

Hmm… Personally, I think the controller should mark it as folder,
graphic or other and the view should have a case statement to pick up
the partial that should be used for rendering the view for it. The
partial might want to use a helper for things like thumbnails, etc. if
required…

Just my thoughts! :slight_smile:
Cheers
Mohit.


#3

Brian A. wrote:

My directory browsing app shows lists of file. If the item is a folder,
show a folder icon and link to the action that shows directory contents.
If the item is a graphics file, show a thumbnail and link to a larger
view. If the item is a non-graphics file, show an appropriate icon and
stream the file if the link is clicked.

The controller creates an array of arrays of items in the current
folder, with flags indicating the type of the item. The view iterates
over the array to create the page.

But the view code looks a lot like programming code, with if-then and
case statements to handle all the possibilities. It feels like it
belongs in the controller. I could remove some code into partials and
helpers, but that’s really just distributing the ugliness around.

On the other hand, using a controller to build an array of html links
doesn’t seem right either.

Philosophically, which way should I go?

It is hard to give advise without seeing your code. I think using
helpers and/or partials is the way to go. Hmmm… having flags seems
like a bad code smell. Each item should have its own type and with that
being the case you can do stuff like this:

<%= render :partial => “#{item.class.to_s.downcase}_show_link” %>

Hope this helps.