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
9 changes: 6 additions & 3 deletions app/queries/sponsors_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ def by_chapter
return if chapter.blank?

chapter_id = chapter.to_s.match?(/\A\d+\z/) ? chapter : lookup_chapter_id
return unless chapter_id

@sponsors = sponsors.joins(:workshops).where('workshops.chapter_id' => chapter_id).group('sponsors.id')
if chapter_id
@sponsors = sponsors.joins(:workshops).where('workshops.chapter_id' => chapter_id).group('sponsors.id')
else
errors.add(:chapter, :not_found, message: 'no chapter with that name')
@sponsors = Sponsor.none
end
end

def lookup_chapter_id
Expand Down
36 changes: 26 additions & 10 deletions app/views/admin/sponsors/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
- title 'Sponsors'
%style= ".form-control::placeholder { opacity: 0.7; }"

.container.py-4.py-lg-5
.row.mb-4
.col-12
%nav{'aria-label': 'breadcrumb'}
%ol.breadcrumb.ms-0
%li.breadcrumb-item.active= t('admin.shared.sponsors')
%li.breadcrumb-item= link_to 'Admin', admin_root_path
%li.breadcrumb-item.active Sponsors

.col-12
%h1.mb-3 Sponsors

.col-12
.row.row-cols-md-auto.align-items-center
= form_with model: @sponsors_search, url: admin_sponsors_path, method: :get, class: 'row row-cols-1 row-cols-md-auto align-items-center' do |f|
= render(ChapterPickerComponent.new(name: 'sponsors_search[chapter]', chapters: @chapters, selected: @sponsors_search.chapter, placeholder: 'Filter by chapter'))
= f.text_field :name, placeholder: 'Filter by sponsor name', class: 'form-control w-auto my-2 my-md-0'
= f.button 'Filter', class: 'btn btn-primary'
= link_to 'Reset form', admin_sponsors_path
= form_with model: @sponsors_search, url: admin_sponsors_path, method: :get do |f|
%fieldset.border.rounded.bg-light.p-3
%legend Filter sponsors
.row.row-cols-1.row-cols-md-auto.align-items-start.gap-1
- chapter_error = @sponsors_search.errors[:chapter].any?
.col
= f.label :chapter, 'Chapter', class: 'form-label'
= render(ChapterPickerComponent.new(name: 'sponsors_search[chapter]', chapters: @chapters, selected: @sponsors_search.chapter, placeholder: 'ex: London'))
- if chapter_error
.invalid-feedback.d-block= @sponsors_search.errors[:chapter].join(', ')
.col
= f.label :name, 'Sponsor name', class: 'form-label'
= f.text_field :name, placeholder: 'ex: Google', class: 'form-control w-auto'
.col
.form-label.invisible  
= f.button 'Filter', class: 'btn btn-primary'
= link_to 'Reset form', admin_sponsors_path

= render partial: 'shared/pagination', locals: { pagy: @pagy, model: 'sponsor' }

Expand All @@ -25,7 +43,7 @@
%th
Level
%th
Chapter(s)
Chapters
%th
Sponsorships
%tbody
Expand All @@ -40,5 +58,3 @@
= link_to chapter.name, admin_chapter_path(chapter)
%td
= sponsor.sponsorships_count

= render partial: 'shared/pagination', locals: { pagy: @pagy, model: 'sponsor' }
2 changes: 1 addition & 1 deletion app/views/shared/_pagination.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.row.align-items-center.justify-content-between
.col-auto
%p.mb-3
!= pagy.info_tag(item_name: model)
!= pagy.info_tag(item_name: model.pluralize(pagy.count))
.col-auto
!= pagy.series_nav(:bootstrap) if pagy.pages > 1
2 changes: 1 addition & 1 deletion spec/features/admin/sponsor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
click_on 'Filter'

expect(page).to have_css('tbody tr', count: 0)
expect(page).to have_text('No sponsor found')
expect(page).to have_text('No sponsors found')
end

scenario 'can clear filtering form' do
Expand Down
10 changes: 10 additions & 0 deletions spec/queries/sponsors_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@
expect(results).to contain_exactly(matching)
end

it 'returns an empty relation when chapter does not exist' do
Fabricate(:sponsor)
search = described_class.new(name: nil, chapter: 'Nonexistent')

results = search.call

expect(results).to be_empty
expect(search.errors[:chapter]).to include('no chapter with that name')
end

it 'returns an empty relation when nothing matches' do
Fabricate(:sponsor, name: 'Apple Inc')

Expand Down