class Form::ActiveModel::ModelValidations::ValidationCopier
TODO: extract Composition behaviour. reduce code in Mapping
.
Public Class Methods
copy(form_class, mapping, models)
click to toggle source
# File lib/reform/form/active_model/model_validations.rb, line 8 def self.copy(form_class, mapping, models) if models.is_a?(Hash) models.each do |model_name, model| new(form_class, mapping, model, model_name).copy end else new(form_class, mapping, models).copy end end
new(form_class, mapping, model, model_name=nil)
click to toggle source
# File lib/reform/form/active_model/model_validations.rb, line 18 def initialize(form_class, mapping, model, model_name=nil) @form_class = form_class @mapping = mapping @model = model @model_name = model_name end
Public Instance Methods
copy()
click to toggle source
# File lib/reform/form/active_model/model_validations.rb, line 25 def copy @model.validators.each(&method(:add_validator)) end
Private Instance Methods
add_custom_validator(validator)
click to toggle source
# File lib/reform/form/active_model/model_validations.rb, line 46 def add_custom_validator validator @form_class.validates(nil, {validator.kind => validator.options}) end
add_native_validator(validator)
click to toggle source
# File lib/reform/form/active_model/model_validations.rb, line 39 def add_native_validator validator attributes = inverse_map_attributes(validator.attributes) if attributes.any? @form_class.validates(*attributes, {validator.kind => validator.options}) end end
add_validator(validator)
click to toggle source
# File lib/reform/form/active_model/model_validations.rb, line 31 def add_validator(validator) if validator.respond_to?(:attributes) add_native_validator validator else add_custom_validator validator end end
create_attributes(attributes)
click to toggle source
# File lib/reform/form/active_model/model_validations.rb, line 54 def create_attributes(attributes) attributes.map do |attribute| [@model_name, attribute].compact end end
inverse_map_attributes(attributes)
click to toggle source
# File lib/reform/form/active_model/model_validations.rb, line 50 def inverse_map_attributes(attributes) @mapping.inverse_image(create_attributes(attributes)) end