docbrown/app/controllers/listings_controller.rb
Michael Kohl 7f75f99560
[deploy] Rename classified listings (#7910)
* Change models and related files

* Update controllers and specs

* More renaming

* Seek and destroy, I mean search and replace

* Round up the stragglers

* Ground control to Major Travis...

* More fixes

* PR feedback

* Various fixes

* Rename view

* Fix list query builder

* Unify request specs

* Fix some API spec errors

* Fix remaining API specs

* Make spec conform to API

* Fix leftover problems

* Fix JS tests

* Fix column name in select

* Fix API specs

* Fix search specs

* Paging Mr. Travis
2020-05-27 13:35:09 +00:00

127 lines
3.1 KiB
Ruby

class ListingsController < ApplicationController
include ListingsToolkit
before_action :check_limit, only: [:create]
JSON_OPTIONS = {
only: %i[
title processed_html tag_list category id user_id slug contact_via_connect location
],
include: {
author: { only: %i[username name], methods: %i[username profile_image_90] }
}
}.freeze
before_action :set_listing, only: %i[edit update destroy]
before_action :set_cache_control_headers, only: %i[index]
before_action :raise_suspended, only: %i[new create update]
before_action :authenticate_user!, only: %i[edit update new dashboard]
after_action :verify_authorized, only: %i[edit update]
def index
published_listings = Listing.where(published: true)
@displayed_listing = published_listings.find_by(slug: params[:slug]) if params[:slug]
if params[:view] == "moderate"
not_found unless @displayed_listing
return redirect_to edit_internal_listing_path(id: @displayed_listing.id)
end
@listings =
if params[:category].blank?
published_listings.
order("bumped_at DESC").
includes(:user, :organization, :taggings).
limit(12)
else
Listing.none
end
@listings_json = @listings.to_json(JSON_OPTIONS)
@displayed_listing_json = @displayed_listing.to_json(JSON_OPTIONS)
# TODO: [mkohl] Can we change this to listings-#{params[:category]}?
set_surrogate_key_header "classified-listings-#{params[:category]}"
end
def new
@listing = Listing.new
@organizations = current_user.organizations
@credits = current_user.credits.unspent
end
def create
super
end
def update
super
end
def edit
authorize @listing
@organizations = current_user.organizations
@credits = current_user.credits.unspent
end
def dashboard
@listings = current_user.listings.
includes(:organization, :taggings)
organizations_ids = current_user.organization_memberships.
where(type_of_user: "admin").
pluck(:organization_id)
@orgs = Organization.where(id: organizations_ids)
@org_listings = Listing.where(organization_id: organizations_ids)
@user_credits = current_user.unspent_credits_count
end
def delete_confirm
@listing = Listing.find_by(slug: params[:slug])
not_found unless @listing
authorize @listing
end
def destroy
authorize @listing
@listing.destroy!
redirect_to "/listings/dashboard", notice: "Listing was successfully deleted."
end
private
def process_no_credit_left
redirect_to credits_path, notice: "Not enough available credits"
end
def process_successful_draft
redirect_to "/listings/dashboard"
end
def process_unsuccessful_draft
render :new
end
def process_successful_creation
redirect_to listings_path
end
def process_unsuccessful_creation
render :new
end
def process_unsuccessful_update
render :edit
end
def process_after_update
redirect_to "/listings"
end
def process_after_unpublish
redirect_to "/listings/dashboard"
end
def check_limit
rate_limit!(:listing_creation)
end
end