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-3.9.3 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.9.2 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-4.0.0 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.9.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.9.0 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.8.4 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.8.3 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.8.2 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.8.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.8.0 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.7.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.7.0 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.6.2 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.6.1 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.6.0 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.5.5 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.5.4 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.5.3 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.5.2 db/migrate/20161027141250_add_position_to_pages.rb
trusty-cms-3.5.1 db/migrate/20161027141250_add_position_to_pages.rb