Module: ActiveRecord::Validations::ClassMethods

Defined in:
lib/classy-inheritance.rb

Instance Method Summary collapse

Instance Method Details

#validates_associated_dependent(model_sym, options, configuration = {}) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/classy-inheritance.rb', line 306

def validates_associated_dependent(model_sym, options, configuration = {})
  configuration = { :message => I18n.translate('activerecord.errors.messages')[:invalid], :on => :save }.update(configuration)

  validates_each(model_sym, configuration) do |record, attr_name, value|
    associate = record.send(attr_name)
    if associate && !associate.valid?
      associate.errors.each do |key, value|
        if options[:prefix]
          key = (options[:prefix] == true) ? "#{model_sym}_#{key}" : "#{options[:prefix]}_#{key}"
        end
        if options[:postfix]
          key = (options[:postfix] == true) ? "#{key}_#{model_sym}" : "#{key}_#{options[:postfix]}"
        end
        record.errors.add(key, value) unless record.errors[key]
      end
    end
  end
end