diff --git a/app/controllers/classified_listings_controller.rb b/app/controllers/classified_listings_controller.rb index 2bdfbd853..3153207e5 100644 --- a/app/controllers/classified_listings_controller.rb +++ b/app/controllers/classified_listings_controller.rb @@ -1,4 +1,5 @@ class ClassifiedListingsController < ApplicationController + include ClassifiedListingsToolkit 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] @@ -28,7 +29,7 @@ class ClassifiedListingsController < ApplicationController end def create - @classified_listing = ClassifiedListing.new(classified_listing_params) + @classified_listing = ClassifiedListing.new(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) @@ -56,7 +57,7 @@ class ClassifiedListingsController < ApplicationController redirect_to "/listings" else @credits = current_user.credits.where(spent: false) - @classified_listing.cached_tag_list = classified_listing_params[:tag_list] + @classified_listing.cached_tag_list = listing_params[:tag_list] @organizations = current_user.organizations render :new end @@ -66,22 +67,16 @@ class ClassifiedListingsController < ApplicationController 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 listing_params[:action] == "bump" + bump_listing 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 + elsif listing_params[:action] == "unpublish" + unpublish_listing + elsif listing_params[:body_markdown].present? && @classified_listing.bumped_at > 24.hours.ago + update_listing_details end clear_listings_cache redirect_to "/listings" @@ -93,21 +88,13 @@ class ClassifiedListingsController < ApplicationController 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 + def 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 diff --git a/app/controllers/concerns/classified_listings_toolkit.rb b/app/controllers/concerns/classified_listings_toolkit.rb new file mode 100644 index 000000000..0fce6c748 --- /dev/null +++ b/app/controllers/concerns/classified_listings_toolkit.rb @@ -0,0 +1,38 @@ +module ClassifiedListingsToolkit + extend ActiveSupport::Concern + + def unpublish_listing + @classified_listing.published = false + @classified_listing.save + @classified_listing.remove_from_index! + end + + def publish_listing + @classified_listing.published = true + @classified_listing.save + @classified_listing.index! + end + + def update_listing_details + @classified_listing.title = listing_params[:title] if listing_params[:title] + @classified_listing.body_markdown = listing_params[:body_markdown] if listing_params[:body_markdown] + @classified_listing.tag_list = listing_params[:tag_list] if listing_params[:tag_list] + @classified_listing.category = listing_params[:category] if listing_params[:category] + @classified_listing.contact_via_connect = listing_params[:contact_via_connect] if listing_params[:contact_via_connect] + @classified_listing.save + end + + def bump_listing + @classified_listing.bumped_at = Time.current + @classified_listing.save + end + + def clear_listings_cache + cb = CacheBuster.new + cb.bust("/listings") + cb.bust("/listings?i=i") + cb.bust("/listings/#{@classified_listing.category}/#{@classified_listing.slug}") + cb.bust("/listings/#{@classified_listing.category}/#{@classified_listing.slug}?i=i") + cb.bust("/listings/#{@classified_listing.category}") + end +end diff --git a/app/controllers/internal/classified_listings_controller.rb b/app/controllers/internal/classified_listings_controller.rb index 8e489d86a..39e751b5f 100644 --- a/app/controllers/internal/classified_listings_controller.rb +++ b/app/controllers/internal/classified_listings_controller.rb @@ -1,4 +1,5 @@ class Internal::ClassifiedListingsController < Internal::ApplicationController + include ClassifiedListingsToolkit layout "internal" def index @@ -12,8 +13,10 @@ class Internal::ClassifiedListingsController < Internal::ApplicationController def update @classified_listing = ClassifiedListing.find(params[:id]) - @classified_listing.update!(listing_params) - reindex_and_bust_cache + handle_publish_status if listing_params[:published] + bump_listing if listing_params[:action] == "bump" + update_listing_details + clear_listings_cache flash[:success] = "Listing updated successfully" redirect_to "/internal/listings/#{@classified_listing.id}/edit" end @@ -28,15 +31,12 @@ class Internal::ClassifiedListingsController < Internal::ApplicationController private def listing_params - allowed_params = %i[published body_markdown title category tag_list] + allowed_params = %i[published body_markdown title category tag_list action] params.require(:classified_listing).permit(allowed_params) end - def reindex_and_bust_cache - @classified_listing.index! if @classified_listing.published - cb = CacheBuster.new - cb.bust("/listings") - cb.bust("/listings/#{@classified_listing.category}") - cb.bust("/listings/#{@classified_listing.category}/#{@classified_listing.path}") + def handle_publish_status + unpublish_listing if listing_params[:published] == "0" + publish_listing if listing_params[:published] == "1" end end diff --git a/app/views/internal/classified_listings/edit.html.erb b/app/views/internal/classified_listings/edit.html.erb index 55d09e7c3..f11ac352c 100644 --- a/app/views/internal/classified_listings/edit.html.erb +++ b/app/views/internal/classified_listings/edit.html.erb @@ -22,12 +22,15 @@ <%= form.select :category, options_for_select(ClassifiedListing.select_options_for_categories.map(&:last), @classified_listing.category) %>
<%= form.label :published %> - <%= form.check_box :published %> + <%= form.check_box :published, checked: @classified_listing.published? %>
<%= form.submit "Update Listing", class: "btn btn-lg btn-primary" %> -
<% end %> - +<%= form_for [:internal, @classified_listing] do |f| %> + + <%= f.submit "Bump Listing ⏫", class: "btn btn-lg btn-primary" %> +<% end %> +
<%= link_to "Destroy Listing", url_for(action: :destroy, id: @classified_listing.id), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger" %>


diff --git a/app/views/social_previews/listing.html.erb b/app/views/social_previews/listing.html.erb new file mode 100644 index 000000000..b20957530 --- /dev/null +++ b/app/views/social_previews/listing.html.erb @@ -0,0 +1,13 @@ +
+

<%= @listing.title %>

+
+ <%= @listing.processed_html.html_safe %> +
+
+ <% if @listing.tags.present? %> + <%= @listing.tags.each do |tag| %> + <%= tag.name %> + <% end %> + <% end %> +
+