Module: Calagator::DuplicateChecking::ClassMethods

Defined in:
lib/calagator/duplicate_checking.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/calagator/duplicate_checking.rb', line 88

def self.extended(klass)
  klass.instance_eval do
    cattr_accessor(:_duplicate_checking_ignores_attributes) { Set.new }
    cattr_accessor(:_duplicate_squashing_ignores_associations) { Set.new }
    cattr_accessor(:_duplicate_finding_scope) { -> { all } }
    cattr_accessor(:_after_squashing_duplicates) { ->(primary) {} }

    belongs_to :duplicate_of, class_name: name, foreign_key: DUPLICATE_MARK_COLUMN
    has_many   :duplicates,   class_name: name, foreign_key: DUPLICATE_MARK_COLUMN

    scope :marked_duplicates, -> { where("#{table_name}.#{DUPLICATE_MARK_COLUMN} IS NOT NULL") }
    scope :non_duplicates, -> { where("#{table_name}.#{DUPLICATE_MARK_COLUMN} IS NULL") }
  end
end

Instance Method Details

#after_squashing_duplicates(*args) ⇒ Object



124
125
126
127
# File 'lib/calagator/duplicate_checking.rb', line 124

def after_squashing_duplicates(*args)
  self._after_squashing_duplicates = args.first unless args.empty?
  _after_squashing_duplicates
end

#duplicate_checking_ignores_attributes(*args) ⇒ Object

Return set of attributes that should be ignored for duplicate checking



104
105
106
107
108
109
# File 'lib/calagator/duplicate_checking.rb', line 104

def duplicate_checking_ignores_attributes(*args)
  unless args.empty?
    _duplicate_checking_ignores_attributes.merge(args.map(&:to_sym))
  end
  DUPLICATE_CHECKING_IGNORES_ATTRIBUTES + _duplicate_checking_ignores_attributes
end

#duplicate_finding_scope(*args) ⇒ Object



119
120
121
122
# File 'lib/calagator/duplicate_checking.rb', line 119

def duplicate_finding_scope(*args)
  self._duplicate_finding_scope = args.first unless args.empty?
  _duplicate_finding_scope
end

#duplicate_squashing_ignores_associations(*args) ⇒ Object

Return set of associations that will be ignored during duplicate squashing



112
113
114
115
116
117
# File 'lib/calagator/duplicate_checking.rb', line 112

def duplicate_squashing_ignores_associations(*args)
  unless args.empty?
    _duplicate_squashing_ignores_associations.merge(args.map(&:to_sym))
  end
  _duplicate_squashing_ignores_associations
end

#find_duplicates_by_type(type) ⇒ Object

Return Hash of duplicate events grouped by the type.



130
131
132
133
134
# File 'lib/calagator/duplicate_checking.rb', line 130

def find_duplicates_by_type(type)
  DuplicateFinder.new(self, type.split(',')).find do |scope|
    scope.instance_exec &duplicate_finding_scope
  end
end

#squash(primary, duplicates) ⇒ Object

Squash duplicates. Options accept ActiveRecord instances or IDs.

Options: :duplicates => ActiveRecord instance(s) to mark as duplicates :primary => ActiveRecord instance to use as the primary record



141
142
143
144
145
# File 'lib/calagator/duplicate_checking.rb', line 141

def squash(primary, duplicates)
  DuplicateSquasher.new(primary, duplicates, name.downcase).squash.tap do |squasher|
    after_squashing_duplicates.call(primary) unless squasher.failure
  end
end