Skip to content

Instantly share code, notes, and snippets.

@bearded-avenger
Created September 3, 2020 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bearded-avenger/59c96c3c7d1423b7fa684ae7b388fa8e to your computer and use it in GitHub Desktop.
Save bearded-avenger/59c96c3c7d1423b7fa684ae7b388fa8e to your computer and use it in GitHub Desktop.
class BookmarksController < ApplicationController
BOOKMARKABLES = {
'Course' => Course,
'Lesson' => Lesson,
'Post' => Post,
'LiveStream' => LiveStream,
'Download' => Download,
'Project' => Project,
'Exercise' => Exercise,
'Quiz' => Quiz
}
before_action :set_bookmark, :set_authorized_editor, only: :destroy
before_action :find_bookmarkable, only: :create
before_action :redirect_if_plugin_not_activated, :authenticate_customer!
include EventsHelper
# POST /bookmarks
def create
@bookmark = @bookmarkable.bookmarks.new
@bookmark.user = current_user
@bookmark.customer = current_customer
respond_to do |format|
if @bookmark.save
TenantEventJob.perform_later(current_tenant.id, (current_customer ? current_customer.id : nil),'created_bookmark','Bookmark', @bookmark.id, tenant_event_params )
format.html {redirect_to polymorphic_path(@bookmark.bookmarkable), success: 'Bookmark was successfully created.'}
format.js
else
format.html {redirect_to polymorphic_path(@bookmark.bookmarkable), danger: 'Bookmark not created.'}
format.js
end
end
end
# DELETE /bookmarks/1
def destroy
respond_to do |format|
if @bookmark.destroy
format.html {redirect_to polymorphic_path(@bookmark.bookmarkable), success: 'Bookmark was successfully destroyed.'}
format.js
else
format.html {redirect_to polymorphic_path(@bookmark.bookmarkable), danger: 'Bookmark not destroyed.'}
format.js
end
end
end
private
def set_bookmark
@bookmark = Bookmark.find(params[:id])
end
def find_bookmarkable
if params[:bookmarkable_type].present?
klass = BOOKMARKABLES.fetch(params[:bookmarkable_type])
@bookmarkable = klass.find(params[:bookmarkable_id])
end
end
def set_authorized_editor
redirect_to polymorphic_path(@bookmark.bookmarkable) unless (
( current_customer && (current_customer == @bookmark.customer) ) ||
( current_user && (current_user == @bookmark.user) ) ||
( current_user && current_user.can_access_site?(current_tenant) )
)
end
def redirect_if_plugin_not_activated
redirect_to site_root_path unless current_tenant.plugin_activated?('bookmarks')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment