· 8 years ago · Feb 21, 2018, 11:51 PM
1#!/usr/bin/env ruby
2#
3# A one file test to show ...
4require 'rubygems'
5
6gem('dm-core', '~> 0.9.8')
7require 'dm-core'
8require 'dm-validations'
9
10# setup the logger
11DataMapper::Logger.new(STDOUT, :debug)
12
13# connect to the DB
14DataMapper.setup(:default, 'sqlite3::memory:')
15
16require 'RedCloth'
17
18
19class Todo
20 include DataMapper::Resource
21
22 before 'valid?'.to_sym, :render_html
23# before :update, :render_html
24
25 property :id, Serial
26 property :body, Text, :lazy => false
27 property :body_html, Text, :lazy => false
28
29 private
30 def render_html
31 @body_html = RedCloth.new(body).to_html if body
32 # if you dont have RedCloth installed just do any arbitrary thing here.. the point of the bug is not updating the body_html field.
33 p "Yes! we're in the render_html method and the @body = #{@body}, @body_html = #{@body_html}"
34 end
35end
36
37
38DataMapper.auto_migrate!
39
40t = Todo.new
41t.body = "*y*"
42t.save
43
44t.body = "*yyyy*"
45t.save
46
47# executing this script i get:
48# > ruby todo.rb
49# Mon, 22 Dec 2008 12:10:51 GMT ~ debug ~ DROP TABLE IF EXISTS "todos"
50# Mon, 22 Dec 2008 12:10:51 GMT ~ debug ~ PRAGMA table_info('todos')
51# Mon, 22 Dec 2008 12:10:51 GMT ~ debug ~ SELECT sqlite_version(*)
52# Mon, 22 Dec 2008 12:10:51 GMT ~ debug ~ CREATE TABLE "todos" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "body" TEXT, "body_html" TEXT)
53# "Yes! we're in the render_html method and the @body = *y*, @body_html = <p><strong>y</strong></p>"
54# Mon, 22 Dec 2008 12:10:51 GMT ~ debug ~ INSERT INTO "todos" ("body") VALUES ('*y*')
55# "Yes! we're in the render_html method and the @body = *yyyy*, @body_html = <p><strong>yyyy</strong></p>"
56# Mon, 22 Dec 2008 12:10:51 GMT ~ debug ~ UPDATE "todos" SET "body" = '*yyyy*' WHERE ("id" = 1)
57
58
59
60
61# as a model in my merb setup i did this to reveal the bug
62# NOTE: in this case the first time i call save IT DOES WORK, only subsequential attempts reveal the bug... interesting.
63
64# irb(main):001:0> t = Todo.new
65# => #<Todo id=nil body=nil body_html=nil>
66# irb(main):002:0> t.body = "*y*"
67# => "*y*"
68# irb(main):003:0> t.save
69# "Yes! we're in the render_html method and the @body = *y*, @body_html = <p><strong>y</strong></p>"
70# ~ INSERT INTO "todos" ("body", "body_html") VALUES ('*y*', '<p><strong>y</strong></p>')
71# => true
72# irb(main):004:0> t.body = "*yyyy*"
73# => "*yyyy*"
74# irb(main):005:0> t.save
75# "Yes! we're in the render_html method and the @body = *yyyy*, @body_html = <p><strong>yyyy</strong></p>"
76# ~ UPDATE "todos" SET "body" = '*yyyy*' WHERE ("id" = 5)
77# => true