2019-07-30 21:29:24 +00:00
# frozen_string_literal: true
2018-03-25 16:15:39 +00:00
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
2018-03-25 17:30:42 +00:00
class NameCopyNamingAlgorithm < CopyNamingAlgorithm
attr_accessor :klass , :nonprofit_id
# @param [Class] klass
def initialize ( klass , nonprofit_id )
2019-07-30 21:29:24 +00:00
@klass = klass
@nonprofit_id = nonprofit_id
2018-03-25 17:30:42 +00:00
end
def copy_addition
" ( #{ Time . now . strftime ( '%F' ) } copy) "
end
def separator_before_copy_number
2019-07-30 21:29:24 +00:00
' '
2018-03-25 17:30:42 +00:00
end
def max_copies
30
end
def max_length
CreateCampaign :: CAMPAIGN_NAME_LENGTH_LIMIT
end
def get_name_for_entity ( name_entity )
name_entity . name
end
def get_already_used_name_entities ( base_name )
2019-07-30 21:29:24 +00:00
end_name = " #{ copy_addition . gsub ( '(' , '\\(' ) . gsub ( ')' , '\\)' ) } \\ d{2} "
2018-03-25 17:30:42 +00:00
end_name_length = copy_addition . length + 3
amount_to_strip = end_name_length + base_name . length - max_length
2019-07-30 21:29:24 +00:00
amount_to_strip = 0 if amount_to_strip < 0
@klass . method ( :where ) . call ( 'name SIMILAR TO ? AND nonprofit_id = ? AND (deleted IS NULL OR deleted = false)' , " #{ base_name [ 0 .. base_name . length - amount_to_strip - 1 ] } _* " + end_name , nonprofit_id ) . select ( 'name' )
2018-03-25 17:30:42 +00:00
end
2019-07-30 21:29:24 +00:00
end