Commit 68c0a36c

mo khan <mo@mokhan.ca>
2015-01-28 05:40:39
happy hound.
1 parent 0c018dc
app/controllers/api/v1/cakes_controller.rb
@@ -10,17 +10,19 @@ module Api
       end
 
       def create
+        name = cake_params[:name]
         category = Category.find(cake_params[:category_id])
-        @cake = current_user.create_cake(name: cake_params[:name], category: category)
+        @cake = current_user.create_cake(name: name, category: category)
         if @cake.save
-          PublishToTwitterJob.set(wait_until: 1.hour.from_now).perform_later(@cake)
+          one_hour = 1.hour.from_now
+          PublishToTwitterJob.set(wait_until: one_hour).perform_later(@cake)
         end
       end
 
       def update
         @cake = current_user.creations.find(params[:id])
         current_user.tag(@cake, with: params[:cake][:tags], on: :tags)
-        @cake.update!(cake_params.reject { |key, value| key == "tags" })
+        @cake.update!(cake_params.reject { |key, _| key == "tags" })
       end
 
       def destroy
spec/controllers/api/v1/cakes_controller_spec.rb
@@ -7,7 +7,7 @@ describe Api::V1::CakesController do
     let(:user) { create(:user) }
 
     before :each do
-      request.env["HTTP_AUTHORIZATION"] = ActionController::HttpAuthentication::Token.encode_credentials(user.authentication_token)
+      api_login(user)
     end
 
     describe "#index" do
@@ -58,10 +58,11 @@ describe Api::V1::CakesController do
       let(:cake) { create(:cake, user: user) }
 
       it "tags the cake" do
-        xhr :patch, :update, id: cake.id, cake: { tags: "cake, cookies, yummy" }
+        tags = ["cake", "cookies", "yummy"]
+        xhr :patch, :update, id: cake.id, cake: { tags: tags.join(", ") }
 
         cake.reload
-        expect(cake.tags.pluck(:name)).to match_array(["cake", "cookies", "yummy"])
+        expect(cake.tags.pluck(:name)).to match_array(tags)
       end
 
       it "updates the description" do
@@ -72,7 +73,7 @@ describe Api::V1::CakesController do
         expect(cake.story).to eql(new_story)
       end
 
-      it 'updates the category' do
+      it "updates the category" do
         category = create(:category)
         xhr :patch, :update, id: cake.id, cake: { category_id: category.id }
 
spec/controllers/api/v1/photos_controller_spec.rb
@@ -1,4 +1,4 @@
-require 'rails_helper'
+require "rails_helper"
 
 module Api
   module V1
@@ -9,11 +9,11 @@ module Api
       let!(:cake) { create(:creation, user: user) }
 
       before :each do
-        request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(user.authentication_token)
+        api_login(user)
       end
 
       describe "#index" do
-        it 'loads the cakes photos' do
+        it "loads the cakes photos" do
           xhr :get, :index, cake_id: cake.id
           expect(assigns(:photos)).to match_array(cake.photos)
         end
@@ -22,23 +22,23 @@ module Api
       describe "#show" do
         let(:photo) { create(:photo, imageable: cake) }
 
-        it 'loads the photo' do
+        it "loads the photo" do
           xhr :get, :show, cake_id: cake.id, id: photo.id
           expect(assigns(:photo)).to eql(photo)
         end
       end
 
       describe "#create" do
-        let(:file) { fixture_file_upload('images/example.png', 'image/png') }
+        let(:file) { fixture_file_upload("images/example.png", "image/png") }
 
-        it 'attaches a new photo to a cake' do
+        it "attaches a new photo to a cake" do
           allow(ProcessPhotoJob).to receive(:perform_later)
 
-          xhr :post, :create, cake_id: cake.id, watermark: 'watery', image: file
+          xhr :post, :create, cake_id: cake.id, watermark: "watery", image: file
 
           cake.reload
           expect(cake.photos.count).to eql(1)
-          expect(cake.photos.first.watermark).to eql('watery')
+          expect(cake.photos.first.watermark).to eql("watery")
           expect(cake.photos.first.image_processing).to be_truthy
           expect(ProcessPhotoJob).to have_received(:perform_later)
         end
