Topic: Move View Logic from the model

I have this in my model:

def html    
  "<div style='margin-top:2px;padding-top:10px;padding-bottom:10px;'>"+
  "<a href='/profile/#{about_user.id}' class='underline'>#{about_user.full_name}</a> posted to #{about_user.his_or_her} \"#{about_user.micro_blog_title}\" "+
  "<a id='blog_news_less_label_#{id}' style='margin-left:10px;font-style:italic;display:none;font-size:11px;' class='jLink emphasis' onclick='showLessBlogNewsItem(#{id})'>hide blog entry</a><a id='blog_news_more_label_#{id}' style='margin-left:10px;font-style:italic;font-size:11px;' class='jLink emphasis' onclick='showMoreBlogNewsItem(#{id})'>show blog entry</a>"+
  "</div>"+
  "<div id='blog_news_#{id}' style='border-top:1px dashed #ccc;border-bottom:1px dashed #ccc;margin-top:4px;padding:10px;display:none;margin-bottom:10px;font-size:12px;' class='info_area'>"+
  "<div style='font-weight:bold;'>#{blog_entry.title}</div>"+
  "<div>#{blog_entry.content_html}</div>"+
  "</div>"
end

I need to get it out to a partial or something. Can anyone suggest a way?

Re: Move View Logic from the model

Here is what happened.

 #view
<%for news_item in news_items%>
  <%=news_item.html%> #this called the html method set in the model.
<%end%>

I knew it was ugly, I knew it couldn't stay, I moved it to a partial, to get the bad smell away!!!

updated

<%for news_item in news_items%>
<%= render :partial => news_item.view_path, :locals => {:news_item => news_item} %> #the model now holds the view_path method, which specifies where the partial is located for the news item.
<%end%>

and now my partial looks like this
<div style='margin-top:2px;padding-top:10px;padding-bottom:10px;'>
  <%= link_to news_item.about_user.full_name, profile_path(news_item.about_user),
        :class => 'underline' %> posted to
  <%= news_item.about_user.his_or_her %> <%= news_item.about_user.micro_blog_title %>
  <%= link_to_function "hide blog entry", "showLessBlogNewsItem(#{news_item.id})",
        :style => 'margin-left:10px;font-style:italic;display:none;font-size:11px;',
        :class => 'jLink emphasis',
        :id => "blog_news_less_label_#{news_item.id}" %>
  <%= link_to_function "show blog entry", "showMoreBlogNewsItem(#{news_item.id})",
        :style => 'margin-left:10px;font-style:italic;font-size:11px;',
        :class => 'jLink emphasis',
        :id => "blog_news_more_label_#{news_item.id}"%>
</div>
<div id="blog_news_<%= news_item.id %>" style='border-top:1px dashed #ccc;border-bottom:1px dashed #ccc;margin-top:4px;padding:10px;display:none;margin-bottom:10px;font-size:12px;'class='info_area'>
  <div style='font-weight:bold;'>
    <%= news_item.blog_entry.title %>
  </div>
  <div>
    <%= news_item.blog_entry.content_html %>
  </div>
</div>

yay!

Last edited by Lake (2007-12-08 01:19:07)

Re: Move View Logic from the model

instead of

<%for news_item in news_items%>
<%= render :partial => news_item.view_path, :locals => {:news_item => news_item} %> #the model now holds the view_path method, which specifies where the partial is located for the news item.
<%end%>

you could simply do:
<%= render :partial => news_item.view_path, :collection => news_items %>

.. assuming your partial was named _news_item.rhtml

the :collection option does the loop you do manually by itself, making each object inside of "news_items" available to the partial as a local variable named like that partial (news_item)

yay! wink

PS: you do save a view path in the Model? Seems very odd to me...

Re: Move View Logic from the model

There are many news items and they all have a different partial that they are going to use. The template that I used in the example is one of like 10 different news items. Is there a better way I should specify that?

Thanks for your input, Duplex. smile

Re: Move View Logic from the model

I'm confused about this now. Each news item has two partials:

_description.html.erb
_title.html.erb

The partials will get passed the news_item variable, and the partials will display their information.
Should I specify the view path in the mode like this?

 
def view_path
   "/blog_news_items/_description.html.erb"
end

I use a view path in each of the models.

Last edited by Lake (2007-12-08 16:35:09)

Re: Move View Logic from the model

If you could explain the relation between NewsItem and BlogEntry a bit it would help. Your code sure looks messed up smile First of all you could put all inline styles inside a CSS file.

You should use more helpers i think to simplify the .rhtml files:
Instead of

<%= link_to news_item.about_user.full_name, profile_path(news_item.about_user), 
        :class => 'underline' %><

You could have this function in helper and just write:
<%= link_to_author_name(news_item) %>

Also you should render partials with collection:
<%= render :partial => "news_item", :collection => @news_items %>

And inside this news_item partial you could have
<h1><%= news_item.title %></h1>
<%= render :partial => "blog_entries/blog_entry", :collection => news_item.blog_entries %>

I dunno. I got confused now smile As I said I don't get the relation between your models sad