class ValidatesDuplicityOf::Duplicator
Public Class Methods
new(callback, record)
click to toggle source
# File lib/validates_duplicity_of/duplicator.rb, line 3 def initialize(callback, record) @callback = callback @record = Record.new record, callback.attr_name, callback.scope end
Public Instance Methods
validate()
click to toggle source
Updates the attribute of the record
# File lib/validates_duplicity_of/duplicator.rb, line 9 def validate return unless @record.update_required? if changed_attribute_match? @record.attr_value = @record.attr_changed_value else @record.attr_value.concat(next_index) end end
Private Instance Methods
changed_attribute_match?()
click to toggle source
# File lib/validates_duplicity_of/duplicator.rb, line 34 def changed_attribute_match? /#{Regexp.escape(@record.attr_value)} \(\d+\)$/.match @record.attr_changed_value end
next_index()
click to toggle source
Computes the next index
# File lib/validates_duplicity_of/duplicator.rb, line 22 def next_index match_names = record_names.flat_map{ |name| name.match(/\((\d+)\)$/) }.compact match_ids = match_names.flat_map(&:captures).map(&:to_i) index = match_ids.sort.last.to_i + 1 " (#{index})" end
record_names()
click to toggle source
Finds the record names with this match
# File lib/validates_duplicity_of/duplicator.rb, line 30 def record_names @record.where_match("#{@record.attr_value} (%)").pluck(@record.attr_name) end