spec/controllers/api/v1/profiles_controller_spec.rb
@@ -1,4 +1,4 @@
-require 'rails_helper'
+require "rails_helper"
 
 module Api
   module V1
@@ -7,40 +7,40 @@ module Api
       let(:user) { create(:user) }
 
       before :each do
-        request.env["HTTP_AUTHORIZATION"] = ActionController::HttpAuthentication::Token.encode_credentials(user.authentication_token)
+        api_login(user)
       end
 
       describe "#show" do
-        it 'loads the current users profile' do
-          xhr :get, :show, id: 'me'
+        it "loads the current users profile" do
+          xhr :get, :show, id: "me"
           expect(assigns(:profile)).to eql(user)
         end
       end
 
       describe "#update" do
-        it 'updates the users profile' do
+        it "updates the users profile" do
           new_attributes = {
-            name: 'new name',
-            email: 'new@example.com',
-            city: 'new town',
-            website: 'http://example.com',
-            twitter: 'blabber',
-            facebook: 'facebookie',
+            name: "new name",
+            email: "new@example.com",
+            city: "new town",
+            website: "http://example.com",
+            twitter: "blabber",
+            facebook: "facebookie",
           }
 
-          xhr :patch, :update, id: 'me', profile: new_attributes
+          xhr :patch, :update, id: "me", profile: new_attributes
 
           user.reload
-          expect(user.name).to eql('new name')
-          expect(user.email).to eql('new@example.com')
-          expect(user.city).to eql('new town')
-          expect(user.website).to eql('http://example.com')
-          expect(user.twitter).to eql('blabber')
-          expect(user.facebook).to eql('facebookie')
+          expect(user.name).to eql("new name")
+          expect(user.email).to eql("new@example.com")
+          expect(user.city).to eql("new town")
+          expect(user.website).to eql("http://example.com")
+          expect(user.twitter).to eql("blabber")
+          expect(user.facebook).to eql("facebookie")
         end
 
-        it 'returns errors' do
-          xhr :patch, :update, id: 'me', profile: { email: '' }
+        it "returns errors" do
+          xhr :patch, :update, id: "me", profile: { email: "" }
           json = JSON.parse(response.body)
           expect(json["email"]).to_not be_empty
         end
spec/controllers/api/v1/tutorials_controller_spec.rb
@@ -4,7 +4,7 @@ describe Api::V1::TutorialsController do
   let(:user) { create(:user) }
 
   before :each do
-    request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(user.authentication_token)
+    api_login(user)
   end
 
   describe "#index" do
@@ -22,9 +22,9 @@ describe Api::V1::TutorialsController do
     it "creates a new tutorial" do
       attributes = {
         url: "https://twitter.com/",
-        image_url: "https://abs.twimg.com/a/1405611403/img/t1/front_page/exp_wc2014_gen_laurenlemon.jpg",
+        image_url: "https://abs.twimg.com/a/img/t1/lemon.jpg",
         heading: "Twitter",
-        description: "Connect with your friends - and other fascinating people. Get in-the-moment updates on the things that interest you. And watch events unfold, in real time, from every angle.",
+        description: "Connect with your friends - and other fascinating people",
         tags: "cake,cookie",
       }
       xhr :post, :create, tutorial: attributes
@@ -34,8 +34,8 @@ describe Api::V1::TutorialsController do
       expect(assigns(:tutorial).description).to eql(attributes[:description])
       expect(assigns(:tutorial).heading).to eql(attributes[:heading])
       expect(assigns(:tutorial).tags.count).to eql(2)
-      expect(assigns(:tutorial).tags.first.name).to eql('cake')
-      expect(assigns(:tutorial).tags.last.name).to eql('cookie')
+      expect(assigns(:tutorial).tags.first.name).to eql("cake")
+      expect(assigns(:tutorial).tags.last.name).to eql("cookie")
     end
   end
 end
spec/controllers/api/v2/cakes_controller_spec.rb
@@ -1,4 +1,4 @@
-require 'rails_helper'
+require "rails_helper"
 
 module Api
   module V2
@@ -6,32 +6,36 @@ module Api
       render_views
 
       describe "#index" do
