Module: Calagator::DuplicateChecking::ClassMethods
- Defined in:
- lib/calagator/duplicate_checking.rb
Class Method Summary collapse
Instance Method Summary collapse
- #after_squashing_duplicates(*args) ⇒ Object
-
#duplicate_checking_ignores_attributes(*args) ⇒ Object
Return set of attributes that should be ignored for duplicate checking.
- #duplicate_finding_scope(*args) ⇒ Object
-
#duplicate_squashing_ignores_associations(*args) ⇒ Object
Return set of associations that will be ignored during duplicate squashing.
-
#find_duplicates_by_type(type) ⇒ Object
Return Hash of duplicate events grouped by the
type
. -
#squash(master, duplicates) ⇒ Object
Squash duplicates.
Class Method Details
.extended(klass) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/calagator/duplicate_checking.rb', line 85 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) { ->(master) {} } 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
117 118 119 120 |
# File 'lib/calagator/duplicate_checking.rb', line 117 def after_squashing_duplicates(*args) self._after_squashing_duplicates = args.first unless args.empty? self._after_squashing_duplicates end |
#duplicate_checking_ignores_attributes(*args) ⇒ Object
Return set of attributes that should be ignored for duplicate checking
101 102 103 104 |
# File 'lib/calagator/duplicate_checking.rb', line 101 def duplicate_checking_ignores_attributes(*args) _duplicate_checking_ignores_attributes.merge(args.map(&:to_sym)) unless args.empty? DUPLICATE_CHECKING_IGNORES_ATTRIBUTES + _duplicate_checking_ignores_attributes end |
#duplicate_finding_scope(*args) ⇒ Object
112 113 114 115 |
# File 'lib/calagator/duplicate_checking.rb', line 112 def duplicate_finding_scope(*args) self._duplicate_finding_scope = args.first unless args.empty? self._duplicate_finding_scope end |
#duplicate_squashing_ignores_associations(*args) ⇒ Object
Return set of associations that will be ignored during duplicate squashing
107 108 109 110 |
# File 'lib/calagator/duplicate_checking.rb', line 107 def duplicate_squashing_ignores_associations(*args) _duplicate_squashing_ignores_associations.merge(args.map(&:to_sym)) unless args.empty? _duplicate_squashing_ignores_associations end |
#find_duplicates_by_type(type) ⇒ Object
Return Hash of duplicate events grouped by the type
.
123 124 125 126 127 |
# File 'lib/calagator/duplicate_checking.rb', line 123 def find_duplicates_by_type(type) DuplicateFinder.new(self, type.split(",")).find do |scope| scope.instance_exec &duplicate_finding_scope end end |
#squash(master, duplicates) ⇒ Object
Squash duplicates. Options accept ActiveRecord instances or IDs.
Options: :duplicates => ActiveRecord instance(s) to mark as duplicates :master => ActiveRecord instance to use as master
134 135 136 137 138 |
# File 'lib/calagator/duplicate_checking.rb', line 134 def squash(master, duplicates) DuplicateSquasher.new(master, duplicates, name.downcase).squash.tap do |squasher| after_squashing_duplicates.call(master) unless squasher.failure end end |