* Allow user to have many orgs * Allow users to handle multi orgs in settings * Make rounded buttons inline * Add multi org function to dashboards * Fix merge conflicts * Fix mistake in merge conflict fix oops * Display the correct membership level * Fix accessibility issues * Display organizations for article editors * Handle submitting org id with preact editors * Make listings work with multiple organizations * Allow listings to have multiple orgs on create * Display the correct number of credits for each org * Move script tag to Webpack * Allow multi orgs for purchasing and viewing credits * Use OrganizationMembership as authorization check * Display multiple organizations for notifications * Allow dashboard to be viewable under multi-orgs * Remove unused method * Add multi-org functionality for article editors * Show pro dashboard buttons for member+ org levels * Leave the correct organization * Allow article API to change org id * Add left-out authorization method oops * Make nav buttons a bit more clear * Fix merge conflict * Fix adding org id for /api/articles and tests * Fix tests for org policy * Use proper logic for displaying org members * Update org actions with new authorization * Use correct org when creating a listing * Remove additional payment charge oops * Mark org notifications as read with authorization * Remove deprecated post_as_organization attribute * Use new org_admin syntax * Remove deprecated org logic for article create and update * Default all RSS posts to not belong to any org * Render org_member page for guest users * Update org policy spec to work with multi orgs * Use org_membership for org traits and move identity code * Use org_member trait * Update to work with multi-orgs * Validate article's org_id if param org_id is blank * Make a let variable * Remove unnecessary eager load for credits * Fix HTML structure and org logic for non-org users * Update credits spec for multi-org * Add test for failed payment when purchased by org * Lint listings_spec * Test that the listing was created under the user * Add tests for POST /listings multi-org * Use double quotes for classes * Fix /manage and a few other multi-org bugs * Fix test for multi org * Use correct method SQL exists? not Rails exist? * Fix reads spec for multi-org * Fix org_controller actions to work with multi org * Test only multi org and not old usage and fix leave_org * Fix org showing user profile img test for multi-org * Fix org logic for users with no orgs * Remove switch org functionality * Update tests and add hidden param for org id * Redirect to the specific organization * Test other org button actions * Use settings_notice instead of legacy notice and refactor * Fix weird extra end issue prob from merge conflicts * Test for with new flash key * Fix user_views_org tests for multi-org * Test for new flash message * Update snapshot with new a11y html * Move styling to stylesheet * Add site admins functionality * Move org_member? method in user model and refactor * Use unspent_credits_count for organizations * Add tests for /listings/new and minor bug fixes * Use .present? in case of empty array * Fix a lingering deprecated method * Use greater than 1 for random numbers * Add tests for counting spent and unspent credits
113 lines
4.8 KiB
Ruby
113 lines
4.8 KiB
Ruby
class ClassifiedListingsController < ApplicationController
|
|
before_action :set_classified_listing, only: %i[edit update]
|
|
before_action :set_cache_control_headers, only: %i[index]
|
|
after_action :verify_authorized, only: %i[edit update]
|
|
before_action :authenticate_user!, only: %i[edit update new]
|
|
|
|
def index
|
|
@displayed_classified_listing = ClassifiedListing.find_by!(slug: params[:slug]) if params[:slug]
|
|
mod_page if params[:view] == "moderate"
|
|
@classified_listings = if params[:category].blank?
|
|
ClassifiedListing.where(published: true).order("bumped_at DESC").limit(12)
|
|
else
|
|
[]
|
|
end
|
|
set_surrogate_key_header "classified-listings-#{params[:category]}"
|
|
end
|
|
|
|
def new
|
|
@classified_listing = ClassifiedListing.new
|
|
@organizations = current_user.organizations
|
|
@credits = current_user.credits.where(spent: false)
|
|
end
|
|
|
|
def edit
|
|
authorize @classified_listing
|
|
@organizations = current_user.organizations
|
|
@credits = current_user.credits.where(spent: false)
|
|
end
|
|
|
|
def create
|
|
@classified_listing = ClassifiedListing.new(classified_listing_params)
|
|
@classified_listing.user_id = current_user.id
|
|
@number_of_credits_needed = ClassifiedListing.cost_by_category(@classified_listing.category)
|
|
@org = Organization.find_by(id: @classified_listing.organization_id)
|
|
available_org_credits = @org.credits.where(spent: false) if @org
|
|
available_individual_credits = current_user.credits.where(spent: false)
|
|
|
|
if @org && available_org_credits.size >= @number_of_credits_needed
|
|
create_listing(available_org_credits)
|
|
elsif available_individual_credits.size >= @number_of_credits_needed
|
|
create_listing(available_individual_credits)
|
|
else
|
|
redirect_to "/credits"
|
|
end
|
|
end
|
|
|
|
def create_listing(credits)
|
|
@classified_listing.bumped_at = Time.current
|
|
@classified_listing.published = true
|
|
# this will 500 for now if they don't belong in the org
|
|
authorize @classified_listing, :authorized_organization_poster? if @classified_listing.organization_id.present?
|
|
if @classified_listing.save
|
|
clear_listings_cache
|
|
credits.limit(@number_of_credits_needed).update_all(spent: true)
|
|
@classified_listing.index!
|
|
redirect_to "/listings"
|
|
else
|
|
@credits = current_user.credits.where(spent: false)
|
|
@classified_listing.cached_tag_list = classified_listing_params[:tag_list]
|
|
@organizations = current_user.organizations
|
|
render :new
|
|
end
|
|
end
|
|
|
|
def update
|
|
authorize @classified_listing
|
|
available_credits = current_user.credits.where(spent: false)
|
|
number_of_credits_needed = ClassifiedListing.cost_by_category(@classified_listing.category) # Bumping
|
|
if params[:classified_listing][:action] == "bump"
|
|
@classified_listing.bumped_at = Time.current
|
|
if available_credits.size >= number_of_credits_needed
|
|
@classified_listing.save
|
|
available_credits.limit(number_of_credits_needed).update_all(spent: true)
|
|
end
|
|
elsif params[:classified_listing][:action] == "unpublish"
|
|
@classified_listing.published = false
|
|
@classified_listing.save
|
|
@classified_listing.remove_from_index!
|
|
elsif params[:classified_listing][:body_markdown].present? && @classified_listing.bumped_at > 24.hours.ago
|
|
@classified_listing.title = params[:classified_listing][:title] if params[:classified_listing][:title]
|
|
@classified_listing.body_markdown = params[:classified_listing][:body_markdown] if params[:classified_listing][:body_markdown]
|
|
@classified_listing.tag_list = params[:classified_listing][:tag_list] if params[:classified_listing][:tag_list]
|
|
@classified_listing.contact_via_connect = params[:classified_listing][:contact_via_connect] if params[:classified_listing][:contact_via_connect]
|
|
@classified_listing.save
|
|
end
|
|
clear_listings_cache
|
|
redirect_to "/listings"
|
|
end
|
|
|
|
private
|
|
|
|
def mod_page
|
|
redirect_to "/internal/listings/#{@displayed_classified_listing.id}/edit"
|
|
end
|
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
def set_classified_listing
|
|
@classified_listing = ClassifiedListing.find(params[:id])
|
|
end
|
|
|
|
# Never trust parameters from the scary internet, only allow a specific list through.
|
|
def classified_listing_params
|
|
accessible = %i[title body_markdown category tag_list contact_via_connect organization_id action]
|
|
params.require(:classified_listing).permit(accessible)
|
|
end
|
|
|
|
def clear_listings_cache
|
|
CacheBuster.new.bust("/listings")
|
|
CacheBuster.new.bust("/listings?i=i")
|
|
CacheBuster.new.bust("/listings/#{@classified_listing.category}/#{@classified_listing.slug}")
|
|
CacheBuster.new.bust("/listings/#{@classified_listing.category}/#{@classified_listing.slug}?i=i")
|
|
end
|
|
end
|