Add a Slug P2P campaign naming algorithm

This commit is contained in:
Eric Schultz 2018-05-24 15:45:05 -05:00
parent 46e55da626
commit c474cc0b7b
2 changed files with 97 additions and 0 deletions

View file

@ -0,0 +1,27 @@
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
class SlugP2pCampaignNamingAlgorithm < CopyNamingAlgorithm
attr_accessor :nonprofit_id
# @param [Integer] nonprofit_id
def initialize(nonprofit_id)
@nonprofit_id = nonprofit_id
end
def copy_addition
""
end
def max_copies
999
end
def get_name_for_entity(name_entity)
name_entity.slug
end
def get_already_used_name_entities(base_name)
end_name = "\\_\\d{3}"
Campaign.where('slug SIMILAR TO ? AND nonprofit_id = ? AND (deleted IS NULL OR deleted = false)', base_name + end_name, nonprofit_id).select('slug')
end
end

View file

@ -0,0 +1,70 @@
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
require 'rails_helper'
describe SlugP2pCampaignNamingAlgorithm do
describe '.create_copy_name' do
before(:all) {
Timecop.freeze(2020,5,4)
}
after(:all) {
Timecop.return
}
def set_name(name)
@name = name
end
let(:short_slug) { "slug_name"}
let(:short_slug_copy_today) { "slug_name_000"}
let(:short_slug_copy_today_plus_1) { "slug_name_001"}
let(:copy_base) {"slug_name"}
let(:nonprofit) {force_create(:nonprofit)}
describe 'campaigns' do
let(:campaign) {force_create(:campaign, :slug => @name, nonprofit: nonprofit)}
let(:campaign2) {force_create(:campaign, :slug => @name2, nonprofit:nonprofit)}
let(:campaigns_at_max_copies) { (0..999).collect{|i|
force_create(:campaign, slug: "#{@copy_base}_#{"%03d" % i}", nonprofit:nonprofit)
}}
let(:algo) {SlugP2pCampaignNamingAlgorithm.new( nonprofit.id)}
describe 'campaign slugs' do
it 'not a copy' do
@name = short_slug
campaign
expect(algo.create_copy_name(@name)).to eq short_slug_copy_today
end
it 'one copy exists' do
@name = short_slug
@name2 = short_slug_copy_today
campaign
campaign2
expect(algo.create_copy_name(@name)).to eq short_slug_copy_today_plus_1
expect(algo.create_copy_name(@name2)).to eq short_slug_copy_today_plus_1
end
it 'errors when at max copies' do
@name = short_slug
@copy_base = copy_base
campaign
campaigns_at_max_copies
expect{ algo.create_copy_name(@name) }.to(raise_error{|error|
expect(error).to be_a ArgumentError
})
campaigns_at_max_copies.each {|i|
expect {algo.create_copy_name(i.slug)}.to(raise_error{|error|
expect(error).to be_a ArgumentError
})
}
end
end
end
end
end