docbrown/app/controllers/admin/listings_controller.rb
Josh Puetz 1c566e0ec4
[deploy] Move /internal to `/admin (#9639)
* First draft - all the big changes

* Changing some more references to 'internal'

* Relocate internal request tests to admin

* Relocate internal system tests to admin

* Fix trailing space

* Test fix

* Move queries from internal to admin

* Docs updates

* Rename internal stimuls controllers to admin (plus docs)

* Rename admin layout

* Fix routing after rebase

* Fixes for latest added admin interfaces

* Serviceworker ignore paths
2020-08-07 10:36:26 -04:00

56 lines
1.5 KiB
Ruby

module Admin
class ListingsController < Admin::ApplicationController
include ListingsToolkit
ALLOWED_PARAMS = %i[
published body_markdown title category listing_category_id tag_list action
].freeze
layout "admin"
def index
@listings =
Listing.includes(%i[user listing_category])
.page(params[:page]).order(bumped_at: :desc).per(50)
@listings = @listings.published unless include_unpublished?
@listings = @listings.in_category(params[:filter]) if params[:filter].present?
end
def edit
@listing = Listing.find(params[:id])
end
def update
@listing = Listing.find(params[:id])
handle_publish_status if listing_params[:published]
bump_listing(@listing.cost) if listing_params[:action] == "bump"
update_listing_details
clear_listings_cache
flash[:success] = "Listing updated successfully"
redirect_to edit_admin_listing_path(@listing)
end
def destroy
@listing = Listing.find(params[:id])
@listing.destroy
flash[:warning] = "'#{@listing.title}' was destroyed successfully"
redirect_to admin_listings_path
end
private
private_constant :ALLOWED_PARAMS
def listing_params
params.require(:listing).permit(ALLOWED_PARAMS)
end
def handle_publish_status
unpublish_listing if listing_params[:published] == "0"
publish_listing if listing_params[:published] == "1"
end
def include_unpublished?
params[:include_unpublished] == "1"
end
end
end