Refactored packages controller. Tidied up a lot. Same functionality.
This commit is contained in:
parent
bab9c89ddb
commit
0382c09361
1 changed files with 24 additions and 29 deletions
|
|
@ -1,39 +1,12 @@
|
|||
class PackagesController < ApplicationController
|
||||
def index
|
||||
@packages = Package.includes(:screenshots)
|
||||
|
||||
if params[:query]
|
||||
@packages = @packages.text_search(params[:query])
|
||||
end
|
||||
|
||||
unless params[:search].blank?
|
||||
logger.debug "Searching for #{params[:search]}"
|
||||
@packages = @packages.general_search(params[:search])
|
||||
end
|
||||
|
||||
# Limit the packages to those that have approved screenshots.
|
||||
# Also eager-load the screenshots.
|
||||
if params[:show]=='onlywith'
|
||||
@packages = @packages.where("screenshots.approved"=>true)
|
||||
end
|
||||
|
||||
if params[:show]=='with'
|
||||
@packages = @packages.with_screenshots
|
||||
logger.debug 'Limiting packages to those with screenshots'
|
||||
elsif params[:show]=='without'
|
||||
@packages = @packages.without_screenshots
|
||||
logger.debug 'Limiting packages to those without screenshots'
|
||||
end
|
||||
end
|
||||
|
||||
def list
|
||||
index
|
||||
@packages = query_packages
|
||||
@packages = @packages.paginate(page: params[:page], per_page: 6)
|
||||
render 'packages/index-list.slim'
|
||||
end
|
||||
|
||||
def grid
|
||||
index
|
||||
@packages = query_packages
|
||||
@packages = @packages.paginate(page: params[:page], per_page: 24)
|
||||
render 'packages/index-grid.slim'
|
||||
end
|
||||
|
|
@ -99,4 +72,26 @@ class PackagesController < ApplicationController
|
|||
disposition: 'inline',
|
||||
status: 404
|
||||
end
|
||||
|
||||
# Return packages matching the criteria given by parameters
|
||||
def query_packages
|
||||
packages = Package.includes(:screenshots)
|
||||
|
||||
# text search
|
||||
if params[:search].present?
|
||||
logger.debug "Searching for #{params[:search]}"
|
||||
packages = packages.general_search(params[:search])
|
||||
end
|
||||
|
||||
case params[:show]
|
||||
when 'with'
|
||||
packages = packages.with_screenshots
|
||||
logger.debug 'Limiting packages to those with screenshots'
|
||||
when 'without'
|
||||
packages = packages.without_screenshots
|
||||
logger.debug 'Limiting packages to those without screenshots'
|
||||
end
|
||||
|
||||
return packages
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue