randumb 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/randumb.rb +14 -4
- metadata +2 -2
data/lib/randumb.rb
CHANGED
@@ -8,7 +8,11 @@ module Randumb
|
|
8
8
|
|
9
9
|
module Relation
|
10
10
|
|
11
|
-
def random(max_items =
|
11
|
+
def random(max_items = nil)
|
12
|
+
# return only the first record if method was called without parameters
|
13
|
+
return_first_record = max_items.nil?
|
14
|
+
max_items ||= 1
|
15
|
+
|
12
16
|
# take out limit from relation to use later
|
13
17
|
|
14
18
|
relation = clone
|
@@ -32,7 +36,13 @@ module Randumb
|
|
32
36
|
ids[rand_index] = id_results[rand_index]["id"] unless ids.has_key?(rand_index)
|
33
37
|
end
|
34
38
|
|
35
|
-
klass.select(original_selects).includes(original_includes).find_all_by_id(ids.values)
|
39
|
+
records = klass.select(original_selects).includes(original_includes).find_all_by_id(ids.values)
|
40
|
+
|
41
|
+
if return_first_record
|
42
|
+
records.first
|
43
|
+
else
|
44
|
+
records
|
45
|
+
end
|
36
46
|
end
|
37
47
|
|
38
48
|
end # Relation
|
@@ -40,7 +50,7 @@ module Randumb
|
|
40
50
|
module Base
|
41
51
|
|
42
52
|
# Class method
|
43
|
-
def random(max_items =
|
53
|
+
def random(max_items = nil)
|
44
54
|
relation.random(max_items)
|
45
55
|
end
|
46
56
|
|
@@ -58,4 +68,4 @@ end
|
|
58
68
|
|
59
69
|
class ActiveRecord::Base
|
60
70
|
extend Randumb::ActiveRecord::Base
|
61
|
-
end
|
71
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: randumb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Zachary Kloepping
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-08-
|
13
|
+
date: 2011-08-30 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|