Commit 7a941ff8

mo khan <mo@mokhan.ca>
2014-10-17 01:56:42
extract tagged scope.
1 parent 850ea76
Changed files (3)
app/controllers/creation_tags_controller.rb
@@ -5,6 +5,6 @@ class CreationTagsController < ApplicationController
 
   def show
     @tag = params[:id].downcase.parameterize
-    @creations = Creation.includes([:user, :photos]).tagged_with([@tag]).where('photos_count > 0').page(params[:page]).per(12)
+    @creations = Creation.tagged(@tag).page(params[:page]).per(12)
   end
 end
app/models/creation.rb
@@ -30,4 +30,8 @@ class Creation < ActiveRecord::Base
   def liked_by(user)
     favorites.find_or_create_by(user: user)
   end
+
+  def self.tagged(tag)
+    includes([:user, :photos]).tagged_with([tag]).where('photos_count > 0')
+  end
 end
spec/controllers/creation_tags_controller_spec.rb
@@ -6,15 +6,16 @@ describe CreationTagsController do
     let(:user) { create(:user) }
 
     before :each do
-      user.tag(cake, :with => "cake", :on => :tags)
+      user.tag(cake, with: "cake", on: :tags)
       get :index
     end
 
     it "should load all the tags" do
-      assigns(:tags).count.should == 1
-      assigns(:tags).first.name.should == "cake"
+      expect(assigns(:tags).count).to eql(1)
+      expect(assigns(:tags).first.name).to eql("cake")
     end
   end
+
   describe "#show" do
     let(:user) { create(:user) }
     let(:tag) { "cake" }
@@ -24,21 +25,21 @@ describe CreationTagsController do
     let(:untagged_cake) { create(:creation) }
 
     before :each do
-      user.tag(tagged_tutorial, :with => tag, :on => :tags)
-      user.tag(tagged_cake, :with => tag, :on => :tags)
+      user.tag(tagged_tutorial, with: tag, on: :tags)
+      user.tag(tagged_cake, with: tag, on: :tags)
       get :show, id: tag
     end
 
     it "should return each cake that is tagged" do
-      assigns(:creations).should include(tagged_cake)
+      expect(assigns(:creations)).to include(tagged_cake)
     end
 
     it "should not return cakes that are not tagged" do
-      assigns(:creations).should_not include(untagged_cake)
+      expect(assigns(:creations)).to_not include(untagged_cake)
     end
 
     it "should include the tag" do
-      assigns(:tag).should == tag
+      expect(assigns(:tag)).to eql(tag)
     end
   end
 end