Sha256: a1e72e180369ad1260e3629fbc63887149a1faaba190aaffc7a2a58a4e0f54e7

Contents?: true

Size: 934 Bytes

Versions: 4

Compression:

Stored size: 934 Bytes

Contents

module AbAdmin
  module Concerns
    module Validations
      class UniqTranslationValidator < ActiveModel::EachValidator
        def validate_each(record, attribute, value)
          ::I18n.available_locales.each do |l|
            next if record.read_attribute(attribute, locale: l).blank?
            records_scope = record.class.const_get(:Translation).where("#{record.class.model_name.singular}_id != #{record.id || 0}")
            same = records_scope.where(name: record.read_attribute(attribute, locale: l), locale: l.to_s).exists?
            record.errors.add("#{attribute}_#{l}", :taken) if same
          end
        end
      end

      class AssetValidator < ActiveModel::EachValidator
        def validate_each(record, attribute, value)
          if Array(record.fileupload_asset(attribute)).all?{|a| a.new_record? }
            record.errors.add(attribute, :blank)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ab_admin-0.6.1 lib/ab_admin/concerns/validations.rb
ab_admin-0.6.0 lib/ab_admin/concerns/validations.rb
ab_admin-0.5.0 lib/ab_admin/concerns/validations.rb
ab_admin-0.4.0 lib/ab_admin/concerns/validations.rb