Skip to content

Commit

Permalink
Improve uniqueness validator
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed Jan 22, 2017
1 parent bfbf8cf commit 10f2f2e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AllCops:
TargetRubyVersion: 2.2

Metrics/AbcSize:
Max: 41.39 # TODO: Lower to 15
Max: 37.23 # TODO: Lower to 15

Metrics/BlockLength:
Exclude:
Expand Down
23 changes: 15 additions & 8 deletions lib/client_side_validations/active_record/uniqueness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@ def client_side_hash(model, attribute, _force = nil)
hash[:id] = model.id unless model.new_record?
hash[:allow_blank] = true if options[:allow_blank]

apply_class_option! hash, model
apply_scope_option! hash, model

hash
end

private

def apply_class_option!(hash, model)
if options.key?(:client_validations) && options[:client_validations].key?(:class)
hash[:class] = options[:client_validations][:class].underscore
elsif model.class.name.demodulize != model.class.name
hash[:class] = model.class.name.underscore
end
end

if options.key?(:scope) && options[:scope].present?
hash[:scope] = Array.wrap(options[:scope]).inject({}) do |scope_hash, scope_item|
scope_hash.merge!(scope_item => model.send(scope_item))
end
end
def apply_scope_option!(hash, model)
return unless options.key?(:scope) && options[:scope].present?

hash
hash[:scope] = Array.wrap(options[:scope]).inject({}) do |scope_hash, scope_item|
scope_hash.merge!(scope_item => model.send(scope_item))
end
end

private

def message_type
:taken
end
Expand Down

0 comments on commit 10f2f2e

Please sign in to comment.