forked from amoeba-rb/amoeba
/
models.rb
211 lines (172 loc) · 3.86 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class Topic < ActiveRecord::Base
has_many :posts
end
class User < ActiveRecord::Base
has_many :posts
end
class Author < ActiveRecord::Base
has_many :posts, :inverse_of => :author
amoeba do
enable
end
end
class Post < ActiveRecord::Base
belongs_to :topic
belongs_to :owner, :class_name => 'User'
belongs_to :author, :inverse_of => :posts
has_one :post_config
has_one :account
has_one :history, :through => :account
has_many :comments
has_many :supercats
has_many :categories, :through => :supercats
has_many :post_widgets
has_many :widgets, :through => :post_widgets
has_many :custom_things
has_and_belongs_to_many :tags
has_and_belongs_to_many :notes
validates_presence_of :topic
validates_presence_of :author
amoeba do
enable
clone [:widgets, :notes]
prepend :title => "Copy of "
append :contents => " (copied version)"
regex :contents => {:replace => /dog/, :with => 'cat'}
customize([
lambda do |orig_obj,copy_of_obj|
orig_obj.comments.each do |oc|
if oc.nerf == "ratatat"
hash = oc.attributes
hash[:id] = nil
hash[:post_id] = nil
hash[:contents] = nil
cc = Comment.new(hash)
copy_of_obj.comments << cc
end
end
end,
lambda do |orig_obj,copy_of_obj|
orig_obj.comments.each do |oc|
if oc.nerf == "bonk"
hash = oc.attributes
hash[:id] = nil
hash[:post_id] = nil
hash[:contents] = nil
hash[:nerf] = "bonkers"
cc = Comment.new(hash)
copy_of_obj.comments << cc
end
end
end
])
end
end
class CustomThing < ActiveRecord::Base
belongs_to :post
class ArrayPack
def load(str)
str.split(',')
end
def dump(int_array)
int_array.join(',')
end
end
serialize :value, ArrayPack.new
end
class Account < ActiveRecord::Base
belongs_to :post
has_one :history
amoeba do
enable
end
end
class History < ActiveRecord::Base
belongs_to :account
end
class Category < ActiveRecord::Base
has_many :supercats
has_many :posts, :through => :supercats
amoeba do
enable
prepend :ramblings => "Copy of "
set :other_ramblings => "La la la"
end
end
class Supercat < ActiveRecord::Base
belongs_to :post
belongs_to :category
has_many :superkittens
amoeba do
include_field :superkittens
prepend :ramblings => "Copy of "
set :other_ramblings => "La la la"
end
end
class Superkitten < ActiveRecord::Base
belongs_to :supercat
end
class PostConfig < ActiveRecord::Base
belongs_to :post
end
class Comment < ActiveRecord::Base
belongs_to :post
has_many :ratings
has_many :reviews
amoeba do
exclude_field :reviews
end
end
class Review < ActiveRecord::Base
belongs_to :comment
end
class Rating < ActiveRecord::Base
belongs_to :comment
end
class Widget < ActiveRecord::Base
has_many :post_widgets
has_many :posts, :through => :post_widgets
end
class PostWidget < ActiveRecord::Base
belongs_to :post
belongs_to :widget
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :posts
end
class Note < ActiveRecord::Base
has_and_belongs_to_many :posts
end
# Inheritance
class Product < ActiveRecord::Base
has_many :images
has_and_belongs_to_many :sections
amoeba do
enable
propagate
end
end
class Section < ActiveRecord::Base
end
class Image < ActiveRecord::Base
end
class Shirt < Product
amoeba do
raised :submissive
end
end
class Necklace < Product
amoeba do
raised :relaxed
end
end
# Polymorphism
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
class Employee < ActiveRecord::Base
has_many :addresses, :as => :addressable
end
class Customer < ActiveRecord::Base
has_many :addresses, :as => :addressable
end