has_moderated 1.0.rc4 → 1.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,10 @@
1
1
  require File.expand_path('../../spec_helper', __FILE__)
2
- require File.expand_path('../../support/photos', __FILE__)
2
+
3
+ def reload_models
4
+ crazy_models.reset
5
+ crazy_models.with_helpers &block if block_given?
6
+ crazy_models
7
+ end
3
8
 
4
9
  describe Photo do
5
10
  before(:each) do
@@ -9,11 +14,12 @@ describe Photo do
9
14
 
10
15
  context "create moderated:" do
11
16
  before do
12
- Object.send(:remove_const, 'Photo')
13
- load 'photo.rb'
14
- Photo.has_moderated_create
15
- Photo.send :include, HasModerated::CarrierWave
16
- Photo.has_moderated_carrierwave_field :avatar
17
+ reload_models.photo {
18
+ mount_uploader :avatar, GenericUploader
19
+ has_moderated_create
20
+ send :include, HasModerated::CarrierWave
21
+ has_moderated_carrierwave_field :avatar
22
+ }
17
23
  end
18
24
 
19
25
  it "should upload photo" do
@@ -34,8 +40,9 @@ describe Photo do
34
40
 
35
41
  context "not moderated:" do
36
42
  before do
37
- Object.send(:remove_const, 'Photo')
38
- load 'photo.rb'
43
+ reload_models.photo {
44
+ mount_uploader :avatar, GenericUploader
45
+ }
39
46
  end
40
47
 
41
48
  it "should upload photo" do
@@ -52,11 +59,12 @@ describe Photo do
52
59
 
53
60
  context "update moderated:" do
54
61
  before do
55
- Object.send(:remove_const, 'Photo')
56
- load 'photo.rb'
57
- Photo.send :include, HasModerated::CarrierWave
58
- Photo.has_moderated_carrierwave_field :avatar
59
- Photo.has_moderated :avatar
62
+ reload_models.photo {
63
+ mount_uploader :avatar, GenericUploader
64
+ send :include, HasModerated::CarrierWave
65
+ has_moderated_carrierwave_field :avatar
66
+ has_moderated :avatar
67
+ }
60
68
  end
61
69
 
62
70
  it "should moderate photo (on create)" do
@@ -92,4 +100,55 @@ describe Photo do
92
100
  assert_photo_uploaded(photo.avatar)
93
101
  end
94
102
  end
103
+
104
+ context "moderated as association to has_moderated_create:" do
105
+ before do
106
+ reload_models.task {
107
+ has_many :renamed_subtasks, :class_name => subtask_class_name, :foreign_key => task_fk
108
+ has_many :photos, :class_name => photo_class_name, :foreign_key => "parentable_id"
109
+ has_moderated_create :with_associations => [:photos, :renamed_subtasks]
110
+ }.subtask {
111
+ belongs_to :task, :class_name => task_class_name
112
+ }.photo {
113
+ mount_uploader :avatar, GenericUploader
114
+ send :include, HasModerated::CarrierWave
115
+ has_moderated_carrierwave_field :avatar
116
+ belongs_to :task, :class_name => task_class_name, :foreign_key => "parentable_id"
117
+ }
118
+
119
+ end
120
+
121
+ it "should upload photo" do
122
+ task = Task.new :title => "Task 1"
123
+ task.photos.build :avatar => carrierwave_test_photo
124
+ task.save
125
+
126
+ Task.count.should eq(0)
127
+ Photo.count.should eq(0)
128
+ tmpEmpty?.should be_false
129
+ uploadEmpty?.should be_true
130
+ Moderation.last.accept
131
+ tmpEmpty?.should be_true
132
+ uploadEmpty?.should be_false
133
+
134
+ Task.first.photos.count.should eq(1)
135
+ photo = Task.first.photos.first
136
+ assert_photo_uploaded(photo.avatar)
137
+ end
138
+
139
+ it "should not add any photos if none were added" do
140
+ task = Task.new :title => "Task 1"
141
+ task.renamed_subtasks.build :title => "Subtask 1"
142
+ task.save
143
+
144
+ Moderation.last.parsed_data[:create][:associations][:photos].should be_nil
145
+
146
+ Task.count.should eq(0)
147
+ Photo.count.should eq(0)
148
+ Moderation.last.accept
149
+
150
+ Task.first.photos.count.should eq(0)
151
+ Photo.count.should eq(0)
152
+ end
153
+ end
95
154
  end
