diff --git a/app/controllers/admin/booths_controller.rb b/app/controllers/admin/booths_controller.rb index 4082e69f..045d24a7 100644 --- a/app/controllers/admin/booths_controller.rb +++ b/app/controllers/admin/booths_controller.rb @@ -4,6 +4,7 @@ module Admin class BoothsController < Admin::BaseController load_and_authorize_resource :conference, find_by: :short_title load_and_authorize_resource through: :conference + include Invitation def index @file_name = "booths_for_#{@conference.short_title}" @@ -38,6 +39,7 @@ module Admin @booth.submitter = current_user if @booth.save + booth_responsible_invite redirect_to admin_conference_booths_path, notice: 'Booth successfully created.' else @@ -54,7 +56,7 @@ module Admin @url = admin_conference_booth_path(@conference.short_title, @booth.id) @booth.update_attributes(booth_params) - + booth_responsible_invite if @booth.save redirect_to admin_conference_booths_path, notice: "Successfully updated booth for #{@booth.title}." diff --git a/app/controllers/booths_controller.rb b/app/controllers/booths_controller.rb index cb14c9bd..4600ab61 100644 --- a/app/controllers/booths_controller.rb +++ b/app/controllers/booths_controller.rb @@ -5,6 +5,7 @@ class BoothsController < ApplicationController load_resource :conference, find_by: :short_title load_and_authorize_resource through: :conference skip_authorize_resource only: [:withdraw, :confirm, :restart] + include Invitation def index @booths = current_user.booths.where(conference_id: @conference.id).uniq @@ -22,6 +23,7 @@ class BoothsController < ApplicationController @booth.submitter = current_user if @booth.save + booth_responsible_invite redirect_to conference_booths_path, notice: 'Booth successfully created.' else @@ -38,6 +40,7 @@ class BoothsController < ApplicationController @url = conference_booth_path(@conference.short_title, @booth.id) @booth.update_attributes(booth_params) + booth_responsible_invite if @booth.save redirect_to conference_booths_path, notice: 'Booth successfully updated!' diff --git a/app/controllers/concerns/invitation.rb b/app/controllers/concerns/invitation.rb new file mode 100644 index 00000000..2d1d84c1 --- /dev/null +++ b/app/controllers/concerns/invitation.rb @@ -0,0 +1,21 @@ +module Invitation + extend ActiveSupport::Concern + + def booth_responsible_invite + if booth_params[:invite_responsible] + emails_array = booth_params[:invite_responsible].split(',') + + emails_array.each do |email| + new_user = User.find_by(email: email) + if new_user.nil? + User.invite!({ email: email }, current_user) + new_user = User.find_by(email: email) + end + if new_user && @booth.responsible_ids.exclude?(new_user.id) + BoothRequest.create(booth_id: @booth.id, user_id: new_user.id, + role: 'responsible') + end + end + end + end +end diff --git a/spec/controllers/admin/booths_controller_spec.rb b/spec/controllers/admin/booths_controller_spec.rb index 11b578a4..853fed4c 100644 --- a/spec/controllers/admin/booths_controller_spec.rb +++ b/spec/controllers/admin/booths_controller_spec.rb @@ -77,6 +77,18 @@ describe Admin::BoothsController do it 'shows success message' do expect(flash[:notice]).to match('Booth successfully created.') end + + it 'creates a new user on inviting booth responsible' do + expect(User.where(email: 'user@example.com')).to exist + end + + it 'does not create a user with invalid email on inviting booth responsible' do + expect(User.where(email: 'example')).not_to exist + end + + it 'invited user should be a part of booth responsibles' do + expect(Booth.last.responsibles.ids).to include(User.find_by(email: 'user@example.com').id) + end end context 'create action fails' do @@ -113,7 +125,7 @@ describe Admin::BoothsController do describe 'PATCH #update' do context 'updates suchessfully' do - before { patch :update, params: { id: booth.id, booth: attributes_for(:booth, title: 'different'), conference_id: conference.short_title } } + before { patch :update, params: { id: booth.id, booth: attributes_for(:booth, title: 'different', invited_users: 'user@example.com, example'), conference_id: conference.short_title } } it 'redirects to admin booth index path' do expect(response).to redirect_to admin_conference_booths_path end @@ -126,6 +138,19 @@ describe Admin::BoothsController do booth.reload expect(booth.title).to eq('different') end + + it 'creates a new user on inviting booth responsible' do + expect(User.where(email: 'user@example.com')).to exist + end + + it 'does not create a user with invalid email on inviting booth responsible' do + expect(User.where(email: 'example')).not_to exist + end + + it 'invited user should be a part of booth responsibles' do + booth.reload + expect(booth.responsibles.ids).to include(User.find_by(email: 'user@example.com').id) + end end end end diff --git a/spec/controllers/booths_controller_spec.rb b/spec/controllers/booths_controller_spec.rb index 8444be9c..f49d2fd9 100644 --- a/spec/controllers/booths_controller_spec.rb +++ b/spec/controllers/booths_controller_spec.rb @@ -57,6 +57,18 @@ describe BoothsController do it 'shows success message' do expect(flash[:notice]).to match('Booth successfully created.') end + + it 'creates a new user on inviting booth responsible' do + expect(User.where(email: 'user@example.com')).to exist + end + + it 'does not create a user with invalid email on inviting booth responsible' do + expect(User.where(email: 'example')).not_to exist + end + + it 'invited user should be a part of booth responsibles' do + expect(Booth.last.responsibles.ids).to include(User.find_by(email: 'user@example.com').id) + end end context 'create action fails' do @@ -93,7 +105,7 @@ describe BoothsController do describe 'PATCH #update' do context 'updates suchessfully' do - before { patch :update, params: { id: booth.id, booth: attributes_for(:booth, title: 'different'), conference_id: conference.short_title } } + before { patch :update, params: { id: booth.id, booth: attributes_for(:booth, title: 'different', invited_users: 'user@example.com, example'), conference_id: conference.short_title } } it 'redirects to booth index path' do expect(response).to redirect_to conference_booths_path @@ -107,6 +119,19 @@ describe BoothsController do booth.reload expect(booth.title).to eq('different') end + + it 'creates a new user on inviting booth responsible' do + expect(User.where(email: 'user@example.com')).to exist + end + + it 'does not create a user with invalid email on inviting booth responsible' do + expect(User.where(email: 'example')).not_to exist + end + + it 'invited user should be a part of booth responsibles' do + booth.reload + expect(booth.responsibles.ids).to include(User.find_by(email: 'user@example.com').id) + end end end end diff --git a/spec/factories/booths.rb b/spec/factories/booths.rb index fa7e9824..4de471c5 100644 --- a/spec/factories/booths.rb +++ b/spec/factories/booths.rb @@ -12,5 +12,6 @@ FactoryBot.define do submitter { create(:user) } responsible_ids { [create(:user).id] } + invite_responsible { 'user@example.com, example' } end end