diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 9dffb31..49a0a59 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -5,11 +5,10 @@ class PostsController < ApplicationController :confirm] def index - if check_for_search_params - @posts = post_search - else - @posts = Post.active - end + @posts = Post.active + .filter_near(search_params[:location]) + .filter_query(search_params[:query]) + .filter_type(search_params[:type]) end def new @@ -89,40 +88,7 @@ def find_post_params params.permit(:id, :validation) end - def check_for_search_params - [:type, :location, :query].any? { |k| params.key?(k) } - end - - def query_location(posts) - if params[:location].blank? - posts - else - posts.near(params[:location]) - end - end - - def query_description(posts) - if params[:query].blank? - posts - else - posts.search(params[:query]) - end - end - - def filter_type(posts) - if params[:type] == "available" - posts.available - elsif params[:type] == "needed" - posts.needed - else - posts - end - end - - def post_search - posts = Post.active - posts = query_location(posts) - posts = query_description(posts) - filter_type(posts) + def search_params + params.permit(:location, :query, :type) end end diff --git a/app/models/post.rb b/app/models/post.rb index aebc2d9..96eab06 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -57,6 +57,32 @@ def address [street, city, state, postal_code, country].compact.join(", ") end + def self.filter_type(type) + if type.present? + # TODO: For rails 5 we can just do post_type: type + # (see: rails/rails#18387) + where(post_type: post_types[type]) + else + all + end + end + + def self.filter_near(location) + if location.present? + near(location) + else + all + end + end + + def self.filter_query(query) + if query.present? + search(query) + else + all + end + end + def self.generate_validation require "securerandom" # a collision here has low probability, but might as well check diff --git a/spec/controllers/post_spec.rb b/spec/controllers/post_spec.rb index 332e050..30c458d 100644 --- a/spec/controllers/post_spec.rb +++ b/spec/controllers/post_spec.rb @@ -2,10 +2,10 @@ describe PostsController, type: :controller do describe "GET #index" do - it "responds successfully with an HTTP 200 status code" do + it "responds successfully with an 200 OK HTTP status code" do get :index - expect(response).to be_success - expect(response).to have_http_status(200) + + expect(response).to have_http_status(:ok) end it "renders the index template" do @@ -21,7 +21,7 @@ expect(assigns(:posts)).to match_array([post1, post2]) end - it "searches for location" do + it "searches by location" do post1 = create( :boston_post, show: true @@ -37,7 +37,26 @@ expect(assigns(:posts)).to match_array([post1]) end - it "searches for description" do + it "searches by title" do + title = "Xyz" + + post1 = create( + :post, + title: title, + show: true + ) + + create( + :post, + show: true + ) + + get :index, query: title + + expect(assigns(:posts)).to match_array([post1]) + end + + it "searches by description" do post1 = create( :post, description: "Need housing in Boston", @@ -54,7 +73,7 @@ expect(assigns(:posts)).to match_array([post1]) end - it "searches for description and location" do + it "searches by description and location" do post1 = create( :boston_post, description: "Need housing in Boston", diff --git a/spec/features/post_spec.rb b/spec/features/post_spec.rb index beaec9e..e4914b3 100644 --- a/spec/features/post_spec.rb +++ b/spec/features/post_spec.rb @@ -28,7 +28,7 @@ expect(page).to have_content post.title end - it "shows a post with a query in it's description" do + it "shows a post with a query in its description" do post1 = create( :available_post, description: "I want some tofu pups",