Class: MiniDefender::Rules::Exists

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/exists.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

#active?, available?, #bails?, #coerce, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, #priority, #stops?, #with_message

Constructor Details

#initialize(model, column) ⇒ Exists

Returns a new instance of Exists.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
# File 'lib/mini_defender/rules/exists.rb', line 4

def initialize(model, column)
  raise ArgumentError, 'model name must be a string or ActiveRecord::Base' unless model.is_a?(String)
  raise ArgumentError, 'Column name must be a string' unless column.is_a?(String)

  @model = model.camelcase.constantize
  @column = column
end

Class Method Details

.make(args) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/mini_defender/rules/exists.rb', line 16

def self.make(args)
  raise ArgumentError, 'Model and column are required.' unless args.length == 2

  self.new(args[0], args[1])
end

.signatureObject



12
13
14
# File 'lib/mini_defender/rules/exists.rb', line 12

def self.signature
  'exists'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



26
27
28
# File 'lib/mini_defender/rules/exists.rb', line 26

def message(attribute, value, validator)
  "The value does not exists."
end

#passes?(attribute, value, validator) ⇒ Boolean

Returns:



22
23
24
# File 'lib/mini_defender/rules/exists.rb', line 22

def passes?(attribute, value, validator)
  @model.where(@column => value).exists?
end