A little bit more polishing and then back to the hard stuff

September 5, 2007

One last thing i want to do – i want to add a new page where you can see all the reeplies, listed by score and date (like the article list). This uses a paginator, like the article list.

By basically copying the article code (i’m not going to reproduce it all here), this is all working. However, it looks bad – the format used to list stories at the bottom of the article doesn’t look so good once they’re a big list using the whole screen. In addition, each story/reeply should have a link to the article to which it replies.

So, i need a new format partial, to list stories and show the associated article as well. This one will be called ‘story_and_article’, and the format will be closer _article than it is to the existing _story partial. Here it is:

<% story = story_and_article %>
<tr>
<td>
<%= render :partial => ‘story/updown_arrows’,
:locals => { :story => story } %>
</td>
<td class=”poster_name”>
<%= story.points_to_s %>
</td>
<td>
<span class=”article_list”><%= link_to story.title,
:controller => ‘story’,
:action => “show”,
:id => story %></span>
<span class=”summary_list”> Reeplying to <%= link_to story.article.title,
:controller => ‘article’,
:action => “show”,
:id => story.article %></span>
<br/>
<% unless story.summary == nil || story.summary == “” %>
<%= render :partial => ‘shared/summary_brief’,
:locals => { :item => story } %>
<br/>
<% end %>
<%= render :partial => ‘shared/added_by’,
:locals => { :item => story } %>
<br/>
</td>
</tr>

Note that the first thing that i do is take the input object “story_and_article” and rename it to “story”, just so i’m not typing stuff like “story_and_article.article.summary”. The rest of it is pretty standard – note that i’m calling another two partials for the summary and ‘added_by’.

The summary partial is quite interesting, as it uses a helper method to show only the first n words of the summary: here’s the partial –

<span class=”summary_show”>”<%= first_x_words(item.summary, 20)%>”</span>

Minimal eh? I only split this into a partial in case i want to add a summary to articles as well – that’s why it works on an ‘item’, not a ‘story’.

Here’s the method it calls:

def first_x_words(str,n=20,finish=’…’)
truncated = str.split(‘ ‘)[0,n].inject{|sum,word| sum + ‘ ‘ + word}
truncated += finish if truncated.length < str.length
return truncated
end

This uses the ruby inject method, which i *have* understood in the past but haven’t used before. It’s a bit like a recursive method call i think. In case you’re wondering, … is n html symbol for “…” – it’s neater than actually writing “…”: a bit smaller and renders better across all platforms apparently.

The ‘added_at’ partial is the same code i’ve been reusing in various places – i just partialled it out for DRYness: again it uses ‘item’ since it IS used by article and story. Note that in the story_and_article code above i pass through a local variable, setting :item to be equal to ‘story’.
<span class=”poster_name”>
<%= “Added” if item.added_at or item.user_id %>
<%= “#{time_ago_in_words(item.added_at)} ago ” if item.added_at %>
<%= “by #{item.user.login}” if item.user%></span>

Done a bit more tweaking and polishing…

Found a bug with those submit_tag confirmation dialogs from yesterday – the box that’s displayed says “Submit Reeply “my reeply title”?”, getting the title from the saved object. But, the title hasn’t been saved to the object yet, so it displays the old name (eg the default of “untitled reeply”). I need to get the data out of the form field rather than the object.

OK, with some more forum help (the forums have been real good to me recently, probably because i’m doing run of the mill stuff rather than arsey video conversion), i just need to change the :onclick parameter:

<%= submit_tag “Delete Reeply”,
:onclick => “return confirm(‘Delete \”‘ + getElementById(‘story_title’).value + ‘\”?’);” %>

The key here is “getElementById” – the camelized name should be a giveaway that this isn’t ruby, it’s javascript. Good to know.

Just noticed something else – on the article list page, if an article has 1 Reeply, it says “1 Reeplies”. Need to adjust my number_of helper method to take pluralization (or lack of it) into account:

def number_of(params = {})
word = params[:word]
number = params[:number].to_i
if number != 1
word = word.pluralize
end
if number == 0
if params[:capitalize]
number = “No”
else
number = “no”
end
end
return number.to_s + ” ” + word
end

so, a an article with stories = 0 has “No Reeplies”, one with stories = 1 has “1 Reeply”, one with stories = 2 has “2 Reeplies”. I’ve left points as being displayed as “0 points” rather than “No points” as points can go to minus numbers, so 0 is a number, unlike Reeplies where 0 means an absence of something.

I think i’m just about done with the tweaking. Now, to get back to the video stuff again. Whoops, it’s 6 o clock already, time flies when you’ve not got much of it left.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: