Move normalising back into model

This commit is contained in:
Christian Schmidt 2024-10-30 20:39:52 +01:00
parent a3da6fb3a1
commit ac50f57e2c
3 changed files with 6 additions and 10 deletions

View File

@ -19,13 +19,7 @@ class Settings::VerificationsController < Settings::BaseController
def account_params
params.require(:account).permit(:attribution_domains).tap do |params|
params[:attribution_domains] = params[:attribution_domains]&.lines&.map do |line|
line
.strip
.delete_prefix('http://')
.delete_prefix('https://')
.delete_prefix('*.')
end
params[:attribution_domains] = params[:attribution_domains].split if params[:attribution_domains]
end
end

View File

@ -4,7 +4,7 @@ module Account::AttributionDomains
extend ActiveSupport::Concern
included do
normalizes :attribution_domains, with: ->(arr) { arr.uniq }
normalizes :attribution_domains, with: ->(arr) { arr.filter_map { |str| str.to_s.strip.delete_prefix('http://').delete_prefix('https://').delete_prefix('*.').presence }.uniq }
validates :attribution_domains, domain: true, length: { maximum: 100 }, if: -> { local? && will_save_change_to_attribution_domains? }
end

View File

@ -756,7 +756,9 @@ RSpec.describe Account do
end
describe 'attribution_domains' do
it { is_expected.to normalize(:attribution_domains).from(['example.com', 'example.com', 'example.net']).to(['example.com', 'example.net']) }
it { is_expected.to normalize(:attribution_domains).from(['example.com', ' example.com ', ' example.net ']).to(['example.com', 'example.net']) }
it { is_expected.to normalize(:attribution_domains).from(['https://example.com', 'http://example.net', '*.example.org']).to(['example.com', 'example.net', 'example.org']) }
it { is_expected.to normalize(:attribution_domains).from(['', ' ', nil]).to([]) }
end
end
@ -800,7 +802,7 @@ RSpec.describe Account do
it { is_expected.to_not allow_values(account_note_over_limit).for(:note) }
it { is_expected.to allow_values([], ['example.com'], (1..100).to_a).for(:attribution_domains) }
it { is_expected.to_not allow_values([''], ['@'], (1..101).to_a).for(:attribution_domains) }
it { is_expected.to_not allow_values(['example com'], ['@'], (1..101).to_a).for(:attribution_domains) }
end
context 'when account is remote' do