Skip to content

Instantly share code, notes, and snippets.

@kematzy
Created July 6, 2010 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kematzy/465158 to your computer and use it in GitHub Desktop.
Save kematzy/465158 to your computer and use it in GitHub Desktop.
puts `ruby -v`
require 'rubygems'
require 'active_support'
require 'dm-core'
require 'dm-sqlite-adapter'
require 'data_mapper'
require 'dm-is-list'
require 'dm-is-tree'
class Album
include DataMapper::Resource
property :id, Serial
property :parent_id, Integer
property :name, String, :length => 150
property :description, Text, :default => "", :lazy => false
property :url_slug, String, :length => 255
property :created_at, DateTime
property :updated_at, DateTime
is :list, :scope => [:parent_id]
# is :tree, :order => [:parent_id, :position]
before :valid?, :set_default_values_if_missing
before :save do |album|
puts "SAVING ALBUM album=[#{album.inspect}]"
end
before :destroy do |album|
puts "DESTROYING Album=[#{album.inspect}]\n"
end
protected
# update all the records with the new parent id
def unlink_from_parent!
self.class.all(:parent_id => self.parent_id).update(:parent_id => self.parent.parent.id || nil )
end
def set_default_values_if_missing
self.parent_id = nil if self.parent_id.blank?
end
end
DataMapper.setup :default, 'sqlite3::memory:'
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.logger.debug "Using DataMapper::VERSION=[#{DataMapper::VERSION}]"
DataMapper.auto_migrate!
puts "\n======= CREATING ALBUMS ======="
m = Album.create(:name => "Album", :description => 'Album Album', :url_slug => "archive")
ac1 = Album.create(:parent_id => m.id, :name => "Album|Child1", :description => 'Album Child1 Album', :url_slug => "album|child1")
ac2 = Album.create(:parent_id => m.id, :name => "Album|Child2", :description => 'Album Child2 Album', :url_slug => "album|child2")
puts "====== /CREATING ALBUMS ======\n\n"
puts "======== ALL ALBUMS ========="
Album.all.each { |a| puts a.inspect }
puts "======== /ALL ALBUMS ========\n\n"
puts "======== B4 DESTROY ========="
m.destroy
puts "======= /AFTER DESTROY ======\n\n"
puts "======== ALL ALBUMS ========="
Album.all.each { |a| puts a.inspect }
puts "======== /ALL ALBUMS ========\n\n"
@kematzy
Copy link
Author

kematzy commented Jul 6, 2010

DataMaper BUG ?? dm-is-tree and before :destroy callbacks

The above code outputs the before :destroy callback because the [ is :tree,... ] is commented out.

Once I uncomment it, the before :destroy callback stops working.

What am I doing wrong ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment