Commit 5cfd25f7
Changed files (7)
app
controllers
services
application
spec
controllers
api
app/controllers/api/v1/creations_controller.rb
@@ -4,8 +4,13 @@ module Api
before_filter :restrict_access
respond_to :json
+ def initialize(repository = CreationRepository.new)
+ @repository = repository
+ super()
+ end
+
def index
- @creations = Creation.visible_creations
+ @creations = @repository.visible_creations
end
private
@@ -15,7 +20,6 @@ module Api
User.exists?(:authentication_token => token)
end
end
-
end
end
end
app/controllers/application_controller.rb
@@ -20,4 +20,3 @@ class ApplicationController < ActionController::Base
@top_members = User.order(:creations_count => :desc).limit(3)
end
end
-
app/controllers/search_controller.rb
@@ -1,10 +1,16 @@
class SearchController < ApplicationController
+ def initialize(creations_repository = CreationRepository.new)
+ super()
+ @creations_repository = creations_repository
+ end
+
+
def index
@search = params[:q]
if @search.blank?
redirect_to(root_url)
else
- @creations = Creation.includes(:user).search(@search).page(params[:page]).per(100)
+ @creations = @creations_repository.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.rb
@@ -37,15 +37,4 @@ class Creation < ActiveRecord::Base
def liked_by(user)
favorites.find_or_create_by(user: user)
end
-
- class << self
- def search(query)
- sql_search = "%#{query}%"
- Creation.where("upper(name) like upper(?) OR upper(story) like upper(?)", sql_search, sql_search)
- end
-
- def visible_creations
- Creation.distinct.includes(:user).joins(:photos).where(is_restricted: false, 'photos.image_processing' => nil)
- end
- end
end
app/models/creation_repository.rb
@@ -0,0 +1,14 @@
+class CreationRepository
+ def initialize(connection = Creation)
+ @connection = connection
+ end
+
+ def search(query)
+ sql_search = "%#{query}%"
+ @connection.includes(:user).where("upper(name) like upper(?) OR upper(story) like upper(?)", sql_search, sql_search)
+ end
+
+ def visible_creations
+ @connection.distinct.includes(:user).joins(:photos).where(is_restricted: false, 'photos.image_processing' => nil)
+ end
+end
app/services/application/find_all_creations_query.rb
@@ -1,5 +1,5 @@
class FindAllCreationsQuery
- def initialize(repository = Creation)
+ def initialize(repository = CreationRepository.new)
@repository = repository
end
spec/controllers/api/v1/creations_controller_spec.rb
@@ -4,10 +4,12 @@ describe Api::V1::CreationsController do
context "when signed in" do
let(:creation) { create(:creation) }
let(:user) { create(:user) }
+ let(:repository) { double }
before :each do
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(user.authentication_token)
- Creation.stub(:visible_creations).and_return([creation])
+ CreationRepository.any_instance.stub(:visible_creations).and_return([creation])
+
get :index, :format => :json
end