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



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

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

Instance Method Details

#duplicate?Boolean Also known as: marked_as_duplicate?, slave?

Returns:

  • (Boolean)


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

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.



76
77
78
79
80
81
82
# File 'lib/calagator/duplicate_checking.rb', line 76

def find_exact_duplicates
  matchable_attributes = attributes.reject { |key, value|
    self.class.duplicate_checking_ignores_attributes.include?(key.to_sym)
  }
  duplicates = self.class.where(matchable_attributes).reject{|t| t.id == id}
  duplicates.blank? ? nil : duplicates
end

#master?Boolean

Returns:

  • (Boolean)


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

def master?
  !slave?
end

#progenitorObject

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



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

def progenitor
  parent = self
  seen = Set.new

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