Commit d4d844dc1cad1c55888342b8dc8dc9683efffbe3
- Date: Fri May 23 10:20:08 +0000 2008
- Committer: Xavier Shay (xavier@rhnh.net)
- Author: Xavier Shay (xavier@rhnh.net)
- Commit SHA1: d4d844dc1cad1c55888342b8dc8dc9683efffbe3
- Tree SHA1: 36f8f70f46f166c5a5cce45f08a0fcb831523372
- spec/views/posts/show.html.erb_spec.rb 7 -++++++
- app/views/posts/show.html.erb 12 ++++++++++++
- app/models/post.rb 4 ++++
- public/stylesheets/rhnh-standard.css 17 +++++++++++++++++
- spec/models/post_spec.rb 12 ++++++++++++
Commit diff
- Diff rendering mode:
- inline
- side by side
app/models/post.rb
|   | ||
| 17 | 17 | end |
| 18 | 18 | end |
| 19 | 19 | |
| 20 | def related_posts | |
| 21 | Post.search(:limit => 4, :conditions => {:tag_list => tag_list.join("|")}).reject {|x| x == self }.first(3) | |
| 22 | end | |
| 23 | ||
| 20 | 24 | before_validation :generate_slug |
| 21 | 25 | before_validation :set_dates |
| 22 | 26 | before_save :apply_filter |
| toggle raw diff | ||
app/views/posts/show.html.erb
|   | ||
| 13 | 13 | </li> |
| 14 | 14 | <% end -%> |
| 15 | 15 | </ol> |
| 16 | <div class="related"> | |
| 17 | <% if @post.related_posts.empty? -%> | |
| 18 | <p>Hungry for more? Head on over to the <%= link_to("archives", archives_path) %>.</p> | |
| 19 | <% else -%> | |
| 20 | <p>Hungry for more? Try these <strong>related posts</strong>, or find more in the <%= link_to("archives", archives_path) %>.</p> | |
| 21 | <ul> | |
| 22 | <% @post.related_posts.each do |post| -%> | |
| 23 | <li><%= link_to(h(post.title), post_path(post)) %></li> | |
| 24 | <% end -%> | |
| 25 | </ul> | |
| 26 | <% end -%> | |
| 27 | </div> | |
| 16 | 28 | <h2>Post a comment</h2> |
| 17 | 29 | <% unless @comment.errors.empty? -%> |
| 18 | 30 | <div class="errors"> |
| toggle raw diff | ||
public/stylesheets/rhnh-standard.css
|   | ||
| 433 | 433 | overflow: auto; |
| 434 | 434 | } |
| 435 | 435 | |
| 436 | /* Post */ | |
| 437 | ||
| 438 | .related { | |
| 439 | background-color: #f8f8f8; | |
| 440 | border: 1px solid silver; | |
| 441 | padding: 5px; | |
| 442 | padding-left: 10px; | |
| 443 | } | |
| 444 | ||
| 445 | .related ul, .related p { | |
| 446 | margin: 0px; | |
| 447 | padding: 0px; | |
| 448 | } | |
| 449 | ||
| 450 | .related ul { | |
| 451 | margin-left: 15px; | |
| 452 | } | |
| 436 | 453 | |
| 437 | 454 | /* Syntax */ |
| 438 | 455 | |
| toggle raw diff | ||
spec/models/post_spec.rb
|   | ||
| 151 | 151 | end |
| 152 | 152 | end |
| 153 | 153 | |
| 154 | describe Post, '#related_posts' do | |
| 155 | it 'returns first 3 related posts, excluding the post' do | |
| 156 | post = Post.new | |
| 157 | post.stub!(:tags).and_return([ | |
| 158 | mock_model(Tag, :name => 'robot'), | |
| 159 | mock_model(Tag, :name => 'heart') | |
| 160 | ]) | |
| 161 | Post.should_receive(:search).with(:limit => 4, :conditions => {:tag_list => 'robot|heart'}).and_return([post, 1, 2, 3, 4]) | |
| 162 | post.related_posts.should == [1, 2, 3] | |
| 163 | end | |
| 164 | end | |
| 165 | ||
| 154 | 166 | describe Post, 'validations' do |
| 155 | 167 | def valid_post_attributes |
| 156 | 168 | { |
| toggle raw diff | ||
spec/views/posts/show.html.erb_spec.rb
|   | ||
| 22 | 22 | :published_at => 1.year.ago, |
| 23 | 23 | :slug => 'a-post', |
| 24 | 24 | :approved_comments => [mock_comment], |
| 25 | ||
| 25 | :tags => [mock_tag], | |
| 26 | :related_posts => [mock_model(Post, | |
| 27 | :title => 'Hello', | |
| 28 | :slug => 'hello', | |
| 29 | :published_at => 1.year.ago.utc | |
| 30 | )] | |
| 26 | 31 | ) |
| 27 | 32 | assigns[:post] = @post |
| 28 | 33 | assigns[:comment] = Comment.new |
| toggle raw diff | ||
