Move administrateur creation in Administrate

This commit is contained in:
Mathieu Magnin 2018-01-17 10:28:59 +01:00
parent 0396db9e5f
commit 67f89d5f86
7 changed files with 72 additions and 52 deletions

View file

@ -13,27 +13,9 @@ class AdministrationsController < ApplicationController
array: true
end
def create
administrateur = current_administration.invite_admin(create_administrateur_params[:email])
if administrateur.errors.empty?
flash.notice = "Administrateur créé"
else
flash.alert = administrateur.errors.full_messages
end
redirect_to administrations_path
end
def update
Administrateur.find_inactive_by_id(params[:id]).invite!
redirect_to administrations_path
end
private
def create_administrateur_params
params.require(:administrateur).permit(:email)
end
end

View file

@ -1,4 +1,21 @@
module Manager
class AdministrateursController < Manager::ApplicationController
def create
administrateur = current_administration.invite_admin(create_administrateur_params[:email])
if administrateur.errors.empty?
flash.notice = "Administrateur créé"
else
flash.alert = administrateur.errors.full_messages
end
redirect_to manager_administrateurs_path
end
private
def create_administrateur_params
params.require(:administrateur).permit(:email)
end
end
end

View file

@ -39,7 +39,9 @@ class AdministrateurDashboard < Administrate::BaseDashboard
# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = [].freeze
FORM_ATTRIBUTES = [
:email
].freeze
# Overwrite this method to customize how procedures are displayed
# across all pages of the admin dashboard.

View file

@ -0,0 +1,16 @@
- content_for(:title) do
= display_resource_name(page.resource_name)
- content_for(:search) do
- if show_search_bar
= render "search", search_term: search_term
%header.header
%h1.header__heading#page-title
= content_for(:title)
.header__actions
= link_to 'nouveau', new_manager_administrateur_path, class: 'button'
= render "collection", collection_presenter: page, resources: resources
= paginate resources

View file

@ -4,7 +4,7 @@ Rails.application.routes.draw do
post 'whitelist', on: :member
end
resources :administrateurs, only: [:index, :show]
resources :administrateurs, only: [:index, :show, :new, :create]
root to: "procedures#index"
end

View file

@ -18,35 +18,4 @@ describe AdministrationsController, type: :controller do
it { expect(subject.status).to eq 200 }
end
end
describe 'POST #create' do
let(:email) { 'plop@plop.com' }
let(:password) { 'password' }
before do
sign_in administration
end
subject { post :create, administrateur: {email: email } }
context 'when email and password are correct' do
it 'add new administrateur in database' do
expect { subject }.to change(Administrateur, :count).by(1)
end
it 'alert new mail are send' do
expect(AdministrationMailer).to receive(:new_admin_email).and_return(AdministrationMailer)
expect(AdministrationMailer).to receive(:deliver_now!)
expect(AdministrationMailer).to receive(:invite_admin).and_return(AdministrationMailer)
expect(AdministrationMailer).to receive(:deliver_now!)
subject
end
end
context 'when email or password are missing' do
let(:email) { '' }
it { expect { subject }.to change(Administrateur, :count).by(0) }
end
end
end

View file

@ -0,0 +1,34 @@
describe Manager::AdministrateursController, type: :controller do
let(:administration){ create(:administration) }
describe 'POST #create' do
let(:email) { 'plop@plop.com' }
let(:password) { 'password' }
before do
sign_in administration
end
subject { post :create, administrateur: { email: email } }
context 'when email and password are correct' do
it 'add new administrateur in database' do
expect { subject }.to change(Administrateur, :count).by(1)
end
it 'alert new mail are send' do
expect(AdministrationMailer).to receive(:new_admin_email).and_return(AdministrationMailer)
expect(AdministrationMailer).to receive(:deliver_now!)
expect(AdministrationMailer).to receive(:invite_admin).and_return(AdministrationMailer)
expect(AdministrationMailer).to receive(:deliver_now!)
subject
end
end
context 'when email or password are missing' do
let(:email) { '' }
it { expect { subject }.to change(Administrateur, :count).by(0) }
end
end
end