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
2 changes: 2 additions & 0 deletions app/models/sponsor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Sponsor < ApplicationRecord

validates :level, inclusion: { in: Sponsor.levels.keys }
validates :name, :address, :avatar, :website, :level, presence: true
validates :number_of_coaches, presence: true, numericality: { greater_than_or_equal_to: 0, only_integer: true }
validates :seats, presence: true, numericality: { greater_than_or_equal_to: 0, only_integer: true }
validate :website_is_url, if: :website?

default_scope -> { order('updated_at desc') }
Expand Down
6 changes: 3 additions & 3 deletions app/views/admin/sponsors/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= simple_form_for [:admin, @sponsor], :html => {:multipart => true, novalidate: true } do |f|
= simple_form_for [:admin, @sponsor], html: { multipart: true, novalidate: false } do |f|
.row
.col-12.col-md-6
= f.input :name
Expand Down Expand Up @@ -27,9 +27,9 @@
%p Only required for hosts.
.row
.col-6
= f.input :seats
= f.input :seats, input_html: { required: true, min: 0 }
.col-6
= f.input :number_of_coaches
= f.input :number_of_coaches, input_html: { required: true, min: 0 }
= f.input :accessibility_info, input_html: { rows: 3 },
hint: raw(t('admin.shared.markdown_hint', link: link_to(t('admin.shared.markdown'), 'https://commonmark.org/help/')))
%p
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20260615081352_backfill_sponsor_number_of_coaches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class BackfillSponsorNumberOfCoaches < ActiveRecord::Migration[8.1]
# Backfill nil number_of_coaches using the same formula as Sponsor#coach_spots
# to ensure existing records pass the new presence validation when edited.
def up
Sponsor.where(number_of_coaches: nil)
.update_all("number_of_coaches = ROUND(seats / 2.0)")
end

def down
# Intentionally irreversible: we can't distinguish original nils from
# intentionally-set values after backfill.
raise ActiveRecord::IrreversibleMigration
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.1].define(version: 2026_04_10_174346) do
ActiveRecord::Schema[8.1].define(version: 2026_06_15_081352) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"

Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/admin/sponsors_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
expect do
post :create, params: {
sponsor: {
name: 'name', website: 'https://example.com', seats: 40,
name: 'name', website: 'https://example.com', seats: 40, number_of_coaches: 10,
address: address, avatar: avatar
}
}
Expand All @@ -53,7 +53,7 @@
expect do
post :create, params: {
sponsor: {
name: 'name', website: 'https://example.com', seats: 40,
name: 'name', website: 'https://example.com', seats: 40, number_of_coaches: 10,
address: address, avatar: avatar, contact_ids: [member.id, member1.id]
}
}
Expand All @@ -67,7 +67,7 @@
expect do
post :create, params: {
sponsor: {
name: 'name', website: 'https://example.com', seats: 40,
name: 'name', website: 'https://example.com', seats: 40, number_of_coaches: 10,
address: address, avatar: avatar, contact_ids: [member.id, member1.id]
}
}
Expand All @@ -81,7 +81,7 @@
expect do
post :create, params: {
sponsor: {
name: 'name', website: 'https://example.com', seats: 40,
name: 'name', website: 'https://example.com', seats: 40, number_of_coaches: 10,
address: Fabricate(:address, latitude: '54.47474', longitude: '-0.12345'),
avatar: avatar, members: []
}
Expand Down
2 changes: 2 additions & 0 deletions spec/fabricators/sponsor_fabricator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
website { "http://#{Faker::Internet.domain_name}" }
avatar { Rack::Test::UploadedFile.new(Rails.root.join('app', 'assets', 'images', 'sponsors', sponsor_image), 'image/png') }
address
seats { Faker::Number.between(from: 1, to: 50) }
number_of_coaches { Faker::Number.between(from: 1, to: 50) }
end

Fabricator(:sponsor_full, from: :sponsor) do
Expand Down
3 changes: 3 additions & 0 deletions spec/features/admin/manage_sponsor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
fill_in 'Website', with: 'https://www.sponsorname.com/'
attach_file('Avatar', Rails.root + 'spec/support/codebar-logo.png')
fill_in 'Student spots', with: 20
fill_in 'Coach spots', with: 10
select "Bronze", from: "Level"

click_on 'Create sponsor'
Expand All @@ -32,6 +33,7 @@
fill_in 'Website', with: 'https://www.sponsorname.com/'
attach_file('Avatar', Rails.root + 'spec/support/codebar-logo.png')
fill_in 'Student spots', with: 20
fill_in 'Coach spots', with: 10

click_on 'Create sponsor'

Expand All @@ -47,6 +49,7 @@
fill_in 'Website', with: 'https://www.sponsorname.com/'
attach_file('Avatar', Rails.root + 'spec/support/codebar-logo.png')
fill_in 'Student spots', with: 20
fill_in 'Coach spots', with: 10

click_on 'Create sponsor'

Expand Down
4 changes: 4 additions & 0 deletions spec/models/sponsor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
it { is_expected.to validate_presence_of(:avatar) }
it { is_expected.to validate_presence_of(:website) }
it { is_expected.to validate_presence_of(:level) }
it { is_expected.to validate_presence_of(:number_of_coaches) }
it { is_expected.to validate_presence_of(:seats) }
it { is_expected.to validate_numericality_of(:number_of_coaches).is_greater_than_or_equal_to(0).only_integer }
it { is_expected.to validate_numericality_of(:seats).is_greater_than_or_equal_to(0).only_integer }

context 'scopes' do
describe 'searching by_name' do
Expand Down