-        let!(:cakes) { create(:category, slug: 'cakes') }
-        let!(:cookies) { create(:category, slug: 'cookies') }
-        let!(:cake) { create(:published_cake, name: 'cake', category: cakes) }
-        let!(:cookie) { create(:published_cake, name: 'cookie', category: cookies) }
-        let!(:unpublished_cake) { create(:cake, name: 'unpublished', category: cakes) }
+        let!(:cakes) { create(:category, slug: "cakes") }
+        let!(:cookies) { create(:category, slug: "cookies") }
+        let!(:cake) { create(:published_cake, name: "cake", category: cakes) }
+        let!(:cookie) do
+          create(:published_cake, name: "cookie", category: cookies)
+        end
+        let!(:unpublished_cake) do
+          create(:cake, name: "unpublished", category: cakes)
+        end
 
-        it 'returns all published cakes' do
+        it "returns all published cakes" do
           xhr :get, :index
           expect(assigns(:cakes)).to match_array([cake, cookie])
         end
 
-        it 'returns all cakes in the category' do
+        it "returns all cakes in the category" do
           xhr :get, :index, category: cookie.category.slug
           expect(assigns(:cakes)).to match_array([cookie])
         end
 
-        it 'returns all cakes matching the search query' do
+        it "returns all cakes matching the search query" do
           xhr :get, :index, q: cake.name[0..2]
           expect(assigns(:cakes)).to match_array([cake])
         end
 
-        it 'returns all cakes tagged with the tag' do
-          cake.tag_list = 'cakes'
+        it "returns all cakes tagged with the tag" do
+          cake.tag_list = "cakes"
           cake.save!
 
-          xhr :get, :index, tags: 'cakes'
+          xhr :get, :index, tags: "cakes"
           expect(assigns(:cakes)).to match_array([cake])
         end
       end
@@ -39,7 +43,7 @@ module Api
       describe "#show" do
         let!(:cake) { create(:published_cake) }
 
-        it 'loads the cake' do
+        it "loads the cake" do
           xhr :get, :show, id: cake.id
           expect(assigns(:cake)).to eql(cake)
         end
spec/controllers/api/v2/categories_controller_spec.rb
@@ -1,12 +1,14 @@
-require 'rails_helper'
+require "rails_helper"
 
 module Api
   module V2
     describe CategoriesController do
+      render_views
+
       describe "#index" do
         let!(:category) { create(:category) }
 
-        it 'loads all the categories' do
+        it "loads all the categories" do
           xhr :get, :index
           expect(assigns(:categories)).to match_array([category])
         end
@@ -16,7 +18,7 @@ module Api
         let!(:other_category) { create(:category) }
         let!(:category) { create(:category) }
 
-        it 'loads the specified category' do
+        it "loads the specified category" do
           xhr :get, :show, id: category.id
           expect(assigns(:category)).to eql(category)
         end
spec/controllers/api/v2/photos_controller_spec.rb
@@ -1,13 +1,15 @@
-require 'rails_helper'
+require "rails_helper"
 
 module Api
   module V2
     describe PhotosController do
+      render_views
+
       describe "#index" do
         let!(:processed_photo) { create(:photo, image_processing: nil) }
         let!(:unprocessed_photo) { create(:photo, image_processing: true) }
 
-        it 'loads all processed photos' do
+        it "loads all processed photos" do
           xhr :get, :index
           expect(assigns(:photos)).to match_array([processed_photo])
         end
@@ -17,7 +19,7 @@ module Api
         let!(:other_photo) { create(:photo) }
         let!(:photo) { create(:photo) }
 
-        it 'loads the specified photo' do
+        it "loads the specified photo" do
           xhr :get, :show, id: photo.id
           expect(assigns(:photo)).to eql(photo)
         end
spec/controllers/api/v2/tutorials_controller_spec.rb
@@ -1,9 +1,10 @@
-require 'rails_helper'
+require "rails_helper"
 
 module Api
   module V2
     describe TutorialsController do
       render_views
+
       describe "#index" do
         let!(:tutorial) { create(:tutorial) }
 
@@ -11,26 +12,27 @@ module Api
           xhr :get, :index
         end
 
-        it 'loads all the tutorials' do
+        it "loads all the tutorials" do
           expect(assigns(:tutorials)).to match_array([tutorial])
         end
 
-        it 'responds with json' do
+        it "responds with json" do
           json = JSON.parse(response.body)
