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.3.13 lib/mini_defender/rules/exists.rb
mini_defender-0.3.12 lib/mini_defender/rules/exists.rb
mini_defender-0.3.11 lib/mini_defender/rules/exists.rb
mini_defender-0.3.10 lib/mini_defender/rules/exists.rb
mini_defender-0.3.9 lib/mini_defender/rules/exists.rb
mini_defender-0.3.8 lib/mini_defender/rules/exists.rb
mini_defender-0.3.7 lib/mini_defender/rules/exists.rb
mini_defender-0.3.6 lib/mini_defender/rules/exists.rb
mini_defender-0.3.5 lib/mini_defender/rules/exists.rb
mini_defender-0.3.2 lib/mini_defender/rules/exists.rb
mini_defender-0.3.0 lib/mini_defender/rules/exists.rb
mini_defender-0.2.0 lib/mini_defender/rules/exists.rb
mini_defender-0.1.10 lib/mini_defender/rules/exists.rb
mini_defender-0.1.9 lib/mini_defender/rules/exists.rb
mini_defender-0.1.8 lib/mini_defender/rules/exists.rb
mini_defender-0.1.6 lib/mini_defender/rules/exists.rb
mini_defender-0.1.4 lib/mini_defender/rules/exists.rb
mini_defender-0.1.3 lib/mini_defender/rules/exists.rb
mini_defender-0.1.2 lib/mini_defender/rules/exists.rb
mini_defender-0.1.1 lib/mini_defender/rules/exists.rb