randumb 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/randumb.rb +15 -36
- metadata +3 -3
data/lib/randumb.rb
CHANGED
@@ -2,69 +2,48 @@ require 'active_support/core_ext/module/delegation'
|
|
2
2
|
require 'active_record/relation'
|
3
3
|
|
4
4
|
module Randumb
|
5
|
-
|
6
|
-
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/query_methods.rb
|
5
|
+
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/query_methods.rb
|
7
6
|
module ActiveRecord
|
8
7
|
|
9
8
|
module Relation
|
10
9
|
|
11
10
|
def random(max_items = nil)
|
12
|
-
|
13
|
-
return_first_record = max_items.nil?
|
11
|
+
return_first_record = max_items.nil? # see return switch at end
|
14
12
|
max_items ||= 1
|
15
|
-
|
16
|
-
# take out limit from relation to use later
|
17
|
-
|
18
13
|
relation = clone
|
19
14
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
relation.includes_values = []
|
27
|
-
|
28
|
-
# does their original query but only for id fields
|
29
|
-
id_only_relation = relation.select("#{table_name}.id")
|
30
|
-
id_results = connection.select_all(id_only_relation.to_sql)
|
31
|
-
|
32
|
-
ids = {}
|
33
|
-
|
34
|
-
while( ids.length < max_items && ids.length < id_results.length )
|
35
|
-
rand_index = rand( id_results.length )
|
36
|
-
ids[rand_index] = id_results[rand_index]["id"] unless ids.has_key?(rand_index)
|
15
|
+
if connection.adapter_name =~ /sqlite/i || connection.adapter_name =~ /postgres/i
|
16
|
+
rand_syntax = "RANDOM()"
|
17
|
+
elsif connection.adapter_name =~ /mysql/i
|
18
|
+
rand_syntax = "RAND()"
|
19
|
+
else
|
20
|
+
raise Exception, "ActiveRecord adapter: '#{connection.adapter_name}' not supported by randumb. Send a pull request or open a ticket: https://github.com/spilliton/randumb"
|
37
21
|
end
|
38
|
-
|
39
|
-
the_scope =
|
40
|
-
|
41
|
-
the_scope = the_scope.select(original_selects) unless original_selects.empty?
|
42
|
-
records = the_scope.find_all_by_id(ids.values)
|
22
|
+
|
23
|
+
the_scope = relation.order(rand_syntax)
|
24
|
+
the_scope = the_scope.limit(max_items) unless relation.limit_value && relation.limit_value < max_items
|
43
25
|
|
26
|
+
# return first record if method was called without parameters
|
44
27
|
if return_first_record
|
45
|
-
|
28
|
+
the_scope.first
|
46
29
|
else
|
47
|
-
|
30
|
+
the_scope.all
|
48
31
|
end
|
49
32
|
end
|
50
33
|
|
51
34
|
end # Relation
|
52
35
|
|
53
36
|
module Base
|
54
|
-
|
55
37
|
# Class method
|
56
38
|
def random(max_items = nil)
|
57
39
|
relation.random(max_items)
|
58
40
|
end
|
59
|
-
|
60
|
-
end # Base
|
41
|
+
end
|
61
42
|
|
62
43
|
|
63
44
|
end # ActiveRecord
|
64
|
-
|
65
45
|
end # Randumb
|
66
46
|
|
67
|
-
|
68
47
|
# Mix it in
|
69
48
|
class ActiveRecord::Relation
|
70
49
|
include Randumb::ActiveRecord::Relation
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: randumb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
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:
|
13
|
+
date: 2012-05-06 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
56
|
requirements: []
|
57
57
|
|
58
58
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.8.
|
59
|
+
rubygems_version: 1.8.24
|
60
60
|
signing_key:
|
61
61
|
specification_version: 3
|
62
62
|
summary: Adds the ability to pull random records from ActiveRecord
|