Commit 09cf5e4b
Changed files (5)
app
controllers
models
creation
spec
controllers
models
creation
app/controllers/search_controller.rb
@@ -1,7 +1,7 @@
class SearchController < ApplicationController
- def initialize(creations_repository = CreationRepository.new)
+ def initialize(cakes = Spank::IOC.resolve(:cakes))
+ @cakes = cakes
super()
- @creations_repository = creations_repository
end
def index
@@ -9,7 +9,7 @@ class SearchController < ApplicationController
if @search.blank?
redirect_to(root_url)
else
- @creations = @creations_repository.search(@search).page(params[:page]).per(100)
+ @creations = @cakes.search(@search).page(params[:page]).per(100)
@members = User.includes(:avatar).where("upper(name) like upper(?)", "%#{@search}%")
@tutorials = Tutorial.where("upper(heading) like upper(?) OR upper(description) like upper(?)", "%#{@search}%", "%#{@search}%")
end
app/models/creation/repository.rb
@@ -11,6 +11,14 @@ class Creation
connection.includes([:user, :photos]).tagged(tag)
end
+ def search(query)
+ connection.includes(:user, :photos).where(["upper(name) like :query OR upper(story) like :query", { query: "%#{query.upcase}%" }])
+ end
+
+ def visible_creations
+ connection.unscoped.distinct.includes(:user, :photos).joins(:photos).where('photos.image_processing' => nil)
+ end
+
private
attr_reader :connection
app/models/creation_repository.rb
@@ -1,19 +0,0 @@
-class CreationRepository
- delegate :find, to: :connection
-
- def initialize(connection = Creation)
- @connection = connection
- end
-
- def search(query)
- connection.includes(:user, :photos).where(["upper(name) like :query OR upper(story) like :query", { query: "%#{query.upcase}%" }])
- end
-
- def visible_creations
- connection.unscoped.distinct.includes(:user, :photos).joins(:photos).where('photos.image_processing' => nil)
- end
-
- private
-
- attr_reader :connection
-end
spec/controllers/search_controller_spec.rb
@@ -6,9 +6,10 @@ describe SearchController do
before { get :index }
it "should redirect you to the home page" do
- response.should redirect_to(root_url)
+ expect(response).to redirect_to(root_url)
end
end
+
context "when a valid search query is given" do
let!(:user) { create(:user, :name => 'cake') }
let!(:bob) { create(:user, :name => 'bob') }
@@ -19,32 +20,32 @@ describe SearchController do
before { get :index, { :q => 'cake' } }
- it "should be successful" do
- response.should be_success
+ it "returns a successful response" do
+ expect(response).to be_success
end
- it "should return all creations that have a matching name" do
- assigns(:creations).should include(cake)
+ it "returns all creations that have a matching name" do
+ expect(assigns(:creations)).to include(cake)
end
- it "should not include cakes that do not match" do
- assigns(:creations).should_not include(donut)
+ it "does not include cakes that do not match" do
+ expect(assigns(:creations)).to_not include(donut)
end
- it "should return all makers that match" do
- assigns(:members).should include(user)
+ it "returns all makers that match" do
+ expect(assigns(:members)).to include(user)
end
- it "should not include makers with names that do not match" do
- assigns(:members).should_not include(bob)
+ it "does not include makers with names that do not match" do
+ expect(assigns(:members)).to_not include(bob)
end
- it "should return all tutorials that match" do
- assigns(:tutorials).should include(tutorial)
+ it "returns all tutorials that match" do
+ expect(assigns(:tutorials)).to include(tutorial)
end
- it "should not return tutorials that do not match" do
- assigns(:tutorials).should_not include(other_tutorial)
+ it "does not return tutorials that do not match" do
+ expect(assigns(:tutorials)).to_not include(other_tutorial)
end
end
end
spec/models/creation/repository_spec.rb
@@ -19,4 +19,31 @@ describe Creation::Repository do
expect(subject.tagged(tag)).to_not include(untagged_cake)
end
end
+
+ describe "#visible_creations" do
+ let!(:user){ create(:user) }
+ let!(:published_cake){ create(:creation, user: user) }
+
+ before :each do
+ published_cake.photos.create(image: 'example.png', image_processing: nil)
+ end
+
+ let(:results) { subject.visible_creations }
+
+ it "returns cakes that do not have photos that are processing" do
+ expect(results.count).to eql(1)
+ expect(results).to include(published_cake)
+ end
+ end
+
+ describe "#search" do
+ let(:cake) { create(:creation, name: 'Cake') }
+ let(:cup_cake) { create(:creation, name: 'Cup Cake') }
+
+ it "returns cakes with a matching name" do
+ results = subject.search('cake')
+ expect(results).to include(cake)
+ expect(results).to_not include(cup_cake)
+ end
+ end
end