Sha256: d3447fe9692be969b8497a7af276c6795f3325010e3ec6120184ed1c82486924

Contents?: true

Size: 739 Bytes

Versions: 41

Compression:

Stored size: 739 Bytes

Contents

# frozen_string_literal: true

class MiniDefender::Rules::Exists < MiniDefender::Rule
  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

  def self.signature
    'exists'
  end

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

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

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

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

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
mini_defender-0.6.8 lib/mini_defender/rules/exists.rb
mini_defender-0.6.7 lib/mini_defender/rules/exists.rb
mini_defender-0.6.5 lib/mini_defender/rules/exists.rb
mini_defender-0.6.4 lib/mini_defender/rules/exists.rb
mini_defender-0.6.3 lib/mini_defender/rules/exists.rb
mini_defender-0.6.2 lib/mini_defender/rules/exists.rb
mini_defender-0.6.1 lib/mini_defender/rules/exists.rb
mini_defender-0.6.0 lib/mini_defender/rules/exists.rb
mini_defender-0.5.8 lib/mini_defender/rules/exists.rb
mini_defender-0.5.7 lib/mini_defender/rules/exists.rb
mini_defender-0.5.6 lib/mini_defender/rules/exists.rb
mini_defender-0.5.5 lib/mini_defender/rules/exists.rb
mini_defender-0.5.4 lib/mini_defender/rules/exists.rb
mini_defender-0.5.3 lib/mini_defender/rules/exists.rb
mini_defender-0.5.2 lib/mini_defender/rules/exists.rb
mini_defender-0.5.1 lib/mini_defender/rules/exists.rb
mini_defender-0.5.0 lib/mini_defender/rules/exists.rb
mini_defender-0.4.1 lib/mini_defender/rules/exists.rb
mini_defender-0.4.0 lib/mini_defender/rules/exists.rb
mini_defender-0.3.14 lib/mini_defender/rules/exists.rb