forked from ryan-allen/workflow
/
activerecord_integration_spike.rb
65 lines (48 loc) · 1.39 KB
/
activerecord_integration_spike.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
%w(rubygems activerecord).each { |lib| require lib }
class Item < ActiveRecord::Base
alias_method :initialize_before_integration, :initialize
attr_accessor :workflow
def initialize(attributes = nil)
initialize_before_integration(attributes)
@workflow = :start # initialize
end
def after_find
@workflow = state.to_sym # reconsitute
end
def before_save
self.saves += 1
end
alias_method :before_save_before_integration, :before_save
def before_save
before_save_before_integration()
self.state = @workflow.to_s
end
end
require 'test/unit'
class TestTest < Test::Unit::TestCase
def test_default_values_are_what_we_expect
@item = Item.new
assert_nil @item.state
assert_equal :start, @item.workflow
assert_equal 0, @item.saves
end
def test_it_increments_saves_on_each_save
@item = Item.new
3.times { @item.save }
assert_equal 3, @item.saves
end
def test_it_serializes_workflow_as_string_to_state_on_save
@item = Item.new
@item.workflow = :a_workflow
@item.save
assert_equal 'a_workflow', @item.state
end
def test_it_reconsitutes_workflow_from_state_field
@item = Item.new
@item.workflow = :lol_ok
@item.save
@item.connection.execute("update items set state = 'err_ok' where id = #{@item.id}")
@item = Item.find(@item.id)
assert_equal :err_ok, @item.workflow
end
end