Sha256: 251a3c4edbbfc6cf9ef97d39b8cc0694685118586142bb3637bc194440b8aba4

Contents?: true

Size: 748 Bytes

Versions: 106

Compression:

Stored size: 748 Bytes

Contents

class AddPositionToPages < ActiveRecord::Migration[5.2]
  def self.up
    unless column_exists? :pages, :position
      add_column :pages, :position, :integer
      Page.reset_column_information
      say_with_time("Putting all pages in a default order...") do
        ActiveRecord::Base.record_timestamps = false
        Page.where(parent_id: nil).each do |p|
          put_children_into_list(p)
        end
        ActiveRecord::Base.record_timestamps = true
      end
    end
  end

  def self.down
    remove_column :pages, :position
  end

  def self.put_children_into_list(page)
    page.children.order("title asc").each_with_index do |pg, idx|
      pg.update_attribute('position', idx + 1)
      put_children_into_list(pg)
    end
  end
end

Version data entries

106 entries across 106 versions & 1 rubygems

Version Path
trusty-cms-6.3.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-7.0.2 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-7.0.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-7.0.0 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.3 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.2.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.2.2.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.2.0 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.1.2 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.1.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.0.5 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.0.4 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.0.3 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.0.2 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.0.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-5.6.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-6.0.0 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-5.6.0 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-5.5.9 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-5.5.8 db/migrate/20161027141250_add_position_to_pages.rb