Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Minor tweaks to better illustrate tutorial content

  • Loading branch information...
commit 8f04b1db4d44783be6b3bea1e903f3515fb1cdb2 1 parent a6c72b6
@jcasimir jcasimir authored
View
2  app/controllers/articles_controller.rb
@@ -4,7 +4,7 @@ def show
end
def index
- @articles, @tag = Article.search(params)
+ @articles, @tag = Article.search_by_tag_name(params[:tag])
end
def new
View
7 app/controllers/comments_controller.rb
@@ -1,9 +1,8 @@
class CommentsController < ApplicationController
def create
- a = Article.find(params[:comment][:article_id])
- c = a.comments.new(params[:comment])
- c.save
+ article = Article.find(params[:comment][:article_id])
+ comment = article.comments.create(params[:comment])
flash[:notice] = "Your comment was added."
- redirect_to article_path(a)
+ redirect_to article_path(article)
end
end
View
10 app/models/article.rb
@@ -28,17 +28,17 @@ def self.valid_ids
Article.select(:id).collect{|a| a.id}
end
- def self.search(params)
- if params[:tag].nil?
+ def self.search_by_tag_name(tag_name)
+ if tag_name.blank?
[Article.scoped, nil]
else
- tag = Tag.find_by_name(params[:tag])
- [tag.articles, tag]
+ tag = Tag.find_by_name(tag_name)
+ tag ? [tag.articles, tag] : [[], nil]
end
end
def self.for_dashboard
- order('created_at DESC').limit(5).all
+ order('created_at DESC').limit(5)
end
def word_count
View
2  app/views/articles/show.html.erb
@@ -17,7 +17,7 @@
<div class='comment'>
<p>
<em><%= comment.author_name %></em>
- said at <%= distance_of_time_in_words(@article.created_at, comment.created_at) %> later:
+ said <%= distance_of_time_in_words(@article.created_at, comment.created_at) %> later:
</p>
<p><%= comment.body %></p>
</div>
View
27 spec/models/articles_spec.rb
@@ -41,7 +41,6 @@
Fabricate(:article)
end
expected = Article.order('created_at DESC').limit(5).all
-
articles = Article.for_dashboard
articles.count.should eq(5)
articles.should eq(expected)
@@ -62,4 +61,30 @@
article.word_count.should eq(6)
end
end
+
+ context ".search_by_tag_name" do
+ context "when given no parameter" do
+ it "should return all the articles" do
+ articles, tag = Article.search_by_tag_name(nil)
+ articles.should == Article.all
+ end
+ end
+
+ context "when given a tag name" do
+ context "and the tag exists" do
+ it "should return the articles associated with that tag and the tag" do
+ tag = Fabricate(:tag)
+ tag.articles = [Fabricate(:article), Fabricate(:article)]
+ Article.search_by_tag_name(tag.name).should == [tag.articles, tag]
+ end
+ end
+
+ context "but the tag does not exist" do
+ it "should return an empty list of articles and no tag" do
+ bad_name = Fabricate(:tag).name + "_no_exist"
+ Article.search_by_tag_name(bad_name).should == [[], nil]
+ end
+ end
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.