@@ -1,10 +1,9 @@
1
1
  require File.expand_path('../../spec_helper', __FILE__)
2
2
 
3
- def reload_task_subtask
4
- Object.send(:remove_const, 'Task') if defined? Task
5
- load 'task.rb'
6
- Object.send(:remove_const, 'Subtask') if defined? Subtask
7
- load 'subtask.rb'
3
+ def reload_models &block
4
+ crazy_models.reset
5
+ crazy_models.with_helpers &block if block_given?
6
+ crazy_models
8
7
  end
9
8
 
10
9
  describe Task do
@@ -16,9 +15,12 @@ describe Task do
16
15
 
17
16
  context "has_many association:" do
18
17
  before do
19
- reload_task_subtask
20
- Task.has_many :renamed_subtasks, :class_name => "Subtask"
21
- Task.has_moderated_association :renamed_subtasks
18
+ reload_models.task {
19
+ has_many :renamed_subtasks, :class_name => subtask_class_name, :foreign_key => task_fk
20
+ has_moderated_association :renamed_subtasks
21
+ }.subtask {
22
+ belongs_to :task, :class_name => task_class_name, :foreign_key => task_fk
23
+ }
22
24
  end
23
25
 
24
26
  it "creates and associates subtask (create)" do
@@ -125,10 +127,12 @@ describe Task do
125
127
 
126
128
  context "has_many polymorphic association:" do
127
129
  before do
128
- reload_task_subtask
129
- Task.has_many :renamed_subtasks, :class_name => "Subtask", :as => :parentable
130
- Subtask.belongs_to :parentable, :polymorphic => true
131
- Task.has_moderated_association :renamed_subtasks
130
+ reload_models.task {
131
+ has_many :renamed_subtasks, :class_name => subtask_class_name, :as => :parentable
132
+ has_moderated_association :renamed_subtasks
133
+ }.subtask {
134
+ belongs_to :parentable, :class_name => task_class_name, :polymorphic => true
135
+ }
132
136
  end
133
137
 
134
138
  it "creates and associates subtask (create)" do
@@ -145,7 +149,7 @@ describe Task do
145
149
 
146
150
  subtask = Task.first.renamed_subtasks.first
147
151
  subtask.title.should eq("Subtask 1")
148
- subtask.parentable_type.should eq("Task")
152
+ subtask.parentable.should eq(Task.first)
149
153
  end
150
154
  end
151
155
 
@@ -156,10 +160,12 @@ describe Task do
156
160
 
157
161
  context "has_one polymorphic association:" do
158
162
  before do
159
- reload_task_subtask
160
- Task.has_one :renamed_subtask, :class_name => "Subtask", :as => :parentable
161
- Subtask.belongs_to :parentable, :polymorphic => true
162
- Task.has_moderated_association :renamed_subtask
163
+ reload_models.task {
164
+ has_one :renamed_subtask, :class_name => subtask_class_name, :as => :parentable
165
+ has_moderated_association :renamed_subtask
166
+ }.subtask {
167
+ belongs_to :parentable, :class_name => task_class_name, :polymorphic => true
168
+ }
163
169
  end
164
170
 
165
171
  it "creates and associates subtask (create)" do
@@ -177,7 +183,7 @@ describe Task do
177
183
 
178
184
  subtask = Task.first.renamed_subtask
179
185
  subtask.title.should eq("Subtask 1")
180
- subtask.parentable_type.should eq("Task")
186
+ subtask.parentable.should eq(Task.first)
181
187
  end
