move subscribe to its own controller, add authorization, test subscribe/unsubscribe
This commit is contained in:
parent
97bf86454e
commit
3be200abcc
7 changed files with 92 additions and 43 deletions
|
|
@ -5,42 +5,6 @@ class ConferenceController < ApplicationController
|
|||
@keynote_speakers = @conference.keynote_speakers
|
||||
end
|
||||
|
||||
def subscribe
|
||||
conference = Conference.find_by_short_title(params[:id])
|
||||
if current_user.subscriptions.where(conference: conference).blank?
|
||||
subscription = Subscription.new(user_id: current_user.id, conference_id: conference.id)
|
||||
begin
|
||||
subscription.save!
|
||||
flash[:success] = 'You have been subscribed to receive Email Notifications from this Conference.'
|
||||
redirect_to root_path
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
flash[:error] = subscription.errors.full_messages.to_sentence
|
||||
redirect_to root_path
|
||||
end
|
||||
else
|
||||
flash[:notice] = 'Already Subscribed'
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
||||
|
||||
def unsubscribe
|
||||
conference = Conference.find_by_short_title(params[:id])
|
||||
subscription = current_user.subscriptions.where(conference_id: conference.id).first
|
||||
if subscription.blank?
|
||||
flash[:notice] = 'Already Unsubscribed'
|
||||
redirect_to root_path
|
||||
else
|
||||
begin
|
||||
subscription.destroy!
|
||||
flash[:notice] = "You have been unsubscribed and now you won't be receiving any Email Notifications."
|
||||
redirect_to root_path
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
flash[:error] = subscription.errors.full_messages.to_sentence
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def gallery_photos
|
||||
@photos = @conference.photos
|
||||
render 'photos', formats: [:js]
|
||||
|
|
|
|||
27
app/controllers/subscriptions_controller.rb
Normal file
27
app/controllers/subscriptions_controller.rb
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
class SubscriptionsController < ApplicationController
|
||||
before_filter :verify_user
|
||||
load_and_authorize_resource :conference, find_by: :short_title
|
||||
load_and_authorize_resource only: [:create, :destroy], through: :conference
|
||||
|
||||
def create
|
||||
@subscription = current_user.subscriptions.build(conference_id: @conference.id)
|
||||
if @subscription.save!
|
||||
flash[:notice] = "You have been subscribed to receive email notifications for #{@conference.short_title}."
|
||||
redirect_to root_path
|
||||
else
|
||||
flash[:error] = subscription.errors.full_messages.to_sentence
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@subscription = current_user.subscriptions.find_by(conference_id: @conference.id)
|
||||
if @subscription.destroy
|
||||
flash[:notice] = "You have been unsubscribed and now you will not be receiving email notifications for #{@conference.short_title}."
|
||||
redirect_to root_path
|
||||
else
|
||||
flash[:error] = @subscription.errors.full_messages.to_sentence
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue