Sha256: c6812ce3eec407751346cd18cddb3f6f5e42baebf72032c95d5ee42efe46f8af

Contents?: true

Size: 832 Bytes

Versions: 10

Compression:

Stored size: 832 Bytes

Contents

class AddPositionToPages < ActiveRecord::Migration
  def self.up
    unless Page.column_names.include?('position')   # ie. unless the reorder extension has been installed
      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.find_all_by_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.find(:all, :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

10 entries across 10 versions & 1 rubygems

Version Path
radiant-drag_order-extension-0.5.0 db/migrate/01_add_position_to_pages.rb
radiant-drag_order-extension-0.4.8 db/migrate/01_add_position_to_pages.rb
radiant-drag_order-extension-0.4.7 db/migrate/01_add_position_to_pages.rb
radiant-drag_order-extension-0.4.6 db/migrate/01_add_position_to_pages.rb
radiant-drag_order-extension-0.4.4 db/migrate/01_add_position_to_pages.rb
radiant-drag_order-extension-0.4.2 db/migrate/01_add_position_to_pages.rb
radiant-drag_order-extension-0.4.0.beta.2 db/migrate/01_add_position_to_pages.rb
radiant-drag_order-extension-0.3.9 db/migrate/01_add_position_to_pages.rb
radiant-drag_order-extension-0.3.8 db/migrate/01_add_position_to_pages.rb
radiant-drag_order-extension-0.3.7 db/migrate/01_add_position_to_pages.rb