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
|
@keynote_speakers = @conference.keynote_speakers
|
||||||
end
|
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
|
def gallery_photos
|
||||||
@photos = @conference.photos
|
@photos = @conference.photos
|
||||||
render 'photos', formats: [:js]
|
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
|
||||||
|
|
@ -129,6 +129,9 @@ class Ability
|
||||||
def signed_in(user)
|
def signed_in(user)
|
||||||
guest # Inherits abilities of guest
|
guest # Inherits abilities of guest
|
||||||
|
|
||||||
|
# Can subscribe, unsubscribe to a conference
|
||||||
|
can [:create, :destroy], Subscription, user_id: user.id
|
||||||
|
|
||||||
# Conference Registration
|
# Conference Registration
|
||||||
can :manage, Registration, user_id: user.id
|
can :manage, Registration, user_id: user.id
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,10 @@ class User < ActiveRecord::Base
|
||||||
},
|
},
|
||||||
presence: true
|
presence: true
|
||||||
|
|
||||||
|
def subscribed? conference
|
||||||
|
self.subscriptions.find_by(conference_id: conference.id).present?
|
||||||
|
end
|
||||||
|
|
||||||
# Returns the ticket purchased ticket
|
# Returns the ticket purchased ticket
|
||||||
# ====Returns
|
# ====Returns
|
||||||
# * +TicketUser::ActiveRecord_Relation+ -> user
|
# * +TicketUser::ActiveRecord_Relation+ -> user
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,10 @@
|
||||||
= simple_format(conference.venue.description, class: 'lead')
|
= simple_format(conference.venue.description, class: 'lead')
|
||||||
.col-md-2
|
.col-md-2
|
||||||
.btn-group-vertical
|
.btn-group-vertical
|
||||||
- unless current_user.blank?
|
- if current_user.nil? || !current_user.subscribed?(conference)
|
||||||
- if Conference.joins(:subscriptions).merge(current_user.subscriptions).include? conference
|
= link_to 'Subscribe', conference_subscriptions_path(conference.short_title), method: :post, class: 'btn btn-info btn-group-vertical'
|
||||||
= link_to "Unsubscribe", subscription_conference_path(conference.short_title), method: "DELETE", class: "btn btn-danger btn-group-vertical"
|
- else
|
||||||
-else
|
= link_to 'Unsubscribe', conference_subscriptions_path(conference.short_title), method: :delete, class: 'btn btn-danger btn-group-vertical'
|
||||||
= link_to "Subscribe", subscription_conference_path(conference.short_title), method: "PATCH", class: "btn btn-info btn-group-vertical"
|
|
||||||
|
|
||||||
- if !@conference || @conference != conference
|
- if !@conference || @conference != conference
|
||||||
- if conference.splashpage && conference.splashpage.public
|
- if conference.splashpage && conference.splashpage.public
|
||||||
|
|
|
||||||
|
|
@ -109,6 +109,7 @@ Osem::Application.routes.draw do
|
||||||
resource :conference_registrations, path: 'register'
|
resource :conference_registrations, path: 'register'
|
||||||
resources :tickets, only: [:index]
|
resources :tickets, only: [:index]
|
||||||
resources :ticket_purchases, only: [:create, :destroy]
|
resources :ticket_purchases, only: [:create, :destroy]
|
||||||
|
resource :subscriptions, only: [:create, :destroy]
|
||||||
|
|
||||||
resource :schedule, only: [] do
|
resource :schedule, only: [] do
|
||||||
get '/' => 'schedule#index'
|
get '/' => 'schedule#index'
|
||||||
|
|
@ -116,8 +117,6 @@ Osem::Application.routes.draw do
|
||||||
|
|
||||||
member do
|
member do
|
||||||
get 'gallery_photos'
|
get 'gallery_photos'
|
||||||
patch 'subscription' => 'conference#subscribe'
|
|
||||||
delete 'subscription' => 'conference#unsubscribe'
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
53
spec/controllers/subscriptions_controller_spec.rb
Normal file
53
spec/controllers/subscriptions_controller_spec.rb
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe SubscriptionsController do
|
||||||
|
let(:conference) { create(:conference) }
|
||||||
|
let(:user) { create(:user) }
|
||||||
|
|
||||||
|
describe 'POST #create' do
|
||||||
|
context 'when user is a guest' do
|
||||||
|
it 'redirects to sign in page' do
|
||||||
|
post :create, conference_id: conference.short_title
|
||||||
|
expect(response).to redirect_to new_user_session_path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when user is signed in' do
|
||||||
|
before(:each) do
|
||||||
|
sign_in(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'redirects to home page' do
|
||||||
|
post :create, conference_id: conference.short_title
|
||||||
|
expect(response).to redirect_to root_path
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'shows success message in flash notice' do
|
||||||
|
post :create, conference_id: conference.short_title
|
||||||
|
expect(flash[:notice]).to match("You have been subscribed to receive email notifications for #{conference.short_title}")
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'subscribes user to conference' do
|
||||||
|
post :create, conference_id: conference.short_title
|
||||||
|
expect(user.subscriptions.pluck(:conference_id)).to include(conference.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'DELETE #destroy' do
|
||||||
|
before(:each) do
|
||||||
|
sign_in(user)
|
||||||
|
post :create, conference_id: conference.short_title
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'redirects to home page' do
|
||||||
|
delete :destroy, conference_id: conference.short_title
|
||||||
|
expect(response).to redirect_to root_path
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'shows success message in flash notice' do
|
||||||
|
delete :destroy, conference_id: conference.short_title
|
||||||
|
expect(flash[:notice]).to match("You have been unsubscribed and now you will not be receiving email notifications for #{conference.short_title}.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue