Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Commit

Permalink
controllers: Pull the scoped_search concern out
Browse files Browse the repository at this point in the history
This fixes the last complaint of codeclimate about those controllers.
  • Loading branch information
DavidS committed Aug 10, 2014
1 parent 4e2d0bf commit c100b36
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
18 changes: 18 additions & 0 deletions app/controllers/concerns/default_search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# everything concerning the scoped_search implementation
module DefaultSearch
def default_index(klass, params)
klass.search_for(params[:search], order: params[:order])
rescue => e
flash[:error] = e.to_s
klass.search_for ''
end

def auto_complete_search_impl(klass, search_params)
begin
@items = klass.complete_for(search_params)
rescue ScopedSearch::QueryNotSupported => e
@items = [{ error: e.to_s }]
end
render json: @items
end
end
13 changes: 3 additions & 10 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
class PeopleController < ApplicationController
include DefaultResponses
include DefaultSearch

before_action :set_person, only: [:show, :edit, :update, :destroy]

# GET /people
# GET /people.json
def index
@people = Person.search_for(params[:search], :order => params[:order])
rescue => e
flash[:error] = e.to_s
@people = Person.search_for ''
@people = default_index(Person, params)
end

def auto_complete_search
begin
@items = Person.complete_for(params[:search])
rescue ScopedSearch::QueryNotSupported => e
@items = [{:error =>e.to_s}]
end
render :json => @items
auto_complete_search_impl(Person, params[:search])
end

# GET /people/1
Expand Down
20 changes: 4 additions & 16 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
class ProjectsController < ApplicationController
include DefaultResponses
include DefaultSearch

before_action :set_project, only: [:show, :add_person, :edit, :update, :destroy]

# GET /projects
# GET /projects.json
def index
@projects = Project.search_for(params[:search], :order => params[:order])
rescue => e
flash[:error] = e.to_s
@projects = Project.search_for ''
@projects = default_index(Project, params)
end

def auto_complete_search
begin
@items = Project.complete_for(params[:search])
rescue ScopedSearch::QueryNotSupported => e
@items = [{:error =>e.to_s}]
end
render :json => @items
auto_complete_search_impl(Project, params[:search])
end

def auto_complete_people_search
begin
@items = Person.complete_for(params[:people_search])
rescue ScopedSearch::QueryNotSupported => e
@items = [{:error =>e.to_s}]
end
render :json => @items
auto_complete_search_impl(Person, params[:people_search])
end

# GET /projects/1
Expand Down

0 comments on commit c100b36

Please sign in to comment.