182
188
  end
183
189
 
@@ -188,10 +194,12 @@ describe Task do
188
194
 
189
195
  context "has_and_belongs_to_many association:" do
190
196
  before do
191
- reload_task_subtask
192
- Task.has_and_belongs_to_many :renamed_subtasks, :class_name => "Subtask", :join_table => "tasks_jointable", :foreign_key => "m1_id", :association_foreign_key => "m2_id"
193
- Subtask.has_and_belongs_to_many :renamed_tasks, :class_name => "Task", :join_table => "tasks_jointable", :foreign_key => "m2_id", :association_foreign_key => "m1_id"
194
- Task.has_moderated_association :renamed_subtasks
197
+ reload_models.task {
198
+ has_and_belongs_to_many :renamed_subtasks, :class_name => subtask_class_name, :join_table => "tasks_jointable", :foreign_key => "m1_id", :association_foreign_key => "m2_id"
199
+ has_moderated_association :renamed_subtasks
200
+ }.subtask {
201
+ has_and_belongs_to_many :renamed_tasks, :class_name => task_class_name, :join_table => "tasks_jointable", :foreign_key => "m2_id", :association_foreign_key => "m1_id"
202
+ }
195
203
  end
196
204
 
197
205
  it "creates and associates a new subtask" do
@@ -228,12 +236,15 @@ describe Task do
228
236
  subtask.title.should eq("Subtask 1")
229
237
  end
230
238
  end
239
+
231
240
  context "has_and_belongs_to_many association (create moderation):" do
232
241
  before :each do # important that we do this before EACH
233
- reload_task_subtask
234
- Task.has_moderated_create :with_associations => [:renamed_subtasks]
235
- Task.has_and_belongs_to_many :renamed_subtasks, :class_name => "Subtask", :join_table => "tasks_jointable", :foreign_key => "m1_id", :association_foreign_key => "m2_id"
236
- Subtask.has_and_belongs_to_many :renamed_tasks, :class_name => "Task", :join_table => "tasks_jointable", :foreign_key => "m2_id", :association_foreign_key => "m1_id"
242
+ reload_models.task {
243
+ has_moderated_create :with_associations => [:renamed_subtasks]
244
+ has_and_belongs_to_many :renamed_subtasks, :class_name => subtask_class_name, :join_table => "tasks_jointable", :foreign_key => "m1_id", :association_foreign_key => "m2_id"
245
+ }.subtask {
246
+ has_and_belongs_to_many :renamed_tasks, :class_name => task_class_name, :join_table => "tasks_jointable", :foreign_key => "m2_id", :association_foreign_key => "m1_id"
247
+ }
237
248
  end
238
249
 
239
250
  it "associates an existing subtask on create 1" do
@@ -283,13 +294,18 @@ describe Task do
283
294
 
284
295
  context "has_many :through association:" do
285
296
  before do
286
- reload_task_subtask
287
- Task.has_many :renamed_connections, :class_name => "TaskConnection", :foreign_key => "m1_id"
288
- Task.has_many :renamed_subtasks, :class_name => "Subtask", :through => :renamed_connections, :source => :renamed_subtask
289
- Subtask.has_many :renamed_connections, :class_name => "TaskConnection", :foreign_key => "m2_id"
290
- Subtask.has_many :renamed_tasks, :class_name => "Task", :through => :renamed_connections, :source => :renamed_task
291
- Task.has_moderated_association :renamed_subtasks
292
- Task.has_moderated_association :renamed_connections
297
+ reload_models.task {
298
+ has_many :renamed_connections, :class_name => task_connection_class_name, :foreign_key => "m1_id"
299
+ has_many :renamed_subtasks, :class_name => subtask_class_name, :through => :renamed_connections, :source => :renamed_subtask
300
+ has_moderated_association :renamed_subtasks
301
+ has_moderated_association :renamed_connections
302
+ }.subtask {
303
+ has_many :renamed_connections, :class_name => task_connection_class_name, :foreign_key => "m2_id"
304
+ has_many :renamed_tasks, :class_name => task_class_name, :through => :renamed_connections, :source => :renamed_task
305
+ }.task_connection {
306
+ belongs_to :renamed_task, :class_name => task_class_name, :foreign_key => "m1_id"
307
+ belongs_to :renamed_subtask, :class_name => subtask_class_name, :foreign_key => "m2_id"
308
+ }
293
309
  end
