Skip to content
Open
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
17 changes: 15 additions & 2 deletions app/controllers/admin/workshops_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ def index
end

def new
@workshop = Workshop.new
chapter_id = params[:chapter_id]
@workshop = if chapter_id.present? && Chapter.exists?(chapter_id)
Workshop.new(chapter_id: chapter_id)
else
Workshop.new
end
authorize @workshop
end

Expand All @@ -24,7 +29,7 @@ def create
authorize(@workshop)

if workshop_type_valid? && @workshop.save
grant_organiser_access(@workshop.chapter.organisers.pluck(:id))
assign_organisers_or_default
assign_host(host_id)

redirect_to admin_workshop_path(@workshop), notice: I18n.t('admin.messages.workshop.created')
Expand Down Expand Up @@ -215,6 +220,14 @@ def assign_organisers(organiser_ids)
revoke_organiser_access(organiser_ids)
end

def assign_organisers_or_default
if organiser_ids.present?
assign_organisers(organiser_ids)
else
grant_organiser_access(@workshop.chapter.organisers.pluck(:id))
end
end

def host_id
params.expect(workshop: [:host])[:host]
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/chapters/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
.mb-4.mt-md-4.mt-lg-0
.d-md-flex.justify-content-between.align-items-center
%h3 Upcoming Workshops
= link_to 'New workshop', new_admin_workshop_path, class: 'btn btn-primary btn-sm'
= link_to 'New workshop', new_admin_workshop_path(chapter_id: @chapter.id), class: 'btn btn-primary btn-sm'
- if @workshops.any?
%ul.list-unstyled.ms-0.mb-0
- @workshops.each do |workshop|
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/workshops/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
= render partial: 'shared_form', locals: { f: f }
.col-12.col-md-6
= render partial: 'virtual_workshop_fields', locals: { f: f }
- if @workshop.chapter
.col-12.col-md-6
= f.input :organisers, collection: @workshop.chapter.organisers, value_method: :id, label_method: :full_name, selected: @workshop.chapter.organisers.pluck(:id), input_html: { multiple: true }
.col-12
= f.input :invitable, hint_html: { class: 'd-block ms-1' }
.row
Expand Down
7 changes: 5 additions & 2 deletions app/views/admin/workshops/_shared_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.row
.col-12
= f.association :chapter, as: :select, collection: Chapter.available_to_user(current_user)
- if @workshop.chapter
= f.hidden_field :chapter_id
- else
.col-12
= f.association :chapter, as: :select, collection: Chapter.available_to_user(current_user)
.col-12
= f.input :local_date, label: 'Date', as: :string, required: true, input_html: { data: { value: @workshop.date_and_time.try(:strftime, '%d/%m/%Y') } }
.col-12.col-md-6
Expand Down
4 changes: 3 additions & 1 deletion app/views/admin/workshops/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
- content_for :title, @workshop.chapter ? "New Workshop for #{@workshop.chapter.name}" : 'New Workshop'

.container.py-4.py-lg-5
.row.mb-4
.col
%h1 New Workshop
%h1= @workshop.chapter ? "New Workshop for #{@workshop.chapter.name}" : 'New Workshop'

= render partial: 'form'
53 changes: 53 additions & 0 deletions spec/features/admin/workshops_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,59 @@
expect(page).to have_text 'Invite'
end
end

context 'when creating a workshop from a chapter page' do
around do |example|
travel_to Time.zone.local(2020, 12, 0o1, 0, 0, 0)
example.run
travel_back
end

scenario 'pre-selects the chapter and allows organisers to be assigned' do
kept_organiser = Fabricate(:member)
removed_organiser = Fabricate(:member)
kept_organiser.add_role(:organiser, chapter)
removed_organiser.add_role(:organiser, chapter)

visit admin_chapter_path(chapter)

within('.col-12.col-lg-8') do
click_on 'New workshop'
end

expect(page).to have_css('h1', text: "New Workshop for #{chapter.name}")
expect(page).to have_title("New Workshop for #{chapter.name}")
expect(page).to have_no_select('workshop_chapter_id')
expect(page).to have_select('workshop_organisers', with_options: [kept_organiser.full_name, removed_organiser.full_name])

unselect removed_organiser.full_name, from: 'workshop_organisers'

fill_in 'Date', with: Date.current
fill_in 'Begins at', with: '11:30'
fill_in 'Ends at', with: '12:45'

within '#host' do
select sponsor.name
end

click_on 'Save'

expect(page).to have_text('Workshop successfully created')
workshop = Workshop.last
expect(workshop.chapter).to eq(chapter)
expect(workshop.organisers.map(&:id)).to include(kept_organiser.id)
expect(workshop.organisers.map(&:id)).not_to include(removed_organiser.id)
end

scenario 'falls back to the standard form when chapter_id is invalid' do
visit new_admin_workshop_path(chapter_id: 0)

expect(page).to have_css('h1', text: 'New Workshop')
expect(page).to have_title('New Workshop')
expect(page).to have_select('workshop_chapter_id')
expect(page).to have_no_select('workshop_organisers')
end
end
end

context 'with dietary restrictions' do
Expand Down