Module: MuckNamedScopeMacros

Defined in:
lib/test/shoulda_macros/scopes.rb

Overview

The following macros are available: should_scope_by_title should_scope_by_alpha_title should_scope_by_name should_scope_latest should_scope_newest should_scope_by_newest should_scope_oldest should_scope_by_oldest should_scope_recent should_scope_before should_scope_since should_scope_only_public should_scope_public should_scope_created_by should_scope_by_creator should_scope_sorted

Instance Method Summary collapse

Instance Method Details

#get_klassObject



339
340
341
# File 'lib/test/shoulda_macros/scopes.rb', line 339

def get_klass
  self.name.gsub(/Test$/, '').constantize
end

#name_for_factory(klass) ⇒ Object



335
336
337
# File 'lib/test/shoulda_macros/scopes.rb', line 335

def name_for_factory(klass)
  klass.name.underscore.to_sym
end

#should_scope_beforeObject

Tests ‘before’ named scope named_scope :before, lambda { |time| {:conditions => [“items.created_at < ?”, time || DateTime.now] } }



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/test/shoulda_macros/scopes.rb', line 202

def should_scope_before
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'before' named scope" do
    setup do
      klass.delete_all
      @old = Factory(factory_name, :created_at => 6.weeks.ago)
      @new = Factory(factory_name)
    end
    should "only find older than a given date" do
      items = klass.before(1.week.ago)
      assert items.include?(@old), "since didn't find older #{klass.name}"
      assert !items.include?(@new), "since found new #{klass.name}"
    end
  end
end

#should_scope_by_alpha_titleObject

Test for ‘by_alpha’ named scope which orders by title: named_scope :by_alpha, :order => “title ASC” requires that the class have a shoulda factory



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/test/shoulda_macros/scopes.rb', line 56

def should_scope_by_alpha_title
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'by_alpha' title scope" do
    setup do
      klass.delete_all
      @first = Factory(factory_name, :title => 'a')
      @second = Factory(factory_name, :title => 'b')
    end
    should "sort by name" do
      assert_equal @first, klass.by_alpha[0]
      assert_equal @second, klass.by_alpha[1]
    end
  end
end

#should_scope_by_creatorObject

Tests ‘by_creator’ named scope. named_scope :by_creator, lambda { |creator_id| { :conditions => [‘creator_id = ?’, creator_id || 0] } }



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/test/shoulda_macros/scopes.rb', line 298

def should_scope_by_creator
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "by_creator" do
    setup do
      klass.delete_all
      @user = Factory(:user)
      @user1 = Factory(:user)
      @item = Factory(factory_name, :creator => @user)
      @item1 = Factory(factory_name, :creator => @user1)
    end
    should "find items by the source they are associated with" do
      items = klass.by_creator(@user)
      assert items.include?(@item), "created_by didn't find item created by user"
      assert !items.include?(@item1), "created_by found item not created by user"
    end
  end
end

#should_scope_by_nameObject

Test for ‘by_name’ named scope which orders by name: named_scope :by_name, :order => “name ASC” requires that the class have a shoulda factory



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/test/shoulda_macros/scopes.rb', line 75

def should_scope_by_name
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'by_name' named scope" do
    setup do
      klass.delete_all
      @first = Factory(factory_name, :name => 'a')
      @second = Factory(factory_name, :name => 'b')
    end
    should "sort by name" do
      assert_equal @first, klass.by_name[0]
      assert_equal @second, klass.by_name[1]
    end
  end
end

#should_scope_by_newestObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/test/shoulda_macros/scopes.rb', line 128

def should_scope_by_newest
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'by_newest' named scope" do
    setup do
      klass.delete_all
      @first = Factory(factory_name, :created_at => 1.day.ago)
      @second = Factory(factory_name, :created_at => 1.week.ago)
    end
    should "sort by created_at" do
      assert_equal @first, klass.by_newest[0]
      assert_equal @second, klass.by_newest[1]
    end
  end
end

#should_scope_by_oldestObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/test/shoulda_macros/scopes.rb', line 163

def should_scope_by_oldest
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'by_oldest' named scope" do
    setup do
      klass.delete_all
      @first = Factory(factory_name, :created_at => 1.day.ago)
      @second = Factory(factory_name, :created_at => 1.week.ago)
    end
    should "sort by created_at" do
      assert_equal @first, klass.by_oldest[1]
      assert_equal @second, klass.by_oldest[0]
    end
  end
end

#should_scope_by_titleObject

Test for ‘by_title’ named scope which orders by title: named_scope :by_title, :order => “title ASC” requires that the class have a shoulda factory



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/test/shoulda_macros/scopes.rb', line 37

def should_scope_by_title
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'by_title' title scope" do
    setup do
      klass.delete_all
      @first = Factory(factory_name, :title => 'a')
      @second = Factory(factory_name, :title => 'b')
    end
    should "sort by name" do
      assert_equal @first, klass.by_title[0]
      assert_equal @second, klass.by_title[1]
    end
  end
end

#should_scope_created_byObject

Tests ‘created_by’ named scope. named_scope :created_by, lambda { |item_object| {:conditions => [“items.source_id = ? AND items.source_type = ?”, item_object.id, item_object.class.to_s] } }



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/test/shoulda_macros/scopes.rb', line 277

