activerecord_random 1.0.0.rc → 1.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/activerecord_random.rb +8 -1
- data/spec/spec_helper.rb +1 -2
- metadata +3 -3
data/README.md
CHANGED
@@ -22,7 +22,7 @@ You can install ActiveRecord Random straight from Rubygems. Thanks to Bundler yo
|
|
22
22
|
Scenario: You have a collection of Questions (model name = Question) and you wish to randomly select one of them.
|
23
23
|
|
24
24
|
<pre>
|
25
|
-
Question.random #=> random
|
25
|
+
Question.random #=> random instance of Question
|
26
26
|
</pre>
|
27
27
|
|
28
28
|
Alternatively you can use the :random finder:
|
data/lib/activerecord_random.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'active_record' unless defined? ActiveRecord::Base
|
2
|
+
require 'ext/active_record/finder_methods/random'
|
3
|
+
|
1
4
|
module ActiveRecord
|
2
5
|
|
3
6
|
module Random #:nodoc:
|
@@ -11,7 +14,11 @@ module ActiveRecord
|
|
11
14
|
#
|
12
15
|
# ActiveRecord::Base.random is a safe method, thus when your model's table is empty it will simply return nil.
|
13
16
|
def random
|
14
|
-
|
17
|
+
if minimum = self.minimum(:id)
|
18
|
+
where("id >= ?", ::Random.new.rand(minimum..self.maximum(:id))).first
|
19
|
+
else
|
20
|
+
nil
|
21
|
+
end
|
15
22
|
end
|
16
23
|
|
17
24
|
end # Random
|
data/spec/spec_helper.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
$:.unshift File.dirname(__FILE__) unless $:.include? File.dirname(__FILE__)
|
2
|
-
$:.unshift File.join(File.dirname(__FILE__), '..') unless $:.include? File.join(File.dirname(__FILE__), '..')
|
3
2
|
|
4
3
|
begin
|
5
4
|
require 'erb'
|
6
|
-
require 'init'
|
7
5
|
require 'logger'
|
8
6
|
require 'rspec'
|
9
7
|
require 'active_record'
|
10
8
|
require 'database_cleaner'
|
11
9
|
require 'factory_girl'
|
10
|
+
require 'activerecord_random'
|
12
11
|
|
13
12
|
RSpec.configure do |config|
|
14
13
|
config.debug = true
|
metadata
CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
|
|
6
6
|
- 1
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.0.
|
9
|
+
- rc2
|
10
|
+
version: 1.0.0.rc2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Gnomeslab, Lda.
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-15 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|