Use true/false for boolean value in form

This commit is contained in:
Matt Jankowski 2025-09-26 13:10:04 -04:00
parent 31abef8917
commit 3815a6c56c
3 changed files with 19 additions and 15 deletions

View File

@ -35,13 +35,7 @@ class UsernameBlock < ApplicationRecord
normalizes :normalized_username, with: ->(value) { value.downcase.gsub(Regexp.union(HOMOGLYPHS.keys), HOMOGLYPHS) }
def comparison
exact? ? 'equals' : 'contains'
end
def comparison=(val)
self.exact = val == 'equals'
end
alias_attribute :comparison, :exact
def self.matches?(str, allow_with_approval: false)
matches_exactly(str).or(matches_partially(str)).where(allow_with_approval: allow_with_approval).any?

View File

@ -7,9 +7,9 @@
= form.input :comparison,
as: :select,
wrapper: :with_block_label,
collection: %w(equals contains),
collection: [[:equals, true], [:contains, false]],
include_blank: false,
label_method: ->(type) { I18n.t(type, scope: 'admin.username_blocks.comparison') }
label_method: ->(type) { I18n.t(type.first, scope: 'admin.username_blocks.comparison') }
.fields-group
= form.input :allow_with_approval,

View File

@ -24,13 +24,23 @@ RSpec.describe 'Admin Username Blocks' do
.to have_http_status(400)
end
it 'creates a username block' do
post admin_username_blocks_path(username_block: { username: 'banana', comparison: 'contains', allow_with_approval: '0' })
context 'with valid params' do
subject { post admin_username_blocks_path(username_block: { username: 'banana', comparison: 'false', allow_with_approval: '0' }) }
expect(response)
.to redirect_to(admin_username_blocks_path)
expect(UsernameBlock.find_by(username: 'banana'))
.to_not be_nil
it 'creates a username block' do
expect { subject }
.to change(UsernameBlock, :count).by(1)
expect(response)
.to redirect_to(admin_username_blocks_path)
expect(UsernameBlock.last)
.to be_present
.and have_attributes(
allow_with_approval: false,
exact: false,
username: 'banana'
)
end
end
end