Class: FeatureValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/feature_validator.rb

Overview

A custom validator to make sure features are correctly assigned to a model.

Adds a presence validation and checks that the manifest is the correct one.

Instance Method Summary collapse

Instance Method Details

#check_validity!Object

Validates the arguiments passed to the validator.

Raises:

  • (ArgumentError)


7
8
9
# File 'app/validators/feature_validator.rb', line 7

def check_validity!
  raise ArgumentError, "You must include a `manifest` option with the name of the manifest to validate when validating a feature" unless options[:manifest].present?
end

#validate_each(record, attribute, feature) ⇒ Object

The actual validator method. It is called when ActiveRecord iterates over all the validators.



13
14
15
16
17
18
19
20
21
22
# File 'app/validators/feature_validator.rb', line 13

def validate_each(record, attribute, feature)
  unless feature
    record.errors[attribute] << :blank
    return
  end

  manifest_name = options[:manifest].respond_to?(:call) ? options[:manifest].call : options[:manifest].to_s

  record.errors[attribute] << :invalid if feature.manifest_name.to_s != manifest_name
end