Module: Calagator::DuplicateChecking

Included in:
Event, Venue
Defined in:
lib/calagator/duplicate_checking.rb,
lib/calagator/duplicate_checking/duplicate_finder.rb,
lib/calagator/duplicate_checking/controller_actions.rb,
lib/calagator/duplicate_checking/duplicate_squasher.rb

Defined Under Namespace

Modules: ClassMethods, ControllerActions Classes: DuplicateFinder, DuplicateSquasher

Constant Summary collapse

DUPLICATE_MARK_COLUMN =
:duplicate_of_id
DEFAULT_SQUASH_METHOD =
:mark
DUPLICATE_CHECKING_IGNORES_ATTRIBUTES =
Set.new((%w[created_at updated_at id] + [DUPLICATE_MARK_COLUMN]).map(&:to_sym))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



47
48
49
# File 'lib/calagator/duplicate_checking.rb', line 47

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#duplicate?Boolean Also known as: marked_as_duplicate?

Returns:

  • (Boolean)


51
52
53
# File 'lib/calagator/duplicate_checking.rb', line 51

def duplicate?
  duplicate_of.present?
end

#find_exact_duplicatesObject

Return either an Array of exact duplicates for this record, or nil if no exact duplicates were found.

Note that this method requires that all associations are set before this method is called.



79
80
81
82
83
84
85
# File 'lib/calagator/duplicate_checking.rb', line 79

def find_exact_duplicates
  matchable_attributes = attributes.reject do |key, _value|
    self.class.duplicate_checking_ignores_attributes.include?(key.to_sym)
  end
  duplicates = self.class.where(matchable_attributes).reject { |t| t.id == id }
  duplicates.presence
end

#originatorObject

Return the ultimate primary for a record, which may be the record itself.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/calagator/duplicate_checking.rb', line 61

def originator
  parent = self
  seen = Set.new

  loop do
    return parent if parent.primary?
    if seen.include?(parent)
      raise DuplicateCheckingError, "Loop detected in duplicates chain at #{parent.class}##{parent.id}"
    end

    seen << parent
    parent = parent.duplicate_of
  end
end

#primary?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/calagator/duplicate_checking.rb', line 56

def primary?
  !duplicate?
end