Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Metrics/AbcSize:
# Configuration parameters: CountComments, Max, CountAsOne.
Metrics/ClassLength:
Exclude:
- 'app/controllers/admin/chapters_controller.rb'
- 'app/controllers/admin/workshops_controller.rb'
- 'app/controllers/application_controller.rb'
- 'app/controllers/events_controller.rb'
Expand Down
7 changes: 5 additions & 2 deletions app/controllers/admin/chapters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ def new

def create
@chapter = Chapter.new(chapter_params)
authorize(@chapter)
authorize @chapter

result = ChapterCreationService.call(chapter_params)
@chapter = result.chapter

if @chapter.save
if result.success
flash[:notice] = "Chapter #{@chapter.name} has been successfully created"
redirect_to [:admin, @chapter]
else
Expand Down
24 changes: 0 additions & 24 deletions app/controllers/admin/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
class Admin::GroupsController < Admin::ApplicationController
after_action :verify_authorized

def new
@group = Group.new
authorize @group
end

def create
@group = Group.new(group_params)
authorize @group

if @group.save
flash[:notice] = "Group #{@group.name} for chapter #{@group.chapter.name} has been successfully created"
redirect_to [:admin, @group]
else
flash[:notice] = @group.errors.full_messages
render 'new'
end
end

def show
@group = Group.find(params[:id])
authorize @group
Expand All @@ -27,10 +9,4 @@ def show
@total_count = @group.members.count
@pagy, @members = pagy(Group.members_by_recent_rsvp(@group), items: 20)
end

private

def group_params
params.expect(group: %i[name description chapter_id])
end
end
4 changes: 0 additions & 4 deletions app/policies/group_policy.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
class GroupPolicy < ApplicationPolicy
def create?
user.is_admin?
end

def show?
admin_or_chapter_organiser?
end
Expand Down
17 changes: 17 additions & 0 deletions app/services/chapter_creation_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ChapterCreationService
Result = Data.define(:chapter, :success, :errors)

def self.call(params)
chapter = Chapter.new(params)

ActiveRecord::Base.transaction do
chapter.save!
chapter.groups.create!(name: 'Students')
chapter.groups.create!(name: 'Coaches')
end

Result.new(chapter: chapter, success: true, errors: nil)
rescue ActiveRecord::RecordInvalid => e
Result.new(chapter: chapter, success: false, errors: e.message)
end
end
13 changes: 0 additions & 13 deletions app/views/admin/groups/new.html.haml

This file was deleted.

3 changes: 0 additions & 3 deletions app/views/layouts/_admin_menu.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
%li
= link_to new_admin_chapter_path, class: 'dropdown-item' do
New chapter
%li
= link_to new_admin_group_path, class: 'dropdown-item' do
New group
%li
= link_to admin_testimonials_path, class: 'dropdown-item' do
Testimonials
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
end

resources :meeting_invitations, only: %i[create update]
resources :groups, only: %i[index new create show]
resources :groups, only: %i[show]
resources :sponsors, except: [:destroy]
resources :feedback, only: [:index]
resources :contacts
Expand Down
23 changes: 23 additions & 0 deletions spec/controllers/admin/chapters_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@
login_as_admin(admin)
end

describe '#create' do
context 'when a chapter-scoped organiser creates a chapter' do
let(:organiser) { Fabricate(:chapter_organiser) }

before do
login(organiser)
end

it 'is denied before anything is persisted' do
expect do
post :create, params: { chapter: {
name: 'codebar Brighton',
email: 'brighton@codebar.io',
city: 'Brighton',
time_zone: 'London'
} }
end.not_to(change { [Chapter.count, Group.count] })

expect(response).to redirect_to(root_path)
end
end
end

describe '#status' do
it 'renders successfully with default 6 months' do
get :status
Expand Down
17 changes: 17 additions & 0 deletions spec/features/admin/chapters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@
click_on 'Create chapter'

expect(page).to have_text('Chapter codebar Brighton has been successfully created')

chapter = Chapter.find_by(name: 'codebar Brighton')
expect(chapter.groups.pluck(:name)).to match_array(%w[Students Coaches])
end

scenario 'an admin submitting an invalid form sees validation errors and no chapter is created' do
visit new_admin_chapter_path

fill_in 'Name', with: ''
fill_in 'Email', with: ''
fill_in 'City', with: 'Brighton'

click_on 'Create chapter'

expect(page).to have_text("Name can't be blank")
expect(page).to have_text("Email can't be blank")
expect(Chapter.count).to eq(0)
end
end

Expand Down
19 changes: 0 additions & 19 deletions spec/features/admin/groups_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
RSpec.feature 'admin groups', type: :feature do
describe '#creating a new group' do
let(:member) { Fabricate(:member) }

before do
Fabricate(:chapter, name: 'Brighton')
login_as_admin(member)
end

scenario 'an admin can create a new chapter' do
visit new_admin_group_path

select 'Students', from: 'group[name]'
select 'Brighton', from: 'group[chapter_id]'
click_on 'Create group'

expect(page).to have_text('Group Students for chapter Brighton has been successfully created')
end
end

describe '#show page' do
let(:member) { Fabricate(:member) }
let(:chapter) { Fabricate(:chapter, name: 'Brighton') }
Expand Down
18 changes: 0 additions & 18 deletions spec/policies/group_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@
let(:admin) { Fabricate(:member).tap { |m| m.add_role(:admin) } }
let(:regular_member) { Fabricate(:member) }

describe '#create?' do
context 'when user is admin' do
let(:user) { admin }

it 'permits access' do
expect(policy.create?).to be true
end
end

context 'when user is regular member' do
let(:user) { regular_member }

it 'denies access' do
expect(policy.create?).to be false
end
end
end

describe '#show?' do
context 'when user is admin' do
let(:user) { admin }
Expand Down
43 changes: 43 additions & 0 deletions spec/services/chapter_creation_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
RSpec.describe ChapterCreationService do
let(:valid_params) do
{
name: 'codebar Brighton',
email: 'brighton@codebar.io',
city: 'Brighton',
time_zone: 'London'
}
end

describe '.call' do
it 'creates chapter with Students and Coaches groups' do
result = described_class.call(valid_params)

expect(result.success).to be true
expect(result.chapter.persisted?).to be true
expect(result.chapter.groups.pluck(:name)).to match_array(%w[Students Coaches])
end

it 'fails when chapter params are invalid' do
invalid_params = valid_params.merge(name: '')
result = described_class.call(invalid_params)

expect(result.success).to be false
expect(result.errors).to be_present
expect(Chapter.where(name: '')).not_to exist
end

it 'rolls back chapter if groups fail' do
# 'bogus' is not in Group::NAMES, so the group fails its real
# inclusion validation and save! raises RecordInvalid inside the
# service's transaction.
invalid_group = Group.new(name: 'bogus')
allow(Group).to receive(:new).and_return(invalid_group)

result = described_class.call(valid_params)

expect(result.success).to be false
expect(result.errors).to be_present
expect(Chapter.where(name: valid_params[:name])).not_to exist
end
end
end