-          expect(json['tutorials'].count).to eql(1)
-          expect(json['tutorials'][0]['id']).to eql(tutorial.id)
-          expect(json['tutorials'][0]['heading']).to eql(tutorial.heading)
-          expect(json['tutorials'][0]['description']).to eql(tutorial.description)
-          expect(json['tutorials'][0]['url']).to eql(tutorial.url)
-          expect(json['tutorials'][0]['submitter']).to eql(tutorial.user.id)
-          expect(json['tutorials'][0]['imageUrl']).to eql(tutorial.image_url)
+          expect(json["tutorials"].count).to eql(1)
+          json_tutorial = json["tutorials"][0]
+          expect(json_tutorial["id"]).to eql(tutorial.id)
+          expect(json_tutorial["heading"]).to eql(tutorial.heading)
+          expect(json_tutorial["description"]).to eql(tutorial.description)
+          expect(json_tutorial["url"]).to eql(tutorial.url)
+          expect(json_tutorial["submitter"]).to eql(tutorial.user.id)
+          expect(json_tutorial["imageUrl"]).to eql(tutorial.image_url)
         end
       end
 
       describe "#show" do
         let!(:tutorial) { create(:tutorial) }
 
-        it 'loads the single tutorial' do
+        it "loads the single tutorial" do
           xhr :get, :show, id: tutorial.id
           expect(assigns(:tutorial)).to eql(tutorial)
         end
spec/controllers/api/v2/users_controller_spec.rb
@@ -1,12 +1,14 @@
-require 'rails_helper'
+require "rails_helper"
 
 module Api
   module V2
     describe UsersController do
+      render_views
+
       describe "#index" do
         let!(:user) { create(:user) }
 
-        it 'loads all users' do
+        it "loads all users" do
           xhr :get, :index
           expect(assigns(:users)).to match_array([user])
         end
@@ -15,7 +17,7 @@ module Api
       describe "#show" do
         let!(:user) { create(:user) }
 
-        it 'loads the info on the user' do
+        it "loads the info on the user" do
           xhr :get, :show, id: user.id
           expect(assigns(:user)).to eql(user)
         end
spec/models/creation/repository_spec.rb
@@ -1,4 +1,4 @@
-require 'rails_helper'
+require "rails_helper"
 
 describe Creation::Repository do
   describe "#tagged" do
@@ -25,7 +25,7 @@ describe Creation::Repository do
     let!(:published_cake){ create(:creation, user: user) }
 
     before :each do
-      published_cake.photos.create(image: 'example.png', image_processing: nil)
+      published_cake.photos.create(image: "example.png", image_processing: nil)
     end
 
     let(:results) { subject.published }
@@ -37,11 +37,11 @@ describe Creation::Repository do
   end
 
   describe "#search" do
-    let(:cake) { create(:creation, name: 'Cake') }
-    let(:cup_cake) { create(:creation, name: 'Cup Cake') }
+    let(:cake) { create(:creation, name: "Cake") }
+    let(:cup_cake) { create(:creation, name: "Cup Cake") }
 
     it "returns cakes with a matching name" do
-      expect(subject.search('cake')).to match_array([cake])
+      expect(subject.search("cake")).to match_array([cake])
     end
   end
 
@@ -55,12 +55,12 @@ describe Creation::Repository do
       cookie.photos << create(:photo)
     end
 
-    it 'returns all cakes in a specific category' do
+    it "returns all cakes in a specific category" do
       cakes = subject.search_with(category: cake_category.slug)
       expect(cakes).to match_array([cake])
     end
 
-    it 'returns all cakes that match the search query' do
+    it "returns all cakes that match the search query" do
       cakes = subject.search_with(q: cake.name[0..6])
       expect(cakes).to match_array([cake])
     end
spec/support/authentication.rb
@@ -5,4 +5,10 @@ module Authentication
     allow(controller).to receive(:current_user).and_return(user)
     allow(controller).to receive(:user_signed_in?).and_return(true)
   end
+
+  def api_login(user)
+    encoded_credentials = ActionController::HttpAuthentication::Token.
+      encode_credentials(user.authentication_token)
+    request.env["HTTP_AUTHORIZATION"] = encoded_credentials
+  end
 end