Sha256: 84c531b2107c1df7c80663b8fa252dfb727a16639e1561df6303b8eaa1608e5a

Contents?: true

Size: 1.18 KB

Versions: 52

Compression:

Stored size: 1.18 KB

Contents

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

    Taxon.reset_column_information # So the new root ids get saved

    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} IS NULL").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
  end

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

Version data entries

52 entries across 40 versions & 9 rubygems

Version Path
spree_core-0.70.7 db/migrate/20091007134354_change_taxons_to_nested_set.rb
spree_core-0.70.6 db/migrate/20091007134354_change_taxons_to_nested_set.rb
apispree_core-0.0.0 db/migrate/20091007134354_change_taxons_to_nested_set.rb
My-Commerce_core-1.1.0 db/migrate/20091007134354_change_taxons_to_nested_set.rb
My-Commerce_core-1.0.0 db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerceapi-1.0.0 api/spec/test_app/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerceapi-1.0.0 promo/spec/test_app/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerceapi-1.0.0 sandbox/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerceapi-1.0.0 core/spec/test_app/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerceapi-1.0.0 dash/spec/test_app/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerceapi-1.0.0 auth/spec/test_app/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerceapi-1.0.0 core/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerce-0.0.3 auth/spec/test_app/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerce-0.0.3 sandbox/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerce-0.0.3 dash/spec/test_app/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerce-0.0.3 core/spec/test_app/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerce-0.0.3 promo/spec/test_app/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerce-0.0.3 core/db/migrate/20091007134354_change_taxons_to_nested_set.rb
MyCommerce-0.0.3 api/spec/test_app/db/migrate/20091007134354_change_taxons_to_nested_set.rb
rfcommerce_sandbox-0.0.3 db/migrate/20091007134354_change_taxons_to_nested_set.rb