-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
stub.rb
73 lines (58 loc) · 1.83 KB
/
stub.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
66
67
68
69
70
71
72
73
module FactoryGirl
module Strategy
class Stub
@@next_id = 1000
def association(runner)
runner.run(:build_stubbed)
end
def result(evaluation)
evaluation.object.tap do |instance|
stub_database_interaction_on_result(instance)
evaluation.notify(:after_stub, instance)
end
end
private
def next_id
@@next_id += 1
end
def stub_database_interaction_on_result(result_instance)
result_instance.id ||= next_id
result_instance.instance_eval do
def persisted?
!new_record?
end
def new_record?
id.nil?
end
def save(*args)
raise 'stubbed models are not allowed to access the database'
end
def destroy(*args)
raise 'stubbed models are not allowed to access the database'
end
def connection
raise 'stubbed models are not allowed to access the database'
end
def reload
raise 'stubbed models are not allowed to access the database'
end
def update_attribute(*args)
raise 'stubbed models are not allowed to access the database'
end
def update_column(*args)
raise 'stubbed models are not allowed to access the database'
end
end
created_at_missing_default = result_instance.respond_to?(:created_at) && !result_instance.created_at
result_instance_missing_created_at = !result_instance.respond_to?(:created_at)
if created_at_missing_default || result_instance_missing_created_at
result_instance.instance_eval do
def created_at
@created_at ||= Time.now.in_time_zone
end
end
end
end
end
end
end