Skip to content

Instantly share code, notes, and snippets.

@bearded-avenger
Created September 10, 2020 23:03
Show Gist options
  • Save bearded-avenger/6f8951ec952061be4e9c3e0e1ae304da to your computer and use it in GitHub Desktop.
Save bearded-avenger/6f8951ec952061be4e9c3e0e1ae304da to your computer and use it in GitHub Desktop.
class Customer < ApplicationRecord
has_many :following_playlists, class_name:'PlaylistFollower', dependent: :destroy
def follow_playlist!(playlist_id)
following_playlists.create(playlist_id: playlist_id)
end
def unfollow_playlist!(playlist_id)
following_playlists.find_by(playlist_id: playlist_id).destroy
end
def is_following_playlist?(playlist_id)
following_playlists.exists?(playlist_id:playlist_id)
end
end
class PlaylistFollower < ApplicationRecord
belongs_to :site
belongs_to :playlist
belongs_to :customer
acts_as_tenant :site
validates_uniqueness_to_tenant :playlist_id, scope: :customer_id
end
class PlaylistFollowersController < ApplicationController
before_action :redirect_if_plugin_not_activated, :set_playlist, :authenticate_customer!
def follow
respond_to do |format|
if current_customer.follow_playlist! @playlist.id
TenantEventJob.perform_later(current_tenant.id, (current_customer ? current_customer.id : nil),'followed_playlist','Playlist', @playlist.id, tenant_event_params )
format.html{ redirect_back(fallback_location:root_path, success: 'You are now following this playlist!') }
format.js {}
else
format.html{ redirect_to :back, notice: 'Oh no! Something happened' }
format.js {}
end
end
end
def unfollow
respond_to do |format|
if current_customer.unfollow_playlist! @playlist.id
format.html{ redirect_back(fallback_location:root_path, success: 'You are now unfollowing this playlist!') }
format.js {}
else
format.html{ redirect_to :back, notice: 'Oh no! Something happened' }
format.js {}
end
end
end
private
def set_playlist
@playlist = Playlist.find(params[:id])
end
def redirect_if_plugin_not_activated
redirect_to site_root_path unless current_tenant.plugin_activated?('playlists')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment