Sha256: 9086b3adebbdab0493eb56ba6ff0c03460059f6df2d4034bfd75c4c94145cf11

Contents?: true

Size: 1.19 KB

Versions: 21

Compression:

Stored size: 1.19 KB

Contents

class CreateEditableTexts < MongoidMigration::Migration
  def self.up
    self.pages.each do |page|
      attributes = {}

      page['editable_elements'].each_with_index do |element, index|
        next unless element['_type'] =~ /Text$/
        attributes.merge!(new_attributes_for(element['_type'], index))
      end

      self.update_page(page['_id'], attributes)
    end
  end

  def self.down

  end

  protected

  def self.new_attributes_for(type, index)
    {
      "editable_elements.#{index}._type"       => 'Locomotive::EditableText',
      "editable_elements.#{index}.format"      => type.ends_with?('LongText') ? 'html' : 'raw',
      "editable_elements.#{index}.rows"        => type.ends_with?('LongText') ? 15 : 2,
      "editable_elements.#{index}.line_break"  => type.ends_with?('LongText') ? true : false
    }
  end

  def self.pages
    Locomotive::Page.collection.find('editable_elements._type' => {
      '$in' => ['Locomotive::EditableShortText', 'Locomotive::EditableLongText']
    })
  end

  def self.update_page(id, attributes)
    selector      = { '_id' => id }
    modifications = { '$set' => attributes }
    Locomotive::Page.collection.find(selector).update(modifications)
  end

end

Version data entries

21 entries across 21 versions & 1 rubygems

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