randumb 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7dfd32d6c13a988c2fb72afea69f8e7d2824b2cf
4
- data.tar.gz: 77ad6781fc9a3618757d916ed815e3356d1b14a0
3
+ metadata.gz: fde8e702f664f00c3ea3972f32eec66ce71637d6
4
+ data.tar.gz: 1938439b5466df1dfe6e29ae1711110ddc2ffb5c
5
5
  SHA512:
6
- metadata.gz: a0a9f3960ff23cf85fd19283ebed1027382841b19f1c4a55292ce15fccce324c0f95b1400f3e17af952bebb7da89894ca8026f117a018e88f4836d3bc4175088
7
- data.tar.gz: d01853119a813ed7f26933ce15db02a1b52ca2be5c74e9fef131facef27a9c4ab417819ebe4cc2b4acdbdd1a144ad98385e53ef8a5944d0936de4d5abe85c599
6
+ metadata.gz: 10ef96fbd1bd664ff50dcc467f6377071781f2f82a1a5f85350942c6e72a6ad7e8db1df4d2da993e2ec7b5e528d74f0b15e8f1f21293f546f51c3bd216ee5e03
7
+ data.tar.gz: baef13e37ec9801a19d26db584fce314e55e1208d0fc03342e772945e9095960c98828faf945d8184b63116f9cb953d20488aaee5cfb46d49bb0d6cff1b1df3e
@@ -162,25 +162,27 @@ module Randumb
162
162
 
163
163
 
164
164
  # Class methods
165
+ # where(nil) is because:
166
+ # http://stackoverflow.com/questions/18198963/with-rails-4-model-scoped-is-deprecated-but-model-all-cant-replace-it
165
167
  module Base
166
168
  def random(max_items = nil, opts = {})
167
- relation.random(max_items, opts)
169
+ where(nil).random(max_items, opts)
168
170
  end
169
171
 
170
172
  def random_weighted(ranking_column, max_items = nil, opts = {})
171
- relation.random_weighted(ranking_column, max_items, opts)
173
+ where(nil).random_weighted(ranking_column, max_items, opts)
172
174
  end
173
175
 
174
176
  def random_by_id_shuffle(max_items = nil, opts = {})
175
- relation.random_by_id_shuffle(max_items, opts)
177
+ where(nil).random_by_id_shuffle(max_items, opts)
176
178
  end
177
179
 
178
- def order_by_rand(opts={})
179
- relation.order_by_rand(opts)
180
+ def order_by_rand(opts = {})
181
+ where(nil).order_by_rand(opts)
180
182
  end
181
183
 
182
184
  def order_by_rand_weighted(ranking_column, opts={})
183
- relation.order_by_rand_weighted(ranking_column, opts)
185
+ where(nil).order_by_rand_weighted(ranking_column, opts)
184
186
  end
185
187
  end
186
188
 
@@ -1,3 +1,3 @@
1
1
  module Randumb
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -1,5 +1,11 @@
1
1
  class Artist < ActiveRecord::Base
2
2
  has_many :albums
3
-
3
+
4
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("3.2")
5
+ default_scope { where("artists.deleted_at IS NULL") }
6
+ else
7
+ default_scope where("artists.deleted_at IS NULL")
8
+ end
9
+
4
10
  scope :at_least_three_views, -> { where("views >= 3") }
5
- end
11
+ end
@@ -57,6 +57,14 @@ class RandumbTest < Minitest::Test
57
57
  assert_equal_for_both_methods nil, Artist.where(:name => "The Little Gentlemen")
58
58
  end
59
59
 
60
+ should "respect default scope" do
61
+ @high_on_fire.deleted_at = Time.now.utc
62
+ @high_on_fire.save!
63
+ assert_equal 0, Artist.count
64
+ assert_nil Artist.order_by_rand.first
65
+ assert_equal @high_on_fire, Artist.unscoped.order_by_rand.first
66
+ end
67
+
60
68
  context "3 records in table" do
61
69
  setup do
62
70
  @fiona_apple = FactoryGirl.create(:artist, :name => "Fiona Apple", :views => 3)
@@ -32,6 +32,7 @@ ActiveRecord::Base.connection.create_table(:artists, :force => true) do |t|
32
32
  t.float "rating"
33
33
  t.datetime "created_at"
34
34
  t.datetime "updated_at"
35
+ t.datetime "deleted_at"
35
36
  end
36
37
 
37
38
  ActiveRecord::Base.connection.create_table(:albums, :force => true) do |t|
@@ -40,6 +41,7 @@ ActiveRecord::Base.connection.create_table(:albums, :force => true) do |t|
40
41
  t.integer "artist_id"
41
42
  t.datetime "created_at"
42
43
  t.datetime "updated_at"
44
+ t.datetime "deleted_at"
43
45
  end
44
46
 
45
47
  # setup models for lazy load
@@ -59,7 +59,17 @@ class WeightedTest < Minitest::Test
59
59
  context "order by ranking_column" do
60
60
  setup do
61
61
  @view_counts = [1, 2, 3, 4, 5]
62
- @view_counts.each { |views| FactoryGirl.create(:artist, :views => views) }
62
+ @view_counts.each { |views| FactoryGirl.create(:artist, views: views) }
63
+ end
64
+
65
+ should "respect default scope" do
66
+ artist = Artist.last
67
+ artist.deleted_at = Time.now.utc
68
+ artist.save!
69
+ 50.times do
70
+ assert Artist.random_weighted("views").id != artist.id, "should never pick deleted artist"
71
+ end
72
+ assert_equal 4, Artist.order_by_rand_weighted("views").all.length
63
73
  end
64
74
 
65
75
  should "order by ranking column with explicit method call" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: randumb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zachary Kloepping
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-13 00:00:00.000000000 Z
11
+ date: 2017-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake