Module: IsUnique::ClassMethods

Defined in:
lib/is_unique.rb

Instance Method Summary collapse

Instance Method Details

#is_unique(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/is_unique.rb', line 9

def is_unique(options = {})

  write_inheritable_attribute :is_unique_hash_column, options[:hash_column] || 'unique_hash'
  class_inheritable_reader    :is_unique_hash_column

  write_inheritable_attribute :is_unique_ignore, [
    primary_key,
    is_unique_hash_column.to_s,
    'created_at',
    'updated_at'
  ].concat(Array(options[:ignore]).map(&:to_s))

  unless self.include?(IsUnique::InstanceMethods)
    self.class_eval do
      include IsUnique::InstanceMethods

      before_save :calculate_unique_hash
    end
  end
end