Sha256: 98f044be271268ece565a5eb1509f7bc708e95b877a3309c022d4ac02f2ab4a5

Contents?: true

Size: 934 Bytes

Versions: 7

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.foreign_key} != #{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

7 entries across 7 versions & 1 rubygems

Version Path
ab_admin-0.3.6 lib/ab_admin/concerns/validations.rb
ab_admin-0.3.5 lib/ab_admin/concerns/validations.rb
ab_admin-0.3.4 lib/ab_admin/concerns/validations.rb
ab_admin-0.3.3 lib/ab_admin/concerns/validations.rb
ab_admin-0.3.2 lib/ab_admin/concerns/validations.rb
ab_admin-0.3.1 lib/ab_admin/concerns/validations.rb
ab_admin-0.3.0 lib/ab_admin/concerns/validations.rb