def should_scope_created_by
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "created_by" do
    setup do
      klass.delete_all
      @user = Factory(:user)
      @user1 = Factory(:user)
      @item = Factory(factory_name, :user => @user)
      @item1 = Factory(factory_name, :user => @user1)
    end
    should "find items by the source they are associated with" do
      items = klass.created_by(@user)
      assert items.include?(@item), "created_by didn't find item created by user"
      assert !items.include?(@item1), "created_by found item not created by user"
    end
  end
end

#should_scope_latestObject

For ‘latest named scope which orders by updated at:

named_scope :latest, :order => "{klass}.updated_at DESC"


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/test/shoulda_macros/scopes.rb', line 93

def should_scope_latest
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'latest' named scope" do
    setup do
      klass.delete_all
      @first = Factory(factory_name, :updated_at => 1.day.ago)
      @second = Factory(factory_name, :updated_at => 1.week.ago)
    end
    should "sort by created_at" do
      assert_equal @first, klass.latest[0]
      assert_equal @second, klass.latest[1]
    end
  end
end

#should_scope_newestObject

Test for ‘newest’ named scope which orders by ‘created_at DESC’ named_scope :newest, :order => “created_at DESC” requires that the class have a shoulda factory



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/test/shoulda_macros/scopes.rb', line 112

def should_scope_newest
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'newest' named scope" do
    setup do
      klass.delete_all
      @first = Factory(factory_name, :created_at => 1.day.ago)
      @second = Factory(factory_name, :created_at => 1.week.ago)
    end
    should "sort by created_at" do
      assert_equal @first, klass.newest[0]
      assert_equal @second, klass.newest[1]
    end
  end
end

#should_scope_oldestObject

Test for ‘oldest’ named scope which orders by ‘created_at ASC’ named_scope :oldest, :order => “items.created_at ASC” requires that the class have a shoulda factory



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/test/shoulda_macros/scopes.rb', line 147

def should_scope_oldest
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'oldest' named scope" do
    setup do
      klass.delete_all
      @first = Factory(factory_name, :created_at => 1.day.ago)
      @second = Factory(factory_name, :created_at => 1.week.ago)
    end
    should "sort by created_at" do
      assert_equal @first, klass.oldest[1]
      assert_equal @second, klass.oldest[0]
    end
  end
end

#should_scope_only_publicObject

Tests ‘only_public’ named scope named_scope :only_public, :conditions => [“items.is_public = ?”, true]



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/test/shoulda_macros/scopes.rb', line 241

def should_scope_only_public
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "public" do
    setup do
      klass.delete_all
      @private_item = Factory(factory_name, :is_public => false)
      @public_item = Factory(factory_name, :is_public => true)
    end
    should "only find public items" do
      assert klass.only_public.include?(@public_item), "didn't find public item"
      assert !klass.only_public.include?(@private_item), "found private item"
    end
  end
end

#should_scope_publicObject

Tests ‘public’ named scope named_scope :public, :conditions => “is_public = true”



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/test/shoulda_macros/scopes.rb', line 259

def should_scope_public
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "public" do
    setup do
      klass.delete_all
      @private_item = Factory(factory_name, :is_public => false)
      @public_item = Factory(factory_name, :is_public => true)
    end
    should "only find public items" do
      assert klass.public.include?(@public_item), "didn't find public item"
      assert !klass.public.include?(@private_item), "found private item"
    end
  end
end

#should_scope_recentObject

Test for ‘recent’ named scope which orders by items created recently named_scope :recent, lambda { { :conditions => [‘items.created_at > ?’, 1.week.ago] } } requires that the class have a shoulda factory



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/test/shoulda_macros/scopes.rb', line 182

def should_scope_recent
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'recent' named scope" do
    setup do
      klass.delete_all
      @recent = Factory(factory_name)
      @not_recent = Factory(factory_name, :created_at => 10.weeks.ago)
    end
    should "get recent" do
      assert klass.recent.include?(@recent), "since didn't include recent #{klass.name}"
    end
    should "not get recent" do
      assert !klass.recent.include?(@not_recent), "since did include recent #{klass.name}"
    end
  end
end

#should_scope_sinceObject

Tests ‘since’ named scope named_scope :since, lambda { |time| {:conditions => [“items.created_at > ?”, time || DateTime.now] } }



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/test/shoulda_macros/scopes.rb', line 221

def should_scope_since
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'since' named scope" do
    setup do
      klass.delete_all
      @old = Factory(factory_name, :created_at => 6.weeks.ago)
      @new = Factory(factory_name)
    end
    should "get newer" do
      assert klass.since(1.day.ago).include?(@new), "since didn't find new #{klass.name}"
    end
    should "not get older" do
      assert !klass.since(1.day.ago).include?(@old), "since found older #{klass.name}"
    end
  end
end

#should_scope_sortedObject

Tests ‘sorted’ named scope named_scope :sorted, :order => “sort ASC”



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/test/shoulda_macros/scopes.rb', line 319

def should_scope_sorted
  klass = get_klass
  factory_name = name_for_factory(klass)
  context "'sorted' named scope" do
    setup do
      klass.delete_all
      @first = Factory(factory_name, :sort => 1)
      @second = Factory(factory_name, :sort => 2)
    end
    should "sort by 'sort' field" do
      assert_equal @first, klass.sorted[0]
      assert_equal @second, klass.sorted[1]
    end
  end
end