Sha256: c3f500fa0b246927ce97a07a0bc3e3571306457fd3324e3547b6fa989f29c561
Contents?: true
Size: 1.61 KB
Versions: 5
Compression:
Stored size: 1.61 KB
Contents
module CamaleonCms class PostUniqValidator < ActiveModel::Validator def validate(record) return if record.draft? slug_array = record.slug.to_s.translations_array ptype = record.post_type return unless ptype.present? # only for posts that belongs to a post type model posts = ptype.site.posts .where( "(#{slug_array.map { |s| "#{CamaleonCms::Post.table_name}.slug LIKE '%-->#{s}<!--%'" } .join(' OR ')} ) OR #{CamaleonCms::Post.table_name}.slug = ?", record.slug ) .where.not(id: record.id) .where.not(status: %i[draft draft_child trash]) unless posts.empty? record.errors[:base] << if slug_array.size > 1 "#{I18n.t('camaleon_cms.admin.post.message.requires_different_slug')}: #{posts.pluck(:slug).map do |slug| record.slug.to_s.translations.map do |lng, r_slug| "#{r_slug} (#{lng})" if slug.translations_array.include?(r_slug) end.join(',') end.join(',').split(',').uniq.clean_empty.join(', ')} " else "#{I18n.t('camaleon_cms.admin.post.message.requires_different_slug')}: #{record.slug} " end end # avoid recursive page parent if record.post_parent.present? && ptype.manage_hierarchy? && record.parents.cama_pluck(:id).include?(record.id) record.errors[:base] << I18n.t('camaleon_cms.admin.post.message.recursive_hierarchy', default: 'Parent Post Recursive') end end end end
Version data entries
5 entries across 5 versions & 1 rubygems