File: uniqueness.rb

package info (click to toggle)
ruby-client-side-validations 3.2.6%2Bgh-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 676 kB
  • sloc: javascript: 3,019; ruby: 2,959; makefile: 2
file content (32 lines) | stat: -rw-r--r-- 1,028 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module ClientSideValidations::ActiveRecord
  module Uniqueness
    def client_side_hash(model, attribute, force = nil)
      hash = {}
      hash[:message]        = model.errors.generate_message(attribute, message_type, options.except(:scope))
      hash[:case_sensitive] = options[:case_sensitive]
      hash[:id]             = model.id unless model.new_record?
      hash[:allow_blank]    = true if options[:allow_blank]

      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

      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

      hash
    end

    private

    def message_type
      :taken
    end
  end
end