Module: NoBrainer::Document::Validation::Uniqueness
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/no_brainer/document/validation/uniqueness.rb
Defined Under Namespace
Modules: ClassMethods
Classes: UniquenessValidator
Instance Method Summary
collapse
Instance Method Details
#_lock_for_uniqueness_once(key) ⇒ Object
14
15
16
17
|
# File 'lib/no_brainer/document/validation/uniqueness.rb', line 14
def _lock_for_uniqueness_once(key)
@locked_keys_for_uniqueness ||= {}
@locked_keys_for_uniqueness[key] ||= NoBrainer::Config.distributed_lock_class.new(key).tap(&:lock)
end
|
#lock_unique_fields ⇒ Object
24
25
26
27
28
29
30
31
|
# File 'lib/no_brainer/document/validation/uniqueness.rb', line 24
def lock_unique_fields
self.class.unique_validators
.flat_map { |validator| validator.attributes.map { |attr| [attr, validator] } }
.select { |f, validator| validator.should_validate_field?(self, f) }
.map { |f, validator| [f, *validator.scope].map { |k| [k, _read_attribute(k)] } }
.map { |params| self.class._uniqueness_key_name_from_params(params) }
.sort.each { |key| _lock_for_uniqueness_once(key) }
end
|
#save?(options = {}) ⇒ Boolean
XXX we don’t use read_attribute_for_validation, which goes through the user getters, but read internal attributes instead. It makes more sense.
7
8
9
10
11
12
|
# File 'lib/no_brainer/document/validation/uniqueness.rb', line 7
def save?(options={})
lock_unique_fields
super
ensure
unlock_unique_fields
end
|
#unlock_unique_fields ⇒ Object
19
20
21
22
|
# File 'lib/no_brainer/document/validation/uniqueness.rb', line 19
def unlock_unique_fields
(@locked_keys_for_uniqueness || {}).values.each(&:unlock)
@locked_keys_for_uniqueness = {}
end
|