Sha256: 857107811218f8cfcf6e812f9b016ffd9b04779d331cd28ae57cb8fe2c5a3b75

Contents?: true

Size: 1.31 KB

Versions: 21

Compression:

Stored size: 1.31 KB

Contents

class LocalizeSlugsOfContentEntries < MongoidMigration::Migration
  def self.up
    Locomotive::Site.all.each do |site|
      puts "[#{site.name}] - #{site.default_locale} / #{site.locales}"

      site.content_entries.each do |entry|
        next if entry.attributes['_slug'].is_a?(Hash) # already translated

        puts "\t#{entry._label} is not translated"

        self.update_entry(site, entry)
      end
    end
  end

  def self.down
    Locomotive::ContentEntry.all.each do |entry|
      selector      = { '_id' => entry._id }
      modifications = entry.attributes['_slug'].values.first

      Locomotive::ContentEntry.collection.find(selector).update({ '$set' => { '_slug' => modifications } })
    end
  end

  # 2 cases:
  # - not translated, set the slug for all the locales of the site
  # - already translated copy the SAME slug for all the locales of the entry
  #
  def self.update_entry(site, entry)
    selector      = { '_id' => entry._id }
    modifications = {}
    slug          = Mongoid::Fields::I18n.with_locale(site.default_locale) { entry._slug }

    locales = entry.localized? ? entry.translated_in : site.locales

    locales.each do |locale|
      modifications[locale.to_s] = slug
    end

    Locomotive::ContentEntry.collection.find(selector).update({ '$set' => { '_slug' => modifications } })
  end

end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
locomotive_cms-2.5.7 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.6 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.6.rc2 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.6.rc1 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.5 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.4 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.3 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.2 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.1 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.0 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.0.rc3 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.0.rc2 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.5.0.rc1 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.4.1 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.4.0 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.3.1 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.3.0 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.2.3 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.2.2 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb
locomotive_cms-2.2.1 mongodb/migrate/20130627101548_localize_slugs_of_content_entries.rb