Skip to content

Instantly share code, notes, and snippets.

@slaskis
Created February 4, 2010 11:59
Show Gist options
  • Save slaskis/294566 to your computer and use it in GitHub Desktop.
Save slaskis/294566 to your computer and use it in GitHub Desktop.
require "rubygems"
require "sinatra" # <----- comment this line to make it work again
require "dm-core"
require "active_support"
require "pp"
class Link
include DataMapper::Resource
property :id, Serial
property :description, String, :length => 1024
property :url, String, :length => 1024
def to_s(with_title=true)
"<a href='#{url}'#{" title='#{description}'" if with_title}>#{description}</a>"
end
end
class Entry
include DataMapper::Resource
property :id, Serial
property :title, String, :length => 255, :index => true
property :class_name, Discriminator
end
class Person < Entry
has n, :links
property :name, String, :length => 255, :index => true
property :email, String, :length => 255, :index => true
end
DataMapper::Logger.new(STDOUT,:debug)
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/bula.db")
DataMapper.auto_migrate!
a = Person.create( :name => "ABC" , :email => "abc@def.com" )
a.links.create( :description => "D" , :url => "http://pic.jpg" )
a.links.create( :description => "E" , :url => "http://pic.jpg" )
a.links.create( :description => "F" , :url => "http://pic.jpg" )
a.links.create( :description => "G" , :url => "http://pic.jpg" )
a.save
pp a.links.size
params = {
"person" => {
"links" => {
"2" => {
"delete" => "Ta bort"
}
}
},
"_method" => "delete",
"id" => "1"
}
pp params
person = Person.get( params["id"] )
if links = params["person"].delete( "links" )
i = links.keys.first.to_i
l = Link.get( i )
person.links.delete( l )
end
person.save
pp person.links.size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment