Sha256: 16f4425d0dca641a55c50f77d5a8d33287111307c250df02117a59504f3ff6f0

Contents?: true

Size: 1.35 KB

Versions: 35

Compression:

Stored size: 1.35 KB

Contents

class ChangeTaxonsToNestedSet < ActiveRecord::Migration
  def up
    add_column :taxons, :lft, :integer
    add_column :taxons, :rgt, :integer

    Spree::Taxon.reset_column_information # So the new root ids get saved

    # Temporarily set the table back to taxons
    Spree::Taxon.table_name = 'taxons'

    Spree::Taxon.class_eval do
      # adapted from awesome nested set to use 'position' information
      indices = {}

      left_column_name = 'lft'
      right_column_name = 'rgt'
      quoted_parent_column_name = 'parent_id'
      scope = lambda{|node|}

      set_left_and_rights = lambda do |node|
        # set left
        node[left_column_name] = indices[scope.call(node)] += 1
        # find
        where("#{quoted_parent_column_name} = ?", node).order('position ASC').each{ |n| set_left_and_rights.call(n) }
        # set right
        node[right_column_name] = indices[scope.call(node)] += 1
        node.save!
      end

      # Find root node(s)
      where("#{quoted_parent_column_name}" => nil).order('position ASC').each do |root_node|
        # setup index for this scope
        indices[scope.call(root_node)] ||= 0
        set_left_and_rights.call(root_node)
      end
    end

    # Set it back after the migration
    Spree::Taxon.table_name = 'spree_taxons'
  end

  def down
    remove_column :taxons, :lft
    remove_column :taxons, :rgt
  end
end

Version data entries

35 entries across 35 versions & 3 rubygems

Version Path
spree_core-1.2.5 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.1.6 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.1.5 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.2.4 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.0.7 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.2.3 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.2.2 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.1.4 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.2.0 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.2.0.rc2 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.2.0.rc1 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.1.3 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.0.6 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_pag_seguro-1.0.7 spec/dummy/db/migrate/20120301154053_change_taxons_to_nested_set.rb
spree_core-1.1.2 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-1.1.2.rc1 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_pag_seguro-1.0.6 spec/dummy/db/migrate/20120301154053_change_taxons_to_nested_set.rb
spree_pag_seguro-1.0.5 spec/dummy/db/migrate/20120301154053_change_taxons_to_nested_set.rb
spree_pag_seguro-1.0.4 spec/dummy/db/migrate/20120301154053_change_taxons_to_nested_set.rb
spree_core-1.1.1 db/migrate/20091007134354_change_taxons_to_nested_set.rb