Class: SmrtLinkOptionsValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- SmrtLinkOptionsValidator
- Defined in:
- app/validators/smrt_link_options_validator.rb
Overview
validate SmrtLinkOptions by version:
-
This allows more flexible modification of fields by version
-
each version will have a set of validations
-
on validate it will loop through each of those validations
-
build a validator and validate the record for each specified attribute
-
the validations are standard ActiveRecord ones with specified options
-
This will then run each validator against the specified key
-
Any errors will be added to the record
-
The validator must be a recognised validator as per github.com/rails/rails/tree/main/activemodel/lib/active_model/validations
-
Or you need to create your own validator as per api.rubyonrails.org/classes/ActiveModel/Validator.html
Instance Method Summary collapse
Instance Method Details
#validate(record) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/validators/smrt_link_options_validator.rb', line 25 def validate(record) # If the version is not present no point validating return if record&.run&.smrt_link_version.blank? # Retrieve the list of smrt link options for the specified version record.run.smrt_link_version..each do |smrt_link_option| # Retrieve the list of validations # each one will have a key and some options # key is the validator name # options is a hash e.g. { greater_than_equal_to: 1 } # see the validator docs in ActiveModel for the standard ones smrt_link_option.validations.each do |key, | validator = validator_by_prefix(key) # We then need to create a new instance of the validator # and pass the options along with the attribute name which is the key # of the smrt link option # we need to symbolize the keys as some validators do not recognise string keys # for options instance = validator.new(.merge(attributes: smrt_link_option.key).symbolize_keys) # finally validate the record # underneath it could be validate or validates_each instance.validate(record) end end end |