sample_models 1.2.6 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,288 +0,0 @@
1
- RAILS_ENV = 'test'
2
- require 'rubygems'
3
- require 'bundler'
4
- begin
5
- Bundler.setup(:default, :test)
6
- rescue Bundler::BundlerError => e
7
- $stderr.puts e.message
8
- $stderr.puts "Run `bundle install` to install missing gems"
9
- exit e.status_code
10
- end
11
- require 'test/unit'
12
-
13
- require 'active_record'
14
- require 'active_record/base'
15
- require 'active_support/core_ext/logger'
16
- require 'active_support/core_ext/module/attribute_accessors'
17
- require 'validates_email_format_of'
18
-
19
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
20
- $LOAD_PATH.unshift(File.dirname(__FILE__))
21
- require 'sample_models'
22
-
23
- # Configure ActiveRecord
24
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
25
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
26
- ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'mysql'])
27
-
28
- def initialize_db
29
- silence_stream(STDOUT) do
30
- ActiveRecord::Schema.define do
31
- create_table 'appointments', :force => true do |appointment|
32
- appointment.datetime 'start_time', 'end_time'
33
- appointment.integer 'user_id', 'calendar_id', 'category_id'
34
- end
35
-
36
- create_table 'blog_posts', :force => true do |blog_post|
37
- blog_post.datetime 'published_at'
38
- blog_post.integer 'merged_into_id', 'user_id'
39
- blog_post.string 'title'
40
- blog_post.float 'average_rating'
41
- end
42
-
43
- create_table "blog_post_tags", :force => true do |t|
44
- t.integer "blog_post_id"
45
- t.integer "tag_id"
46
- end
47
-
48
- create_table "bookmarks", :force => true do |t|
49
- t.integer "bookmarkable_id"
50
- t.string "bookmarkable_type"
51
- end
52
-
53
- create_table 'calendars', :force => true do |appointment|
54
- appointment.integer 'user_id'
55
- end
56
-
57
- create_table 'categories', :force => true do |category|
58
- category.string 'name'
59
- category.integer 'parent_id'
60
- end
61
-
62
- create_table 'comments', :force => true do |comment|
63
- comment.boolean 'flagged_as_spam', :default => false
64
- end
65
-
66
- create_table 'episodes', :force => true do |episode|
67
- episode.integer 'show_id'
68
- episode.string 'name'
69
- episode.date 'original_air_date'
70
- end
71
-
72
- create_table 'external_users', :force => true do |external_user|
73
- end
74
-
75
- create_table 'networks', :force => true do |network|
76
- network.string 'name'
77
- end
78
-
79
- create_table 'shows', :force => true do |show|
80
- show.integer 'network_id', 'subscription_price'
81
- show.string 'name'
82
- end
83
-
84
- create_table 'subscriptions', :force => true do |subscription|
85
- subscription.integer 'subscribable_id', 'user_id'
86
- subscription.string 'subscribable_type'
87
- end
88
-
89
- create_table "tags", :force => true do |t|
90
- t.string "tag"
91
- end
92
-
93
- create_table 'users', :force => true do |user|
94
- user.integer 'favorite_blog_post_id', 'external_user_id'
95
- user.string 'email', 'gender', 'homepage', 'login', 'password'
96
- end
97
-
98
- create_table 'user2s', :force => true do |user2|
99
- user2.string 'login'
100
- end
101
-
102
- create_table 'user_with_passwords', :force => true do |user|
103
- end
104
-
105
- create_table 'videos', :force => true do |video|
106
- video.integer 'episode_id', 'show_id', 'network_id', 'view_count'
107
- end
108
-
109
- create_table 'video_favorites', :force => true do |video_favorite|
110
- video_favorite.integer 'user_id', 'video_id'
111
- end
112
-
113
- create_table 'video_takedown_events', :force => true do |vte|
114
- vte.integer 'video_id'
115
- end
116
- end
117
- end
118
- end
119
-
120
- # ============================================================================
121
- # Define ActiveRecord classes
122
- class Appointment < ActiveRecord::Base
123
- belongs_to :calendar
124
- belongs_to :category
125
- belongs_to :user
126
-
127
- validates_presence_of :calendar_id, :user_id
128
- validates_uniqueness_of :start_time
129
- validate :validate_calendar_has_same_user_id
130
-
131
- def validate_calendar_has_same_user_id
132
- if calendar.user_id != user_id
133
- errors.add "Appointment needs same user as the calendar"
134
- end
135
- end
136
- end
137
-
138
- class BlogPost < ActiveRecord::Base
139
- has_many :blog_post_tags
140
- belongs_to :merged_into,
141
- :class_name => 'BlogPost', :foreign_key => 'merged_into_id'
142
- has_many :tags, :through => :blog_post_tags
143
- belongs_to :user
144
-
145
- validates_presence_of :title
146
- validates_presence_of :user_id
147
- end
148
-
149
- class BlogPostTag < ActiveRecord::Base
150
- belongs_to :blog_post
151
- belongs_to :tag
152
- end
153
-
154
- class Bookmark < ActiveRecord::Base
155
- belongs_to :bookmarkable, :polymorphic => true
156
- end
157
-
158
- class Calendar < ActiveRecord::Base
159
- belongs_to :user
160
-
161
- validates_presence_of :user_id
162
- end
163
-
164
- class Category < ActiveRecord::Base
165
- belongs_to :parent, :class_name => 'Category'
166
- end
167
-
168
- class Comment < ActiveRecord::Base
169
- end
170
-
171
- class Episode < ActiveRecord::Base
172
- belongs_to :show
173
-
174
- validates_presence_of :show_id, :original_air_date
175
- end
176
-
177
- class ExternalUser < ActiveRecord::Base
178
- end
179
-
180
- class Network < ActiveRecord::Base
181
- end
182
-
183
- class Show < ActiveRecord::Base
184
- belongs_to :network
185
- end
186
-
187
- class Subscription < ActiveRecord::Base
188
- belongs_to :subscribable, :polymorphic => true
189
- belongs_to :user
190
- end
191
-
192
- class Tag < ActiveRecord::Base
193
- validates_uniqueness_of :tag
194
-
195
- has_many :blog_post_tags
196
- has_many :blog_posts, :through => :blog_post_tags
197
- end
198
-
199
- class User < ActiveRecord::Base
200
- belongs_to :favorite_blog_post,
201
- :class_name => 'BlogPost', :foreign_key => 'favorite_blog_post_id'
202
- belongs_to :external_user
203
-
204
- validates_email_format_of :email
205
- validates_inclusion_of :gender, :in => %w(f m)
206
- validates_uniqueness_of :email, :login, :case_sensitive => false
207
- validates_uniqueness_of :external_user_id, :allow_nil => true
208
- end
209
-
210
- class User2 < ActiveRecord::Base
211
- validates_presence_of :login
212
- validates_uniqueness_of :login
213
- end
214
-
215
- class UserWithPassword < ActiveRecord::Base
216
- attr_accessor :password
217
-
218
- validates_presence_of :password
219
- end
220
-
221
- class Video < ActiveRecord::Base
222
- belongs_to :show
223
- belongs_to :network
224
- belongs_to :episode
225
-
226
- validate :validate_episode_has_same_show_id
227
-
228
- def validate_episode_has_same_show_id
229
- if episode && episode.show_id != show_id
230
- errors.add "Video needs same show as the episode; show_id is #{show_id.inspect} while episode.show_id is #{episode.show_id.inspect}"
231
- end
232
- end
233
- end
234
-
235
- class VideoFavorite < ActiveRecord::Base
236
- belongs_to :video
237
- belongs_to :user
238
-
239
- validates_presence_of :user_id, :video_id
240
- validates_uniqueness_of :video_id, :scope => :user_id
241
- end
242
-
243
- class VideoTakedownEvent < ActiveRecord::Base
244
- belongs_to :video
245
-
246
- validates_presence_of :video_id
247
- validates_uniqueness_of :video_id
248
- end
249
-
250
- # ============================================================================
251
- # sample_models configuration
252
- SampleModels.configure Appointment do |appointment|
253
- appointment.before_save do |a|
254
- a.user_id = a.calendar.user_id
255
- end
256
- end
257
-
258
- SampleModels.configure BlogPost do |bp|
259
- bp.published_at.force_unique
260
-
261
- bp.funny_sample :title => 'Funny haha', :average_rating => 3.0
262
- end
263
-
264
- SampleModels.configure Category do |category|
265
- category.parent.default nil
266
- end
267
-
268
- SampleModels.configure Subscription do |sub|
269
- sub.subscribable.default_class BlogPost
270
- end
271
-
272
- SampleModels.configure User do |user|
273
- user.external_user_id.default nil
274
- end
275
-
276
- SampleModels.configure Video do |video|
277
- video.before_save do |v, sample_attrs|
278
- if v.episode && v.episode.show != v.show
279
- if sample_attrs[:show]
280
- v.episode.show = v.show
281
- else
282
- v.show = v.episode.show
283
- end
284
- end
285
- end
286
- video.view_count.default 0
287
- end
288
-