-
Notifications
You must be signed in to change notification settings - Fork 37
/
models.rb
104 lines (82 loc) · 2.42 KB
/
models.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Person < ActiveRecord::Base #:nodoc:
validates_uniqueness_of :name
has_many :androids, :foreign_key => :owner_id, :dependent => :destroy
end
class Android < ActiveRecord::Base #:nodoc:
validates_uniqueness_of :name
has_many :components, :dependent => :destroy
has_one :sticker
has_many :memories, :foreign_key => 'parent_id'
is_paranoid
# this code is to ensure that our destroy and restore methods
# work without triggering before/after_update callbacks
before_update :raise_hell
def raise_hell
raise "hell"
end
end
class Component < ActiveRecord::Base #:nodoc:
is_paranoid
belongs_to :android, :dependent => :destroy
has_many :sub_components, :dependent => :destroy
NEW_NAME = 'Something Else!'
after_destroy :change_name
def change_name
self.update_attribute(:name, NEW_NAME)
end
end
class SubComponent < ActiveRecord::Base #:nodoc:
is_paranoid
belongs_to :component, :dependent => :destroy
end
class Memory < ActiveRecord::Base #:nodoc:
is_paranoid
belongs_to :android, :class_name => "Android", :foreign_key => "parent_id"
end
class Sticker < ActiveRecord::Base #:nodoc
MM_NAME = "You've got method_missing"
# this simply serves to ensure that we don't break method_missing
# if it is implemented on a class and called before is_paranoid
def method_missing name, *args, &block
self.name = MM_NAME
end
is_paranoid
belongs_to :android
end
class AndroidWithScopedUniqueness < ActiveRecord::Base #:nodoc:
set_table_name :androids
validates_uniqueness_of :name, :scope => :deleted_at
is_paranoid
end
class Ninja < ActiveRecord::Base #:nodoc:
validates_uniqueness_of :name, :scope => :visible
is_paranoid :field => [:visible, false, true]
alias_method :vanish, :destroy
end
class Pirate < ActiveRecord::Base #:nodoc:
is_paranoid :field => [:alive, false, true]
end
class DeadPirate < ActiveRecord::Base #:nodoc:
set_table_name :pirates
is_paranoid :field => [:alive, true, false]
end
class RandomPirate < ActiveRecord::Base #:nodoc:
set_table_name :pirates
def after_destroy
raise 'after_destroy works'
end
end
class UndestroyablePirate < ActiveRecord::Base #:nodoc:
set_table_name :pirates
is_paranoid :field => [:alive, false, true]
def before_destroy
false
end
end
class Uuid < ActiveRecord::Base #:nodoc:
set_primary_key "uuid"
def before_create
self.uuid = "295b3430-85b8-012c-cfe4-002332cf7d5e"
end
is_paranoid
end