Module: WaNew::ClientValidate::ActiveRecord::Uniqueness

Defined in:
lib/wanew/client_validate/active_record/uniqueness.rb

Instance Method Summary collapse

Instance Method Details

#client_hash(model, attribute) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wanew/client_validate/active_record/uniqueness.rb', line 3

def client_hash(model, attribute)
  extra_options = options.except(*::ActiveModel::Errors::CALLBACKS_OPTIONS - [:on, :allow_blank])
  hash = { :message => model.errors.generate_message(attribute, message_type, extra_options.except(:case_sensitive, :scope)) }
  hash = hash.merge(extra_options).merge(model.new_record? ? {} : { :id => model.id })

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

  hash
end