randumb 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/randumb.rb +61 -0
- metadata +64 -0
data/lib/randumb.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'active_support/core_ext/module/delegation'
|
2
|
+
require 'active_record/relation'
|
3
|
+
|
4
|
+
module Randumb
|
5
|
+
|
6
|
+
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/query_methods.rb
|
7
|
+
module ActiveRecord
|
8
|
+
|
9
|
+
module Relation
|
10
|
+
|
11
|
+
def random(max_items = 1)
|
12
|
+
# take out limit from relation to use later
|
13
|
+
|
14
|
+
relation = clone
|
15
|
+
|
16
|
+
# store these for including at the end
|
17
|
+
original_includes = relation.includes_values
|
18
|
+
original_selects = relation.select_values
|
19
|
+
|
20
|
+
# clear these for our id only query
|
21
|
+
relation.select_values = []
|
22
|
+
relation.includes_values = []
|
23
|
+
|
24
|
+
# does their original query but only for id fields
|
25
|
+
id_only_relation = relation.select("#{table_name}.id")
|
26
|
+
id_results = connection.select_all(id_only_relation.to_sql)
|
27
|
+
|
28
|
+
ids = {}
|
29
|
+
|
30
|
+
while( ids.length < max_items && ids.length < id_results.length )
|
31
|
+
rand_index = rand( id_results.length )
|
32
|
+
ids[rand_index] = id_results[rand_index]["id"] unless ids.has_key?(rand_index)
|
33
|
+
end
|
34
|
+
|
35
|
+
klass.select(original_selects).includes(original_includes).find_all_by_id(ids.values)
|
36
|
+
end
|
37
|
+
|
38
|
+
end # Relation
|
39
|
+
|
40
|
+
module Base
|
41
|
+
|
42
|
+
# Class method
|
43
|
+
def random(max_items = 1)
|
44
|
+
relation.random(max_items)
|
45
|
+
end
|
46
|
+
|
47
|
+
end # Base
|
48
|
+
|
49
|
+
|
50
|
+
end # ActiveRecord
|
51
|
+
|
52
|
+
end # Randumb
|
53
|
+
|
54
|
+
# Mix it in
|
55
|
+
class ActiveRecord::Relation
|
56
|
+
include Randumb::ActiveRecord::Relation
|
57
|
+
end
|
58
|
+
|
59
|
+
class ActiveRecord::Base
|
60
|
+
extend Randumb::ActiveRecord::Base
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: randumb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Zachary Kloepping
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-17 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
description:
|
27
|
+
email:
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files: []
|
33
|
+
|
34
|
+
files:
|
35
|
+
- lib/randumb.rb
|
36
|
+
homepage: https://github.com/spilliton/randumb
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.6.6
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.8.8
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Adds the ability to pull random records from ActiveRecord
|
63
|
+
test_files: []
|
64
|
+
|