Commit 471269cb

mo khan <mo@mokhan.ca>
2014-10-19 02:31:55
move search filtering into repository.
1 parent 8f4898e
Changed files (2)
app
models
services
app/models/creation/repository.rb
@@ -18,8 +18,25 @@ class Creation
       connection.includes(:user, :photos).search(query)
     end
 
+    def search_with(params)
+      filtered_by(search_filters_for(params))
+    end
+
     private
 
     attr_reader :connection
+
+    def search_filters_for(params)
+      [
+        ->(cakes) { cakes.published },
+        ->(cakes) { params[:category].blank? ? cakes.all : cakes.where(category: Category.find_by(slug: params[:category].downcase)) },
+        ->(cakes) { params[:q].blank? ? cakes.all : cakes.search(params[:q]) },
+        ->(cakes) { cakes.order(created_at: sort(params)) },
+      ]
+    end
+
+    def sort(params)
+      params[:sort] == "oldest" ? :asc : :desc
+    end
   end
 end
app/services/application/all_cakes_query.rb
@@ -4,21 +4,6 @@ class AllCakesQuery
   end
 
   def fetch(params)
-    @repository.filtered_by(search_filters_for(params))
-  end
-
-  private
-
-  def search_filters_for(params)
-    [
-      ->(cakes) { cakes.published },
-      ->(cakes) { params[:category].blank? ? cakes.all : cakes.where(category: Category.find_by(slug: params[:category].downcase)) },
-      ->(cakes) { params[:q].blank? ? cakes.all : cakes.search(params[:q]) },
-      ->(cakes) { cakes.order(created_at: sort(params)) },
-    ]
-  end
-
-  def sort(params)
-    params[:sort] == "oldest" ? :asc : :desc
+    @repository.search_with(params)
   end
 end