294
310
 
295
311
  it "associates subtask 1 (update)" do
@@ -373,9 +389,12 @@ describe Task do
373
389
 
374
390
  context "has_one association:" do
375
391
  before do
376
- reload_task_subtask
377
- Task.has_one :renamed_subtask, :class_name => "Subtask"
378
- Task.has_moderated_association :renamed_subtask
392
+ reload_models.task {
393
+ has_one :renamed_subtask, :class_name => subtask_class_name, :foreign_key => task_fk
394
+ has_moderated_association :renamed_subtask
395
+ }.subtask {
396
+ belongs_to :task, :class_name => task_class_name
397
+ }
379
398
  end
380
399
 
381
400
  it "creates and associates subtask (= new, task save)" do
@@ -430,15 +449,72 @@ describe Task do
430
449
  end
431
450
  end
432
451
 
452
+ context "has_one association (create moderation):" do
453
+ before :each do
454
+ reload_models.task {
455
+ has_one :renamed_subtask, :class_name => subtask_class_name, :foreign_key => task_fk
456
+ has_moderated_create :with_associations => [:renamed_subtask]
457
+ }.subtask {
458
+ belongs_to :task, :class_name => task_class_name
459
+ }
460
+ end
461
+
462
+ it "associates an existing subtask on create 1" do
463
+ Task.has_moderated_association :renamed_subtask
464
+ Subtask.create! :title => "Subtask 1"
465
+ Subtask.count.should eq(1)
466
+ Moderation.count.should eq(0)
467
+
468
+ task = Task.new :title => "Task 1"
469
+ task.renamed_subtask = Subtask.first
470
+ task.save
471
+
472
+ Subtask.first.task_id.should be_nil
473
+
474
+ Task.count.should eq(0)
475
+ Moderation.count.should eq(1)
476
+ Moderation.last.accept
477
+ Moderation.count.should eq(0)
478
+ Subtask.first.task_id.should_not be_nil
479
+
480
+ subtask = Task.first.renamed_subtask
481
+ subtask.title.should eq("Subtask 1")
482
+ end
483
+
484
+ it "associates an existing subtask on create 2" do
485
+ Subtask.create! :title => "Subtask 1"
486
+ Subtask.count.should eq(1)
487
+ Moderation.count.should eq(0)
488
+
489
+ task = Task.new :title => "Task 1"
490
+ task.renamed_subtask = Subtask.first
491
+ task.save
492
+
493
+ Subtask.first.task_id.should be_nil
494
+
495
+ Task.count.should eq(0)
496
+ Moderation.count.should eq(1)
497
+ Moderation.last.accept
498
+ Moderation.count.should eq(0)
499
+ Subtask.first.task_id.should_not be_nil
500
+
501
+ subtask = Task.first.renamed_subtask
502
+ subtask.title.should eq("Subtask 1")
503
+ end
504
+ end
505
+
433
506
  #
434
507
  # has_moderated_create
435
508
  #
436
509
 
437
510
  context "create moderation with association:" do
438
511
  before do
439
- reload_task_subtask
440
- Task.has_many :renamed_subtasks, :class_name => "Subtask"
441
- Task.has_moderated_create :with_associations => [:renamed_subtasks]
512
+ reload_models.task {
513
+ has_many :renamed_subtasks, :class_name => subtask_class_name, :foreign_key => task_fk
514
+ has_moderated_create :with_associations => [:renamed_subtasks]
515
+ }.subtask {
516
+ belongs_to :task, :class_name => task_class_name
517
+ }
442
518
  end
