sample_models 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +345 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/init.rb +2 -0
- data/install.rb +1 -0
- data/lib/sample_models/creation.rb +116 -0
- data/lib/sample_models/finder.rb +81 -0
- data/lib/sample_models/model.rb +136 -0
- data/lib/sample_models/sampler.rb +110 -0
- data/lib/sample_models.rb +117 -0
- data/sample_models.gemspec +89 -0
- data/spec/sample_models_spec.rb +11 -0
- data/spec_or_test/database.yml +7 -0
- data/spec_or_test/setup.rb +249 -0
- data/spec_or_test/specs_or_test_cases.rb +569 -0
- data/spec_or_test/vendor/validates_email_format_of/CHANGELOG +11 -0
- data/spec_or_test/vendor/validates_email_format_of/MIT-LICENSE +20 -0
- data/spec_or_test/vendor/validates_email_format_of/README +28 -0
- data/spec_or_test/vendor/validates_email_format_of/TODO +1 -0
- data/spec_or_test/vendor/validates_email_format_of/init.rb +1 -0
- data/spec_or_test/vendor/validates_email_format_of/lib/validates_email_format_of.rb +41 -0
- data/spec_or_test/vendor/validates_email_format_of/rakefile +28 -0
- data/spec_or_test/vendor/validates_email_format_of/test/database.yml +3 -0
- data/spec_or_test/vendor/validates_email_format_of/test/fixtures/people.yml +3 -0
- data/spec_or_test/vendor/validates_email_format_of/test/fixtures/person.rb +3 -0
- data/spec_or_test/vendor/validates_email_format_of/test/schema.rb +5 -0
- data/spec_or_test/vendor/validates_email_format_of/test/test_helper.rb +35 -0
- data/spec_or_test/vendor/validates_email_format_of/test/validates_email_format_of_test.rb +82 -0
- data/tasks/sample_models_tasks.rake +4 -0
- data/test/test_sample_models.rb +28 -0
- data/uninstall.rb +1 -0
- data/vendor/ar_query/MIT-LICENSE +20 -0
- data/vendor/ar_query/README +0 -0
- data/vendor/ar_query/ar_query.gemspec +16 -0
- data/vendor/ar_query/init.rb +1 -0
- data/vendor/ar_query/install.rb +1 -0
- data/vendor/ar_query/lib/ar_query.rb +146 -0
- data/vendor/ar_query/spec/ar_query_spec.rb +318 -0
- data/vendor/ar_query/tasks/ar_query_tasks.rake +0 -0
- data/vendor/ar_query/uninstall.rb +1 -0
- metadata +117 -0
@@ -0,0 +1,249 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_record'
|
3
|
+
RAILS_ENV = 'test'
|
4
|
+
require 'active_record/base'
|
5
|
+
require File.dirname(__FILE__) +
|
6
|
+
'/vendor/validates_email_format_of/lib/validates_email_format_of'
|
7
|
+
require File.dirname(__FILE__) + '/../lib/sample_models'
|
8
|
+
|
9
|
+
# Configure ActiveRecord
|
10
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
11
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
|
12
|
+
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'mysql'])
|
13
|
+
|
14
|
+
def initialize_db
|
15
|
+
silence_stream(STDOUT) do
|
16
|
+
ActiveRecord::Schema.define do
|
17
|
+
create_table 'appointments', :force => true do |appointment|
|
18
|
+
appointment.datetime 'start_time', 'end_time'
|
19
|
+
appointment.integer 'user_id', 'calendar_id', 'category_id'
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table 'blog_posts', :force => true do |blog_post|
|
23
|
+
blog_post.datetime 'published_at'
|
24
|
+
blog_post.integer 'merged_into_id', 'user_id'
|
25
|
+
blog_post.string 'title'
|
26
|
+
blog_post.float 'average_rating'
|
27
|
+
end
|
28
|
+
|
29
|
+
create_table "blog_post_tags", :force => true do |t|
|
30
|
+
t.integer "blog_post_id"
|
31
|
+
t.integer "tag_id"
|
32
|
+
end
|
33
|
+
|
34
|
+
create_table "bookmarks", :force => true do |t|
|
35
|
+
t.integer "bookmarkable_id"
|
36
|
+
t.string "bookmarkable_type"
|
37
|
+
end
|
38
|
+
|
39
|
+
create_table 'calendars', :force => true do |appointment|
|
40
|
+
appointment.integer 'user_id'
|
41
|
+
end
|
42
|
+
|
43
|
+
create_table 'categories', :force => true do |category|
|
44
|
+
category.string 'name'
|
45
|
+
category.integer 'parent_id'
|
46
|
+
end
|
47
|
+
|
48
|
+
create_table 'comments', :force => true do |comment|
|
49
|
+
comment.boolean 'flagged_as_spam', :default => false
|
50
|
+
end
|
51
|
+
|
52
|
+
create_table 'episodes', :force => true do |episode|
|
53
|
+
episode.integer 'show_id'
|
54
|
+
episode.string 'name'
|
55
|
+
episode.date 'original_air_date'
|
56
|
+
end
|
57
|
+
|
58
|
+
create_table 'networks', :force => true do |network|
|
59
|
+
network.string 'name'
|
60
|
+
end
|
61
|
+
|
62
|
+
create_table 'shows', :force => true do |show|
|
63
|
+
show.integer 'network_id', 'subscription_price'
|
64
|
+
show.string 'name'
|
65
|
+
end
|
66
|
+
|
67
|
+
create_table 'subscriptions', :force => true do |subscription|
|
68
|
+
subscription.integer 'subscribable_id', 'user_id'
|
69
|
+
subscription.string 'subscribable_type'
|
70
|
+
end
|
71
|
+
|
72
|
+
create_table "tags", :force => true do |t|
|
73
|
+
t.string "tag"
|
74
|
+
end
|
75
|
+
|
76
|
+
create_table 'users', :force => true do |user|
|
77
|
+
user.integer 'favorite_blog_post_id'
|
78
|
+
user.string 'email', 'gender', 'homepage', 'login', 'password'
|
79
|
+
end
|
80
|
+
|
81
|
+
create_table 'user_with_passwords', :force => true do |user|
|
82
|
+
end
|
83
|
+
|
84
|
+
create_table 'videos', :force => true do |video|
|
85
|
+
video.integer 'episode_id', 'show_id', 'network_id', 'view_count'
|
86
|
+
end
|
87
|
+
|
88
|
+
create_table 'video_favorites', :force => true do |video_favorite|
|
89
|
+
video_favorite.integer 'user_id', 'video_id'
|
90
|
+
end
|
91
|
+
|
92
|
+
create_table 'video_takedown_events', :force => true do |vte|
|
93
|
+
vte.integer 'video_id'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# ============================================================================
|
100
|
+
# Define ActiveRecord classes
|
101
|
+
class Appointment < ActiveRecord::Base
|
102
|
+
belongs_to :calendar
|
103
|
+
belongs_to :category
|
104
|
+
belongs_to :user
|
105
|
+
|
106
|
+
validates_presence_of :calendar_id, :user_id
|
107
|
+
validates_uniqueness_of :start_time
|
108
|
+
|
109
|
+
def validate
|
110
|
+
if calendar.user_id != user_id
|
111
|
+
errors.add "Appointment needs same user as the calendar"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
class BlogPost < ActiveRecord::Base
|
117
|
+
has_many :blog_post_tags
|
118
|
+
belongs_to :merged_into,
|
119
|
+
:class_name => 'BlogPost', :foreign_key => 'merged_into_id'
|
120
|
+
has_many :tags, :through => :blog_post_tags
|
121
|
+
belongs_to :user
|
122
|
+
|
123
|
+
validates_presence_of :title
|
124
|
+
validates_presence_of :user_id
|
125
|
+
end
|
126
|
+
|
127
|
+
class BlogPostTag < ActiveRecord::Base
|
128
|
+
belongs_to :blog_post
|
129
|
+
belongs_to :tag
|
130
|
+
end
|
131
|
+
|
132
|
+
class Bookmark < ActiveRecord::Base
|
133
|
+
belongs_to :bookmarkable, :polymorphic => true
|
134
|
+
end
|
135
|
+
|
136
|
+
class Calendar < ActiveRecord::Base
|
137
|
+
belongs_to :user
|
138
|
+
|
139
|
+
validates_presence_of :user_id
|
140
|
+
end
|
141
|
+
|
142
|
+
class Category < ActiveRecord::Base
|
143
|
+
belongs_to :parent, :class_name => 'Category'
|
144
|
+
end
|
145
|
+
|
146
|
+
class Comment < ActiveRecord::Base
|
147
|
+
end
|
148
|
+
|
149
|
+
class Episode < ActiveRecord::Base
|
150
|
+
belongs_to :show
|
151
|
+
|
152
|
+
validates_presence_of :show_id, :original_air_date
|
153
|
+
end
|
154
|
+
|
155
|
+
class Network < ActiveRecord::Base
|
156
|
+
end
|
157
|
+
|
158
|
+
class Show < ActiveRecord::Base
|
159
|
+
belongs_to :network
|
160
|
+
end
|
161
|
+
|
162
|
+
class Subscription < ActiveRecord::Base
|
163
|
+
belongs_to :subscribable, :polymorphic => true
|
164
|
+
belongs_to :user
|
165
|
+
end
|
166
|
+
|
167
|
+
class Tag < ActiveRecord::Base
|
168
|
+
validates_uniqueness_of :tag
|
169
|
+
|
170
|
+
has_many :blog_post_tags
|
171
|
+
has_many :blog_posts, :through => :blog_post_tags
|
172
|
+
end
|
173
|
+
|
174
|
+
class User < ActiveRecord::Base
|
175
|
+
belongs_to :favorite_blog_post,
|
176
|
+
:class_name => 'BlogPost', :foreign_key => 'favorite_blog_post_id'
|
177
|
+
|
178
|
+
validates_email_format_of :email
|
179
|
+
validates_inclusion_of :gender, :in => %w(f m)
|
180
|
+
validates_uniqueness_of :email, :login, :case_sensitive => false
|
181
|
+
end
|
182
|
+
|
183
|
+
class UserWithPassword < ActiveRecord::Base
|
184
|
+
attr_accessor :password
|
185
|
+
|
186
|
+
validates_presence_of :password
|
187
|
+
end
|
188
|
+
|
189
|
+
class Video < ActiveRecord::Base
|
190
|
+
belongs_to :show
|
191
|
+
belongs_to :network
|
192
|
+
belongs_to :episode
|
193
|
+
|
194
|
+
def validate
|
195
|
+
if episode && episode.show_id != show_id
|
196
|
+
errors.add "Video needs same show as the episode; show_id is #{show_id.inspect} while episode.show_id is #{episode.show_id.inspect}"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
class VideoFavorite < ActiveRecord::Base
|
202
|
+
belongs_to :video
|
203
|
+
belongs_to :user
|
204
|
+
|
205
|
+
validates_presence_of :user_id, :video_id
|
206
|
+
validates_uniqueness_of :video_id, :scope => :user_id
|
207
|
+
end
|
208
|
+
|
209
|
+
class VideoTakedownEvent < ActiveRecord::Base
|
210
|
+
belongs_to :video
|
211
|
+
|
212
|
+
validates_presence_of :video_id
|
213
|
+
validates_uniqueness_of :video_id
|
214
|
+
end
|
215
|
+
|
216
|
+
# ============================================================================
|
217
|
+
# sample_models configuration
|
218
|
+
SampleModels.configure Appointment do |appointment|
|
219
|
+
appointment.before_save do |a|
|
220
|
+
a.user_id = a.calendar.user_id
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
SampleModels.configure BlogPost do |bp|
|
225
|
+
bp.published_at.force_unique
|
226
|
+
|
227
|
+
bp.funny_sample :title => 'Funny haha', :average_rating => 3.0
|
228
|
+
end
|
229
|
+
|
230
|
+
SampleModels.configure Category do |category|
|
231
|
+
category.parent.default nil
|
232
|
+
end
|
233
|
+
|
234
|
+
SampleModels.configure Subscription do |sub|
|
235
|
+
sub.subscribable.default_class BlogPost
|
236
|
+
end
|
237
|
+
|
238
|
+
SampleModels.configure Video do |video|
|
239
|
+
video.before_save do |v, sample_attrs|
|
240
|
+
if v.episode && v.episode.show != v.show
|
241
|
+
if sample_attrs[:show]
|
242
|
+
v.episode.show = v.show
|
243
|
+
else
|
244
|
+
v.show = v.episode.show
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
video.view_count.default 0
|
249
|
+
end
|