class Locomotive::Steam::Adapters::Filesystem::Sanitizers::ContentEntry

Public Instance Methods

apply_to_dataset(dataset) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 19
def apply_to_dataset(dataset)
  dataset.all.each do |entity|
    _apply_to_dataset(entity, dataset)
  end
  clean
end
apply_to_entity(entity) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 10
def apply_to_entity(entity)
  super
  add_label(entity)

  locales.each do |locale|
    set_automatic_translations(entity, locale)
  end
end
apply_to_entity_with_dataset(entity, dataset) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 26
def apply_to_entity_with_dataset(entity, dataset)
  # Note: this statement attaches the site to the entity
  apply_to_entity(entity)


  # make sure it gets an unique slug and an _id + set default values
  _apply_to_dataset(entity, dataset)
end

Private Instance Methods

_apply_to_dataset(entity, dataset) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 37
def _apply_to_dataset(entity, dataset)
  set_slug(entity, dataset)
  set_id(entity)
  set_default_values(entity)
end
add_label(entity) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 43
def add_label(entity)
  value = entity.attributes.delete(:_label)
  name  = entity.content_type.label_field_name

  if entity.attributes[name].respond_to?(:translations) # localized?
    entity.attributes[name][default_locale] = value
  else
    entity.attributes[name] ||= value
  end
end
clean() click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 122
def clean
  @fields_with_default = nil
end
each_field_with_default(entity, &block) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 117
def each_field_with_default(entity, &block)
  @fields_with_default ||= entity.content_type.fields_with_default
  @fields_with_default.each(&block)
end
is_slug_unique?(id, slug, dataset, locale) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 113
def is_slug_unique?(id, slug, dataset, locale)
  dataset.query(locale) { where(_slug: slug, k(:_id, :ne) => id) }.first.nil?
end
set_automatic_translations(entity, locale) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 65
def set_automatic_translations(entity, locale)
  return if locale == default_locale

  entity.localized_attributes.each do |(name, _)|
    next if entity[name].blank?
    entity[name][locale] ||= entity[name][default_locale]
  end
end
set_default_values(entity) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 74
def set_default_values(entity)
  each_field_with_default(entity) do |field|
    name  = field.type == 'select' ? "#{field.name}_id" : field.name
    value = field.localized? ? entity[name][default_locale] : entity[name]

    return unless value.nil?

    if field.localized?
      entity[name].translations = field.default
    else
      entity[name] = field.default
    end
  end
end
set_id(entity) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 54
def set_id(entity)
  # don't override the id if it was set from a MongoDB dump
  return if entity._id =~ /[a-z0-9]{12,}/

  if (slug = entity[:_slug]).respond_to?(:translations)
    entity[:_id] = slug[locale]
  else
    entity[:_id] = slug
  end
end
set_slug(entity, dataset) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 89
def set_slug(entity, dataset)
  if entity._slug.blank?
    if entity._label.respond_to?(:translations)
      entity._label.each do |locale, label|
        entity[:_slug][locale] = slugify(entity._id, label, dataset, locale)
      end
    else
      # same value for any locale
      entity[:_slug].translations = slugify(entity._id, entity._label, dataset)
    end
  end
end
slugify(id, label, dataset, locale = nil) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb, line 102
def slugify(id, label, dataset, locale = nil)
  base, index = label.to_s.permalink(false), nil
  _slugify = -> (i) { [base, i].compact.join('-') }

  while !is_slug_unique?(id, _slugify.call(index), dataset, locale)
    index = index ? index + 1 : 1
  end

  _slugify.call(index)
end