Skip to content

Commit

Permalink
Admins can create / update Chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
tjgrathwell committed Dec 11, 2015
1 parent f275207 commit de10660
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 4 deletions.
44 changes: 42 additions & 2 deletions app/controllers/chapters_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ChaptersController < ApplicationController
before_action :authenticate_user!, :except => [:show, :index]
before_action :assign_chapter, :only => [:show]
before_action :authenticate_user!, except: [:show, :index]
before_action :assign_chapter, except: [:index, :new, :create]
before_action :validate_admin!, except: [:show, :index]

def index
@chapters = Chapter.all
Expand All @@ -12,8 +13,47 @@ def show
).sort_by(&:ends_at)
end

def new
@chapter = Chapter.new
end

def edit
end

def create
@chapter = Chapter.new(chapter_params)

if @chapter.save
redirect_to @chapter, notice: 'Chapter was successfully created.'
else
render :new
end
end

def update
if @chapter.update_attributes(chapter_params)
redirect_to @chapter, notice: 'Chapter was successfully updated.'
else
render :edit
end
end

def destroy
unless @chapter.destroyable?
return redirect_to root_url, alert: "Can't delete a chapter that's still assigned to an event or external event."
end

@chapter.destroy

redirect_to chapters_url
end

private

def chapter_params
params.require(:chapter).permit(Chapter::PERMITTED_ATTRIBUTES)
end

def assign_chapter
@chapter = Chapter.find(params[:id])
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/chapter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
class Chapter < ActiveRecord::Base
PERMITTED_ATTRIBUTES = [:name, :organization_id]

belongs_to :organization
has_many :events
has_many :external_events

validates_presence_of :organization

def destroyable?
(events_count + external_events_count) == 0
end
end
18 changes: 18 additions & 0 deletions app/views/chapters/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="row">
<div class="col-md-6">
<%= simple_form_for(@chapter) do |f| %>
<%= render 'shared/model_error_messages', model: @chapter %>

<div class="form-group">
<%= f.label :organization_id %>
<%= f.collection_select(:organization_id, Organization.order(:name), :id, :name, {prompt: true}, {class: 'form-control'}) %>
</div>

<%= f.input :name %>

<div class="actions">
<%= f.submit class: 'btn btn-submit', data: {disable_with: 'Please wait...'} %>
</div>
<% end %>
</div>
</div>
9 changes: 9 additions & 0 deletions app/views/chapters/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%= content_for(:header_text, "Editing chapter") %>
<%= render 'form' %>
<%= render 'shared/actions', links: [
['Show', @chapter],
['Back', chapters_path]
] %>

12 changes: 11 additions & 1 deletion app/views/chapters/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<tr>
<th data-default-sort="asc">Name</th>
<th>Events</th>
<% if current_user.try(:admin?) %>
<th></th>
<% end %>
</tr>
</thead>

Expand All @@ -13,9 +16,16 @@
<tr>
<td><%= link_to chapter.name, chapter %></td>
<td data-label="Events:"><%= chapter.events_count + chapter.external_events_count %></td>
<% if current_user.try(:admin?) %>
<td>
<% if current_user.try(:admin?) && chapter.destroyable? %>
<%= link_to 'Destroy', chapter, data: {confirm: 'Are you sure?'}, method: :delete, class: 'btn btn-danger' %>
<% end %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>

<%= render 'shared/actions', links: crud_object_nav_links(:chapter) %>
<%= render 'shared/actions', links: crud_object_nav_links(:chapter, current_user.try(:admin?) ? ['New Chapter', new_chapter_path] : nil) %>
7 changes: 7 additions & 0 deletions app/views/chapters/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= content_for(:header_text, 'New chapter') %>
<%= render 'form' %>
<%= render 'shared/actions', links: [
['Back', chapters_path]
] %>
1 change: 1 addition & 0 deletions app/views/chapters/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<%= render 'shared/events_table', events: @chapter_events %>
<%= render 'shared/actions', links: [
current_user.try(:admin?) ? ['Edit', edit_chapter_path(@chapter)] : nil,
['Back', chapters_path]
] %>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
resources :meetup_users, only: [:show]

resources :locations
resources :chapters, only: [:index, :show]
resources :chapters
resources :regions do
resources :region_leaderships, only: [:index, :create, :destroy]
end
Expand Down
71 changes: 71 additions & 0 deletions spec/controllers/chapters_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'rails_helper'

describe ChaptersController do
let(:organization) { create(:organization, name: 'SpaceBridge') }
let(:user) { create(:user, admin: true) }

before do
sign_in user
end

describe '#index' do
let!(:chapter) { create(:chapter) }

it 'shows all the chapters' do
get :index
expect(assigns(:chapters)).to match_array([chapter])
end
end

describe '#new' do
it 'shows an empty chapter' do
get :new
expect(response).to be_success
end
end

describe '#create' do
it 'creates a new chapter' do
expect {
post :create, chapter: {name: "Fabulous Chapter", organization_id: organization.id}
}.to change(Chapter, :count).by(1)
end
end

describe '#edit' do
let!(:chapter) { create(:chapter) }

it "shows a chapter edit form" do
get :edit, id: chapter.id
expect(response).to be_success
end
end

describe '#update' do
let!(:chapter) { create(:chapter) }

it "changes chapter details" do
expect {
put :update, id: chapter.id, chapter: {name: 'Sandwich Chapter'}
}.to change { chapter.reload.name }
expect(response).to redirect_to(chapter_path(chapter))
end
end

describe "#destroy" do
let!(:chapter) { create(:chapter) }

it "can delete a chapter that belongs to no events" do
expect {
delete :destroy, {id: chapter.id}
}.to change(Chapter, :count).by(-1)
end

it "cannot delete a chapter that belongs to a event" do
create(:event, chapter: chapter)
expect {
delete :destroy, {id: chapter.id}
}.not_to change(Chapter, :count)
end
end
end

0 comments on commit de10660

Please sign in to comment.