docbrown/app/controllers/profile_pins_controller.rb
Jeremy Friesen c7902a3261
Favor destroy_by over where.first&.destroy (#15868)
* Favor `destroy_by` over `where.first&.destroy`

We can use [ActiveRecord::Relation.destroy_by][1] instead of the method
chain.

[1]:https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy_by

* Restoring custom behavior

Those constants are not ActiveRecord::Base objects, so don't implement
the method.
2021-12-22 23:55:52 -05:00

38 lines
1.1 KiB
Ruby

class ProfilePinsController < ApplicationController
before_action :authenticate_user!, only: %i[create update]
def create
@profile_pin = ProfilePin.new
@profile_pin.profile_id = current_user.id
@profile_pin.profile_type = "User"
@profile_pin.pinnable_id = profile_pin_params[:pinnable_id].to_i
@profile_pin.pinnable_type = "Article"
if @profile_pin.save
flash[:success] = "📌 Pinned! (pinned posts display chronologically, 5 max)"
else
flash[:error] = "You can only have five pins"
end
redirect_back(fallback_location: "/dashboard")
bust_user_profile
end
def update
# for removing pinnable
current_user.profile_pins.destroy_by(id: params[:id])
bust_user_profile
flash[:pins_success] = "🗑 Pin removed"
redirect_back(fallback_location: "/dashboard")
end
private
def profile_pin_params
params.require(:profile_pin).permit(:pinnable_id)
end
def bust_user_profile
cache_bust = EdgeCache::Bust.new
cache_bust.call(current_user.path)
cache_bust.call("#{current_user.path}?i=i")
end
end