learn_rails 0.0.0 → 0.0.1

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.
@@ -0,0 +1,575 @@
1
+ require 'spec_helper'
2
+
3
+ describe LearnRails::Associations do
4
+ context "belongs_to association" do
5
+ context "without options" do
6
+ it "should return the correct code" do
7
+ [ %w(Task belongs_to :user),
8
+ %w(Task belongs_to "user")
9
+ ].each do |association|
10
+ LearnRails::Associations.code_for(association).should eql belongs_to_code
11
+ end
12
+ end
13
+ end
14
+
15
+ context "with class_name option" do
16
+ it "should return the correct code" do
17
+ [ %w(Task belongs_to :user, :class_name => :person),
18
+ %w(Task belongs_to :user, :class_name => "person"),
19
+ %w(Task belongs_to :user, class_name: :person),
20
+ %w(Task belongs_to :user, class_name: "person")
21
+ ].each do |association|
22
+ LearnRails::Associations.code_for(association).should eql belongs_to_with_class_name_code
23
+ end
24
+ end
25
+ end
26
+
27
+ context "with foreign_key option" do
28
+ it "should return the correct code" do
29
+ [ %w(Task belongs_to :user, :foreign_key => :person_id),
30
+ %w(Task belongs_to :user, :foreign_key => "person_id"),
31
+ %w(Task belongs_to :user, foreign_key: :person_id),
32
+ %w(Task belongs_to :user, foreign_key: "person_id")
33
+ ].each do |association|
34
+ LearnRails::Associations.code_for(association).should eql belongs_to_with_foreign_key_code
35
+ end
36
+ end
37
+ end
38
+
39
+ context "with primary_key option" do
40
+ it "should return the correct code" do
41
+ [ %w(Task belongs_to :user, :primary_key => :todo_id),
42
+ %w(Task belongs_to :user, :primary_key => "todo_id"),
43
+ %w(Task belongs_to :user, primary_key: :todo_id),
44
+ %w(Task belongs_to :user, primary_key: "todo_id")
45
+ ].each do |association|
46
+ LearnRails::Associations.code_for(association).should eql belongs_to_with_primary_key_code
47
+ end
48
+ end
49
+ end
50
+
51
+ context "with readonly option" do
52
+ it "should return the correct code" do
53
+ [ %w(Task belongs_to :user, :readonly => true),
54
+ %w(Task belongs_to :user, :readonly => "true"),
55
+ %w(Task belongs_to :user, readonly: true),
56
+ %w(Task belongs_to :user, readonly: "true")
57
+ ].each do |association|
58
+ LearnRails::Associations.code_for(association).should eql belongs_to_with_readonly_code
59
+ end
60
+ end
61
+ end
62
+
63
+ context "with conditions options" do
64
+ context "one option" do
65
+ it "should return the correct code" do
66
+ [ %w(Task belongs_to :user, :conditions => { :status => "active" }),
67
+ %w(Task belongs_to :user, :conditions => {status: "active"}),
68
+ %w(Task belongs_to :user, :conditions => { status: "active"}),
69
+ %w(Task belongs_to :user, :conditions => {status: "active" }),
70
+ %w(Task belongs_to :user, conditions: { :status => "active" }),
71
+ %w(Task belongs_to :user, conditions: {status: "active"}),
72
+ %w(Task belongs_to :user, conditions: { status: "active"}),
73
+ %w(Task belongs_to :user, conditions: {status: "active" })
74
+ ].each do |association|
75
+ LearnRails::Associations.code_for(association).should eql belongs_to_with_one_condition_code
76
+ end
77
+ end
78
+ end
79
+
80
+ context "multiple options" do
81
+ it "should return the correct code" do
82
+ [ %w(Task belongs_to :user, :conditions => { status => "active", registered => true }),
83
+ %w(Task belongs_to :user, :conditions => { status: "active", registered: true}),
84
+ %w(Task belongs_to :user, conditions: { status => "active", registered => true }),
85
+ %w(Task belongs_to :user, conditions: { status: "active", registered: true})
86
+ ].each do |association|
87
+ LearnRails::Associations.code_for(association).should eql belongs_to_with_multiple_conditions_code
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ context "has_one association" do
95
+ context "without options" do
96
+ it "should return the correct code" do
97
+ [ %w(User has_one :task),
98
+ %w(User has_one "task")
99
+ ].each do |association|
100
+ LearnRails::Associations.code_for(association).should eql has_one_code
101
+ end
102
+ end
103
+ end
104
+
105
+ context "with class_name option" do
106
+ it "should return the correct code" do
107
+ [ %w(User has_one :task, :class_name => :to_do),
108
+ %w(User has_one :task, :class_name => "ToDo"),
109
+ %w(User has_one :task, class_name: :to_do),
110
+ %w(User has_one :task, class_name: "ToDo")
111
+ ].each do |association|
112
+ LearnRails::Associations.code_for(association).should eql has_one_with_class_name_code
113
+ end
114
+ end
115
+ end
116
+
117
+ context "with foreign_key option" do
118
+ it "should return the correct code" do
119
+ [ %w(User has_one :task, :foreign_key => :employee_id),
120
+ %w(User has_one :task, :foreign_key => "employee_id"),
121
+ %w(User has_one :task, foreign_key: :employee_id),
122
+ %w(User has_one :task, foreign_key: "employee_id")
123
+ ].each do |association|
124
+ LearnRails::Associations.code_for(association).should eql has_one_with_foreign_key_code
125
+ end
126
+ end
127
+ end
128
+
129
+ context "with primary_key option" do
130
+ it "should return the correct code" do
131
+ [ %w(User has_one :task, :primary_key => :primary_id),
132
+ %w(User has_one :task, :primary_key => "primary_id"),
133
+ %w(User has_one :task, primary_key: :primary_id),
134
+ %w(User has_one :task, primary_key: "primary_id")
135
+ ].each do |association|
136
+ LearnRails::Associations.code_for(association).should eql has_one_with_primary_key_code
137
+ end
138
+ end
139
+ end
140
+
141
+ context "with readonly option" do
142
+ it "should return the correct code" do
143
+ [ %w(User has_one :task, :readonly => true),
144
+ %w(User has_one :task, :readonly => "true"),
145
+ %w(User has_one :task, readonly: true),
146
+ %w(User has_one :task, readonly: "true")
147
+ ].each do |association|
148
+ LearnRails::Associations.code_for(association).should eql has_one_with_readonly_code
149
+ end
150
+ end
151
+ end
152
+
153
+ context "with conditions options" do
154
+ context "one option" do
155
+ it "should return the correct code" do
156
+ [ %w(User has_one :task, :conditions => { :status => "active" }),
157
+ %w(User has_one :task, :conditions => {status: "active"}),
158
+ %w(User has_one :task, :conditions => { status: "active"}),
159
+ %w(User has_one :task, :conditions => {status: "active" }),
160
+ %w(User has_one :task, conditions: { :status => "active" }),
161
+ %w(User has_one :task, conditions: {status: "active"}),
162
+ %w(User has_one :task, conditions: { status: "active"}),
163
+ %w(User has_one :task, conditions: {status: "active" })
164
+ ].each do |association|
165
+ LearnRails::Associations.code_for(association).should eql has_one_with_one_condition_code
166
+ end
167
+ end
168
+ end
169
+
170
+ context "multiple options" do
171
+ it "should return the correct code" do
172
+ [ %w(User has_one :task, :conditions => { status => "active", registered => true }),
173
+ %w(User has_one :task, :conditions => { status: "active", registered: true}),
174
+ %w(User has_one :task, conditions: { status => "active", registered => true }),
175
+ %w(User has_one :task, conditions: { status: "active", registered: true})
176
+ ].each do |association|
177
+ LearnRails::Associations.code_for(association).should eql has_one_with_multiple_conditions_code
178
+ end
179
+ end
180
+ end
181
+ end
182
+
183
+ end
184
+
185
+ context "has_many association" do
186
+ context "without options" do
187
+ it "should return the correct code" do
188
+ [ %w(User has_many :tasks),
189
+ %w(User has_many "tasks")
190
+ ].each do |association|
191
+ LearnRails::Associations.code_for(association).should eql has_many_code
192
+ end
193
+ end
194
+ end
195
+ end
196
+
197
+ private
198
+
199
+ def belongs_to_code
200
+ <<-code.gsub(/^\s+/, '')
201
+ # def user(force_reload = false)
202
+ # @user = nil if force_reload
203
+ # @user ||= User.find_by_id(self.user_id)
204
+ # end
205
+ #
206
+ # def user=(user)
207
+ # self.user_id = user.id
208
+ # end
209
+ #
210
+ # def build_user(attributes = {})
211
+ # self.user = User.new(attributes)
212
+ # end
213
+ #
214
+ # def create_user(attributes = {})
215
+ # self.user = User.create(attributes)
216
+ # end
217
+ #
218
+ # def create_user!(attributes = {})
219
+ # self.user = User.create!(attributes)
220
+ # end
221
+ code
222
+ end
223
+
224
+ def belongs_to_with_class_name_code
225
+ <<-code.gsub(/^\s+/, '')
226
+ # def user(force_reload = false)
227
+ # @user = nil if force_reload
228
+ # @user ||= Person.find_by_id(self.user_id)
229
+ # end
230
+ #
231
+ # def user=(user)
232
+ # self.user_id = user.id
233
+ # end
234
+ #
235
+ # def build_user(attributes = {})
236
+ # self.user = Person.new(attributes)
237
+ # end
238
+ #
239
+ # def create_user(attributes = {})
240
+ # self.user = Person.create(attributes)
241
+ # end
242
+ #
243
+ # def create_user!(attributes = {})
244
+ # self.user = Person.create!(attributes)
245
+ # end
246
+ code
247
+ end
248
+
249
+ def belongs_to_with_foreign_key_code
250
+ <<-code.gsub(/^\s+/, '')
251
+ # def user(force_reload = false)
252
+ # @user = nil if force_reload
253
+ # @user ||= User.find_by_id(self.person_id)
254
+ # end
255
+ #
256
+ # def user=(user)
257
+ # self.person_id = user.id
258
+ # end
259
+ #
260
+ # def build_user(attributes = {})
261
+ # self.user = User.new(attributes)
262
+ # end
263
+ #
264
+ # def create_user(attributes = {})
265
+ # self.user = User.create(attributes)
266
+ # end
267
+ #
268
+ # def create_user!(attributes = {})
269
+ # self.user = User.create!(attributes)
270
+ # end
271
+ code
272
+ end
273
+
274
+ def belongs_to_with_primary_key_code
275
+ <<-code.gsub(/^\s+/, '')
276
+ # def user(force_reload = false)
277
+ # @user = nil if force_reload
278
+ # @user ||= User.find_by_id(self.user_id)
279
+ # end
280
+ #
281
+ # def user=(user)
282
+ # self.user_id = user.todo_id
283
+ # end
284
+ #
285
+ # def build_user(attributes = {})
286
+ # self.user = User.new(attributes)
287
+ # end
288
+ #
289
+ # def create_user(attributes = {})
290
+ # self.user = User.create(attributes)
291
+ # end
292
+ #
293
+ # def create_user!(attributes = {})
294
+ # self.user = User.create!(attributes)
295
+ # end
296
+ code
297
+ end
298
+
299
+ def belongs_to_with_readonly_code
300
+ <<-code.gsub(/^\s+/, '')
301
+ # def user(force_reload = false)
302
+ # @user = nil if force_reload
303
+ # @user ||= User.find_by_id(self.user_id)
304
+ # end
305
+ #
306
+ # def build_user(attributes = {})
307
+ # self.user = User.new(attributes)
308
+ # end
309
+ #
310
+ # def create_user(attributes = {})
311
+ # self.user = User.create(attributes)
312
+ # end
313
+ #
314
+ # def create_user!(attributes = {})
315
+ # self.user = User.create!(attributes)
316
+ # end
317
+ code
318
+ end
319
+
320
+ def belongs_to_with_one_condition_code
321
+ <<-code.gsub(/^\s+/, '')
322
+ # def user(force_reload = false)
323
+ # @user = nil if force_reload
324
+ # @user ||= User.first(:conditions => {:id => self.user_id, :status => "active"})
325
+ # end
326
+ #
327
+ # def user=(user)
328
+ # self.user_id = user.id
329
+ # end
330
+ #
331
+ # def build_user(attributes = {})
332
+ # self.user = User.new(attributes)
333
+ # end
334
+ #
335
+ # def create_user(attributes = {})
336
+ # self.user = User.create(attributes)
337
+ # end
338
+ #
339
+ # def create_user!(attributes = {})
340
+ # self.user = User.create!(attributes)
341
+ # end
342
+ code
343
+ end
344
+
345
+ def belongs_to_with_multiple_conditions_code
346
+ <<-code.gsub(/^\s+/, '')
347
+ # def user(force_reload = false)
348
+ # @user = nil if force_reload
349
+ # @user ||= User.first(:conditions => {:id => self.user_id, :status => "active", :registered => "true"})
350
+ # end
351
+ #
352
+ # def user=(user)
353
+ # self.user_id = user.id
354
+ # end
355
+ #
356
+ # def build_user(attributes = {})
357
+ # self.user = User.new(attributes)
358
+ # end
359
+ #
360
+ # def create_user(attributes = {})
361
+ # self.user = User.create(attributes)
362
+ # end
363
+ #
364
+ # def create_user!(attributes = {})
365
+ # self.user = User.create!(attributes)
366
+ # end
367
+ code
368
+ end
369
+
370
+ def has_one_code
371
+ <<-code.gsub(/^\s+/, '')
372
+ # def task(force_reload = false)
373
+ # @task = nil if force_reload
374
+ # @task ||= Task.find_by_user_id(self.id)
375
+ # end
376
+ #
377
+ # def task=(task)
378
+ # task.user_id = self.id
379
+ # task.save
380
+ # end
381
+ #
382
+ # def build_task(attributes = {})
383
+ # attributes[:user_id] = self.id
384
+ # Task.new(attributes)
385
+ # end
386
+ #
387
+ # def create_task(attributes = {})
388
+ # attributes[:user_id] = self.id
389
+ # Task.create(attributes)
390
+ # end
391
+ #
392
+ # def create_task!(attributes = {})
393
+ # attributes[:user_id] = self.id
394
+ # Task.create!(attributes)
395
+ # end
396
+ code
397
+ end
398
+
399
+ def has_one_with_class_name_code
400
+ <<-code.gsub(/^\s+/, '')
401
+ # def task(force_reload = false)
402
+ # @task = nil if force_reload
403
+ # @task ||= ToDo.find_by_user_id(self.id)
404
+ # end
405
+ #
406
+ # def task=(task)
407
+ # task.user_id = self.id
408
+ # task.save
409
+ # end
410
+ #
411
+ # def build_task(attributes = {})
412
+ # attributes[:user_id] = self.id
413
+ # ToDo.new(attributes)
414
+ # end
415
+ #
416
+ # def create_task(attributes = {})
417
+ # attributes[:user_id] = self.id
418
+ # ToDo.create(attributes)
419
+ # end
420
+ #
421
+ # def create_task!(attributes = {})
422
+ # attributes[:user_id] = self.id
423
+ # ToDo.create!(attributes)
424
+ # end
425
+ code
426
+ end
427
+
428
+ def has_one_with_foreign_key_code
429
+ <<-code.gsub(/^\s+/, '')
430
+ # def task(force_reload = false)
431
+ # @task = nil if force_reload
432
+ # @task ||= Task.find_by_employee_id(self.id)
433
+ # end
434
+ #
435
+ # def task=(task)
436
+ # task.employee_id = self.id
437
+ # task.save
438
+ # end
439
+ #
440
+ # def build_task(attributes = {})
441
+ # attributes[:employee_id] = self.id
442
+ # Task.new(attributes)
443
+ # end
444
+ #
445
+ # def create_task(attributes = {})
446
+ # attributes[:employee_id] = self.id
447
+ # Task.create(attributes)
448
+ # end
449
+ #
450
+ # def create_task!(attributes = {})
451
+ # attributes[:employee_id] = self.id
452
+ # Task.create!(attributes)
453
+ # end
454
+ code
455
+ end
456
+
457
+ def has_one_with_primary_key_code
458
+ <<-code.gsub(/^\s+/, '')
459
+ # def task(force_reload = false)
460
+ # @task = nil if force_reload
461
+ # @task ||= Task.find_by_user_id(self.primary_id)
462
+ # end
463
+ #
464
+ # def task=(task)
465
+ # task.user_id = self.primary_id
466
+ # task.save
467
+ # end
468
+ #
469
+ # def build_task(attributes = {})
470
+ # attributes[:user_id] = self.primary_id
471
+ # Task.new(attributes)
472
+ # end
473
+ #
474
+ # def create_task(attributes = {})
475
+ # attributes[:user_id] = self.primary_id
476
+ # Task.create(attributes)
477
+ # end
478
+ #
479
+ # def create_task!(attributes = {})
480
+ # attributes[:user_id] = self.primary_id
481
+ # Task.create!(attributes)
482
+ # end
483
+ code
484
+ end
485
+
486
+ def has_one_with_readonly_code
487
+ <<-code.gsub(/^\s+/, '')
488
+ # def task(force_reload = false)
489
+ # @task = nil if force_reload
490
+ # @task ||= Task.find_by_user_id(self.id)
491
+ # end
492
+ #
493
+ # def build_task(attributes = {})
494
+ # attributes[:user_id] = self.id
495
+ # Task.new(attributes)
496
+ # end
497
+ #
498
+ # def create_task(attributes = {})
499
+ # attributes[:user_id] = self.id
500
+ # Task.create(attributes)
501
+ # end
502
+ #
503
+ # def create_task!(attributes = {})
504
+ # attributes[:user_id] = self.id
505
+ # Task.create!(attributes)
506
+ # end
507
+ code
508
+ end
509
+
510
+ def has_one_with_one_condition_code
511
+ <<-code.gsub(/^\s+/, '')
512
+ # def task(force_reload = false)
513
+ # @task = nil if force_reload
514
+ # @task ||= Task.first(:conditions => {:user_id => self.id, :status => "active"})
515
+ # end
516
+ #
517
+ # def task=(task)
518
+ # task.user_id = self.id
519
+ # task.save
520
+ # end
521
+ #
522
+ # def build_task(attributes = {})
523
+ # attributes[:user_id] = self.id
524
+ # Task.new(attributes)
525
+ # end
526
+ #
527
+ # def create_task(attributes = {})
528
+ # attributes[:user_id] = self.id
529
+ # Task.create(attributes)
530
+ # end
531
+ #
532
+ # def create_task!(attributes = {})
533
+ # attributes[:user_id] = self.id
534
+ # Task.create!(attributes)
535
+ # end
536
+ code
537
+ end
538
+
539
+ def has_one_with_multiple_conditions_code
540
+ <<-code.gsub(/^\s+/, '')
541
+ # def task(force_reload = false)
542
+ # @task = nil if force_reload
543
+ # @task ||= Task.first(:conditions => {:user_id => self.id, :status => "active", :registered => "true"})
544
+ # end
545
+ #
546
+ # def task=(task)
547
+ # task.user_id = self.id
548
+ # task.save
549
+ # end
550
+ #
551
+ # def build_task(attributes = {})
552
+ # attributes[:user_id] = self.id
553
+ # Task.new(attributes)
554
+ # end
555
+ #
556
+ # def create_task(attributes = {})
557
+ # attributes[:user_id] = self.id
558
+ # Task.create(attributes)
559
+ # end
560
+ #
561
+ # def create_task!(attributes = {})
562
+ # attributes[:user_id] = self.id
563
+ # Task.create!(attributes)
564
+ # end
565
+ code
566
+ end
567
+
568
+ def has_many_code
569
+ <<-code.gsub(/^\s+/, '')
570
+ # def tasks
571
+ # Task.where(user_id: self.id)
572
+ # end
573
+ code
574
+ end
575
+ end