443
519
 
444
520
  it "moderates create" do
@@ -462,6 +538,13 @@ describe Task do
462
538
  Task.last.renamed_subtasks.count.should eq(1)
463
539
  Task.last.renamed_subtasks.first.title.should eq("Subtask 1")
464
540
  end
541
+
542
+ it "doesn't create anything if nothing was created" do
543
+ task = Task.create! :title => "Task 1"
544
+ Moderation.last.accept
545
+
546
+ Task.first.renamed_subtasks.count.should eq(0)
547
+ end
465
548
  end
466
549
 
467
550
  #
@@ -470,8 +553,9 @@ describe Task do
470
553
 
471
554
  context "destroy moderation:" do
472
555
  before do
473
- reload_task_subtask
474
- Task.has_moderated_destroy
556
+ reload_models.task {
557
+ has_moderated_destroy
558
+ }
475
559
  end
476
560
 
477
561
  it "moderates destroy" do
@@ -490,8 +574,9 @@ describe Task do
490
574
 
491
575
  context "moderates attributes:" do
492
576
  before do
493
- reload_task_subtask
494
- Task.has_moderated :title
577
+ reload_models.task {
578
+ has_moderated :title
579
+ }
495
580
  end
496
581
 
497
582
  it "moderates an attribute" do
@@ -513,13 +598,14 @@ describe Task do
513
598
 
514
599
  context "common features:" do
515
600
  it "get_moderation_attributes can be overriden in model" do
516
- reload_task_subtask
517
- Task.has_moderated_create
518
- Task.class_eval do
519
- def get_moderation_attributes
520
- { :test => "ok" }
601
+ reload_models.task {
602
+ has_moderated_create
603
+ self.class_eval do
604
+ def get_moderation_attributes
605
+ { :test => "ok" }
606
+ end
521
607
  end
522
- end
608
+ }
523
609
  Task.create! :title => "Task 1"
524
610
  data = YAML::load(Moderation.last.data)[:create][:attributes]
525
611
  data.should_not be_blank
@@ -529,8 +615,9 @@ describe Task do
529
615
  end
530
616
 
531
617
  it "knows if it's a create moderation" do
532
- reload_task_subtask
533
- Task.has_moderated_create
618
+ reload_models.task {
619
+ has_moderated_create
620
+ }
534
621
 
535
622
  Task.create! :title => "Task 1"
536
623
 
@@ -540,8 +627,9 @@ describe Task do
540
627
  end
541
628
 
542
629
  it "knows if it's a destroy moderation" do
543
- reload_task_subtask
544
- Task.has_moderated_destroy
630
+ reload_models.task {
631
+ has_moderated_destroy
632
+ }
545
633
 
546
634
  Task.create! :title => "Task 1"
547
635
  Task.last.destroy
@@ -554,11 +642,12 @@ describe Task do
554
642
 
555
643
  context "hooks:" do
556
644
  before do
557
- reload_task_subtask
558
- Task.has_moderated :title
559
- Task.moderation_creating do |moderation|
560
- moderation.data = "Test!"
561
- end
645
+ reload_models.task {
646
+ has_moderated :title
647
+ moderation_creating do |moderation|
648
+ moderation.data = "Test!"
649
+ end
650
+ }
562
651
  end
563
652
 
564
653
  it "handles a creating hook properly" do
@@ -569,18 +658,110 @@ describe Task do
569
658
  end
570
659
 
571
660
  context "preview:" do
572
- before do
573
- reload_task_subtask
574
- Task.has_moderated :title
661
+ it "shows a live preview of changed attributes" do
662
+ reload_models.task {
663
+ has_moderated :title
664
+ }
665
+
666
+ Task.create! :title => "Task 1"
667
+ Task.last.title.should be_blank
668
+
669
+ last_task_id = Task.last.id
670
+ Moderation.last.live_preview do |preview|
671
+ preview.title.should eq("Task 1")
672
+ preview.id.should eq(last_task_id)
673
+ end
674
+
675
+ Task.last.title.should be_blank
575
676
  end
