Class: CamaleonCms::PostUniqValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/camaleon_cms/post_uniq_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/validators/camaleon_cms/post_uniq_validator.rb', line 3

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