diff --git a/app/controllers/admin/workshops_controller.rb b/app/controllers/admin/workshops_controller.rb index 38306b7c9..07116dfbc 100644 --- a/app/controllers/admin/workshops_controller.rb +++ b/app/controllers/admin/workshops_controller.rb @@ -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 @@ -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') @@ -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 diff --git a/app/views/admin/chapters/show.html.haml b/app/views/admin/chapters/show.html.haml index 4af651a15..99d594134 100644 --- a/app/views/admin/chapters/show.html.haml +++ b/app/views/admin/chapters/show.html.haml @@ -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| diff --git a/app/views/admin/workshops/_form.html.haml b/app/views/admin/workshops/_form.html.haml index c2efc2437..02b1c994f 100644 --- a/app/views/admin/workshops/_form.html.haml +++ b/app/views/admin/workshops/_form.html.haml @@ -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 diff --git a/app/views/admin/workshops/_shared_form.html.haml b/app/views/admin/workshops/_shared_form.html.haml index 6681b082c..042c431b3 100644 --- a/app/views/admin/workshops/_shared_form.html.haml +++ b/app/views/admin/workshops/_shared_form.html.haml @@ -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 diff --git a/app/views/admin/workshops/new.html.haml b/app/views/admin/workshops/new.html.haml index 1d275527c..317bbcb0c 100644 --- a/app/views/admin/workshops/new.html.haml +++ b/app/views/admin/workshops/new.html.haml @@ -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' diff --git a/spec/features/admin/workshops_spec.rb b/spec/features/admin/workshops_spec.rb index df7eac7a2..4e2ef4a95 100644 --- a/spec/features/admin/workshops_spec.rb +++ b/spec/features/admin/workshops_spec.rb @@ -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