576
677
 
577
- it "shows a preview of changed attributes" do
578
- Task.create! :title => "Task 1"
678
+ it "shows a saved preview of changed attributes" do
679
+ reload_models.task {
680
+ has_moderated :title
681
+ }
682
+
683
+ task = Task.create! :title => "Task 1"
579
684
  Task.last.title.should be_blank
580
685
 
581
686
  preview = Moderation.last.preview
582
687
  preview.title.should eq("Task 1")
583
688
  preview.id.should eq(Task.last.id)
689
+ Task.last.title.should be_blank
690
+ end
691
+
692
+ it "supports dirty tracking for the saved preview" do
693
+ reload_models.task {
694
+ has_moderated :title
695
+ }
696
+
697
+ task = Task.create! :title => "Task 1"
698
+ Task.last.title.should be_blank
699
+
700
+ preview = Moderation.last.preview
701
+ preview.title_changed?.should be_true
702
+ preview.title_change.should eq([nil, "Task 1"])
703
+ end
704
+
705
+ it "shows a saved preview of has_many association" do
706
+ reload_models.task {
707
+ has_many :renamed_subtasks, :class_name => subtask_class_name, :foreign_key => task_fk
708
+ has_moderated_association :renamed_subtasks
709
+ }.subtask {
710
+ belongs_to :task, :class_name => task_class_name
711
+ }
712
+
713
+ task = Task.create! :title => "Task 1"
714
+
715
+ task.renamed_subtasks.create! :title => "Subtask 1"
716
+ preview = Moderation.last.preview
717
+ subtask = preview.renamed_subtasks.first
718
+ subtask.title.should eq("Subtask 1")
719
+ subtask.id.should_not be_blank
720
+ subtask.task.should_not be_blank
721
+
722
+ Task.last.renamed_subtasks.count.should eq(0)
723
+ Subtask.count.should eq(0)
724
+ end
725
+
726
+ it "shows a saved preview of has_many :through association" do
727
+ reload_models.task {
728
+ attr_accessible :title, :desc
729
+ has_many :renamed_connections, :class_name => task_connection_class_name, :foreign_key => "m1_id"
730
+ has_many :renamed_subtasks, :class_name => subtask_class_name, :through => :renamed_connections, :source => :renamed_subtask
731
+ has_moderated_association :renamed_subtasks
732
+ has_moderated_association :renamed_connections
733
+ }.subtask {
734
+ attr_accessible :title, :desc
735
+ belongs_to :task
736
+ has_many :renamed_connections, :class_name => task_connection_class_name, :foreign_key => "m2_id"
737
+ has_many :renamed_tasks, :class_name => task_class_name, :through => :renamed_connections, :source => :renamed_task
738
+ }.task_connection {
739
+ belongs_to :renamed_task, :class_name => task_class_name, :foreign_key => "m1_id"
740
+ belongs_to :renamed_subtask, :class_name => subtask_class_name, :foreign_key => "m2_id"
741
+ }
742
+
743
+ task = Task.create! :title => "Task 1"
744
+ conn = TaskConnection.new :title => "Connection 1"
745
+ conn.renamed_subtask = Subtask.new :title => "Subtask 1"
746
+ task.renamed_connections << conn
747
+ task.save
748
+
749
+ TaskConnection.count.should eq(0)
750
+ Subtask.count.should eq(0)
751
+
752
+ task = Moderation.last.preview
753
+
754
+ TaskConnection.count.should eq(0)
755
+ Subtask.count.should eq(0)
756
+ Moderation.count.should eq(1)
757
+
758
+ subtask = task.renamed_subtasks.first
759
+ subtask.title.should eq("Subtask 1")
760
+ subtask.renamed_connections.first.should be_present
761
+ conn = task.renamed_connections.first
762
+ conn.title.should eq("Connection 1")
763
+ conn.renamed_subtask.title.should eq("Subtask 1")
764
+ conn.renamed_task.title.should eq("Task 1")
584
765
  end
585
766
  end
586
767
  end
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'spork'
3
+ require 'pry'
3
4
  #uncomment the following line to use spork with the debugger
4
5
  #require 'spork/ext/ruby-debug'
5
6
 
@@ -0,0 +1,85 @@
1
+ # This is crazy and ugly and a hack! But it works and I don't want to have 100 different model files in models/
2
+ # Please do not use this code for your stuff, it's not optimal in any way. I will probably do this more properly when
3
+ # I have the time.
4
+
5
+ def crazy_model_classes
6
+ [:Task, :Subtask, :TaskConnection, :Photo]
7
+ end
8
+
9
+ def crazy_models
10
+ $crazy_models ||= CrazyModels.new
11
+ end
12
+
13
+ class CrazyModels
14
+ module SetupHelpers
15
+ crazy_model_classes.each do |name|
16
+ self.send(:define_method, "#{name.to_s.underscore}_class_name") do
17
+ crazy_models.get_klass(name).to_s
18
+ end
19
+ self.send(:define_method, "#{name.to_s.underscore}_fk") do
20
+ name.to_s.underscore + "_id"
21
+ end
22
+ end
23
+ end
24
+
25
+ class SetupHelperHolder
26
+ extend SetupHelpers
27
+ end
28
+
29
+ def initialize
30
+ @counter = 0
31
+ @current_models = Hash.new
32
+ end
33
+
34
+ def with_helpers &block
35
+ SetupHelperHolder.class_eval &block
36
+ end
37
+
38
+ crazy_model_classes.each do |name|
39
+ self.send(:define_method, name.to_s.underscore) do |&block|
40
+ @current_models[name].class_eval(&block)
41
+ self
42
+ end
43
+ end
44
+
45
+ def reset
46
+ @counter += 1
47
+ crazy_model_classes.each do |name|
48
+ klass_name = "#{name}#{@counter}"
49
+ table_name = name.to_s.underscore.pluralize
50
+ klass = Class.new(ActiveRecord::Base) do
51
+ self.table_name = table_name
52
+ extend SetupHelpers
53
+ self
54
+ end
55
+ klass = Object.const_set(klass_name, klass)
56
+ @current_models[name] = klass
57
+ end
58
+ self
59
+ end
60
+
61
+ def get_klass model_name
62
+ @current_models[model_name]
63
+ end
64
+
65
+ def access model_name, method, *args, &block
66
+ @current_models[model_name].send(method, *args, &block) if @current_models[model_name]
67
+ end
68
+ end
69
+
70
+ # This part is especially ugly and unncessary, but I'm lazy...
71
+ crazy_model_classes.each do |name|
72
+ begin
73
+ Object.instance_eval{ remove_const name }
74
+ rescue ; end
75
+ # proxy module, proxies to current AR model
76
+ m = Module.new do
77
+ def self.method_missing(m, *args, &block)
78
+ $crazy_models.access(name.to_sym, m, *args, &block)
79
+ end
80
+ end
81
+ Object.const_set name.to_s, m
82
+ end
83
+
84
+ # initialize
85
+ crazy_models.reset
@@ -10,11 +10,12 @@ def tmpEmpty?
10
10
  end
11
11
 
12
12
  def uploadEmpty?
13
- dirEmpty?(UPLOADDIR)
13
+ photoModel = crazy_models.get_klass(:Photo)
14
+ dir = File.expand_path("../../../public/uploads/#{photoModel.to_s.underscore}/avatar", __FILE__)
15
+ dirEmpty?(dir)
14
16
  end
15
17
 
16
18
  TEMPDIR = File.expand_path("../../../public/uploads/tmp", __FILE__)
17
- UPLOADDIR = File.expand_path("../../../public/uploads/photo/avatar/1", __FILE__)
18
19
 
19
20
  def carrierwave_test_photo
20
21
  test_photo_path = File.expand_path("../../../public/test.jpg", __FILE__)