activerecord_random 1.0.0.rc

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 3.0.1'
4
+
5
+ group :development, :test do
6
+ gem 'mysql2', '~> 0.2'
7
+
8
+ if RUBY_VERSION =~ /1\.9\.\d/
9
+ gem('ruby-debug-base19', '=0.11.23') if RUBY_VERSION == '1.9.1'
10
+ gem 'ruby-debug19', '~> 0.11.0'
11
+ else
12
+ gem 'ruby-debug'
13
+ end
14
+
15
+ gem 'rspec', '~> 2.0'
16
+ gem 'factory_girl_rails', '~> 1.0.0'
17
+ gem 'database_cleaner', '~> 0.6.0'
18
+ end
@@ -0,0 +1,106 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.1)
6
+ actionpack (= 3.0.1)
7
+ mail (~> 2.2.5)
8
+ actionpack (3.0.1)
9
+ activemodel (= 3.0.1)
10
+ activesupport (= 3.0.1)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4.1)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.12)
16
+ rack-test (~> 0.5.4)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.1)
19
+ activesupport (= 3.0.1)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4.1)
22
+ activerecord (3.0.1)
23
+ activemodel (= 3.0.1)
24
+ activesupport (= 3.0.1)
25
+ arel (~> 1.0.0)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.1)
28
+ activemodel (= 3.0.1)
29
+ activesupport (= 3.0.1)
30
+ activesupport (3.0.1)
31
+ archive-tar-minitar (0.5.2)
32
+ arel (1.0.1)
33
+ activesupport (~> 3.0.0)
34
+ builder (2.1.2)
35
+ columnize (0.3.2)
36
+ database_cleaner (0.6.0)
37
+ diff-lcs (1.1.2)
38
+ erubis (2.6.6)
39
+ abstract (>= 1.0.0)
40
+ factory_girl (1.3.2)
41
+ factory_girl_rails (1.0)
42
+ factory_girl (~> 1.3)
43
+ rails (>= 3.0.0.beta4)
44
+ i18n (0.4.2)
45
+ linecache19 (0.5.11)
46
+ ruby_core_source (>= 0.1.4)
47
+ mail (2.2.9)
48
+ activesupport (>= 2.3.6)
49
+ i18n (~> 0.4.1)
50
+ mime-types (~> 1.16)
51
+ treetop (~> 1.4.8)
52
+ mime-types (1.16)
53
+ mysql2 (0.2.6)
54
+ polyglot (0.3.1)
55
+ rack (1.2.1)
56
+ rack-mount (0.6.13)
57
+ rack (>= 1.0.0)
58
+ rack-test (0.5.6)
59
+ rack (>= 1.0)
60
+ rails (3.0.1)
61
+ actionmailer (= 3.0.1)
62
+ actionpack (= 3.0.1)
63
+ activerecord (= 3.0.1)
64
+ activeresource (= 3.0.1)
65
+ activesupport (= 3.0.1)
66
+ bundler (~> 1.0.0)
67
+ railties (= 3.0.1)
68
+ railties (3.0.1)
69
+ actionpack (= 3.0.1)
70
+ activesupport (= 3.0.1)
71
+ rake (>= 0.8.4)
72
+ thor (~> 0.14.0)
73
+ rake (0.8.7)
74
+ rspec (2.1.0)
75
+ rspec-core (~> 2.1.0)
76
+ rspec-expectations (~> 2.1.0)
77
+ rspec-mocks (~> 2.1.0)
78
+ rspec-core (2.1.0)
79
+ rspec-expectations (2.1.0)
80
+ diff-lcs (~> 1.1.2)
81
+ rspec-mocks (2.1.0)
82
+ ruby-debug-base19 (0.11.24)
83
+ columnize (>= 0.3.1)
84
+ linecache19 (>= 0.5.11)
85
+ ruby_core_source (>= 0.1.4)
86
+ ruby-debug19 (0.11.6)
87
+ columnize (>= 0.3.1)
88
+ linecache19 (>= 0.5.11)
89
+ ruby-debug-base19 (>= 0.11.19)
90
+ ruby_core_source (0.1.4)
91
+ archive-tar-minitar (>= 0.5.2)
92
+ thor (0.14.4)
93
+ treetop (1.4.8)
94
+ polyglot (>= 0.3.1)
95
+ tzinfo (0.3.23)
96
+
97
+ PLATFORMS
98
+ ruby
99
+
100
+ DEPENDENCIES
101
+ activerecord (~> 3.0.1)
102
+ database_cleaner (~> 0.6.0)
103
+ factory_girl_rails (~> 1.0.0)
104
+ mysql2 (~> 0.2)
105
+ rspec (~> 2.0)
106
+ ruby-debug19 (~> 0.11.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Gnomeslab, Lda.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ ## Introduction
2
+
3
+ ActiveRecord Random is a Ruby Gem that empowers your ActiveRecord Models with the ability to return a random record without using SQL RAND().
4
+
5
+ ## Compatibility
6
+
7
+ Developed to be compatible with
8
+
9
+ * **Rails:** v3.0.x
10
+ * **Ruby:** v1.8.7, v1.9.1 or v1.9.2
11
+
12
+ ## Installation
13
+
14
+ You can install ActiveRecord Random straight from Rubygems. Thanks to Bundler you just need to add this to your Gemfile
15
+
16
+ <pre>
17
+ gem 'activerecord_random'
18
+ </pre>
19
+
20
+ ## Examples
21
+
22
+ Scenario: You have a collection of Questions (model name = Question) and you wish to randomly select one of them.
23
+
24
+ <pre>
25
+ Question.random #=> random random instance of Question
26
+ </pre>
27
+
28
+ Alternatively you can use the :random finder:
29
+
30
+ <pre>
31
+ Question.find(:random) #=> random instance of Question
32
+ </pre>
33
+
34
+ That simple
35
+
36
+ ## Documentation
37
+
38
+ This gem's documentation documentation is available at **TODO**
39
+
40
+ ## License
41
+
42
+ Copyright (c) 2010 Gnomeslab, Lda., released under the MIT license. For more information regarding MIT license, please check our [MIT license file](http://github.com/GnomesLab/activerecord_random/blob/master/MIT-LICENSE)
43
+
44
+ ## Feedback, issues and contributions
45
+
46
+ If you have an issue with this Gem please create a ticket in our [issue tracker](http://gnomeslab.lighthouseapp.com/projects/63976-activerecord_random/overview).
47
+
48
+ Feel free to fork this project at any time and submit your changes (along with their respective tests).
49
+
50
+ Should you just wish to provide feedback or say hi, you can always contact us directly through diogo (dot) almeida (at) gnomeslab (dot) com
@@ -0,0 +1,38 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+ require 'rake/gempackagetask'
4
+
5
+ desc 'Generate documentation for the ActiveRecord Random Ruby Gem'
6
+ Rake::RDocTask.new(:rdoc) do |rdoc|
7
+ rdoc.rdoc_dir = 'rdoc'
8
+ rdoc.title = 'ActiveRecord Random'
9
+ rdoc.options << '--line-numbers' << '--inline-source'
10
+ rdoc.rdoc_files.include('README.md')
11
+ rdoc.rdoc_files.include('lib/**/*.rb')
12
+ end
13
+
14
+ begin
15
+ require 'rspec/core/rake_task'
16
+
17
+ RSpec::Core::RakeTask.new(:spec) do |t|
18
+ t.ruby_opts = '-w'
19
+ end
20
+
21
+ task :default => :spec
22
+ rescue LoadError
23
+ raise 'RSpec could not be loaded. Run `bundle install` to get all development dependencies.'
24
+ end
25
+
26
+ # Rubygems
27
+ namespace :rubygems do
28
+ gemspec = eval(File.read('activerecord_random.gemspec'))
29
+ Rake::GemPackageTask.new(gemspec) do |pkg|
30
+ pkg.gem_spec = gemspec
31
+ end
32
+
33
+ desc "build the gem and release it to rubygems.org"
34
+ task :release => :gem do
35
+ sh "rake spec"
36
+ sh "gem push pkg/activerecord_random-#{gemspec.version}.gem"
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveRecord
2
+
3
+ module Random #:nodoc:
4
+
5
+ # ActiveRecord::Base.random returns a random instance of self.
6
+ #
7
+ # This method does not use SQL RAND(). Instead, it performs a record count and then uses Ruby rand.
8
+ #
9
+ # Example
10
+ # Question.random #=> Random instance of random
11
+ #
12
+ # ActiveRecord::Base.random is a safe method, thus when your model's table is empty it will simply return nil.
13
+ def random
14
+ where("id >= ?", rand(self.count + 1)).first
15
+ end
16
+
17
+ end # Random
18
+
19
+ end # ActiveRecord
20
+
21
+ ActiveRecord::Base.send :extend, ActiveRecord::Random
@@ -0,0 +1,11 @@
1
+ module ActiveRecord
2
+
3
+ module FinderMethods
4
+
5
+ def find(*args)
6
+ args == [:random] ? self.random : super(args)
7
+ end
8
+
9
+ end # FinderMethods
10
+
11
+ end # ActiveRecord
@@ -0,0 +1,2 @@
1
+ class Question < ActiveRecord::Base
2
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define(:version => 20101111160738) do
2
+
3
+ create_table "questions", :force => true do |t|
4
+ t.string "title", :null => false
5
+ t.datetime "created_at"
6
+ t.datetime "updated_at"
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ Factory.define(:question) do |q|
2
+ q.sequence(:title) { |n| "Question ##{n}" }
3
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::Random do
4
+
5
+ describe "Class Methods" do
6
+
7
+ it "returns a random instance of the invoked ActiveRecord Model" do
8
+ 10.times { |n| Factory.create(:question) }
9
+ Question.random.should be_an_instance_of Question
10
+ end
11
+
12
+ it "returns nil when no records are found" do
13
+ Question.random.should be_nil
14
+ end
15
+
16
+ end # Class Methods
17
+
18
+ describe "Random finder" do
19
+
20
+ it "Question.find(:random) returns a random instance of Question" do
21
+ 10.times { |n| Factory.create(:question) }
22
+ Question.find(:random).should be_an_instance_of Question
23
+ end
24
+
25
+ it "also returns nil when no records are found by the random finder" do
26
+ Question.find(:random).should be_nil
27
+ end
28
+
29
+ end # Random finder
30
+
31
+ end # ActiveRecord::Random
@@ -0,0 +1,72 @@
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
+
4
+ begin
5
+ require 'erb'
6
+ require 'init'
7
+ require 'logger'
8
+ require 'rspec'
9
+ require 'active_record'
10
+ require 'database_cleaner'
11
+ require 'factory_girl'
12
+
13
+ RSpec.configure do |config|
14
+ config.debug = true
15
+ # == Mock Framework
16
+ config.mock_with :rspec
17
+
18
+ # database_cleaner
19
+ config.before(:suite) do
20
+ DatabaseCleaner.strategy = :transaction
21
+ DatabaseCleaner.clean_with(:truncation)
22
+ end
23
+
24
+ config.before(:each) do
25
+ DatabaseCleaner.start
26
+ end
27
+
28
+ config.after(:each) do
29
+ DatabaseCleaner.clean
30
+ end
31
+ end
32
+ rescue LoadError => load_error
33
+ puts "Please run bundle install"
34
+ rescue StandardError => e
35
+ puts "Something went wrong while loading the environment."
36
+ throw e
37
+ end
38
+
39
+ # Requires supporting files with custom matchers and macros, etc, in ./support/ and its subdirectories.
40
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
41
+
42
+ # Requires all supporting factories
43
+ Dir.glob(File.join(File.dirname(__FILE__), 'factories', '*_factory.rb')).each { |f| require f }
44
+
45
+ # ActiveRecord Test database configurations
46
+ database_yml_file = File.expand_path('../config/database.yml', __FILE__)
47
+
48
+ begin
49
+ if File.exists?(database_yml_file)
50
+ active_record_configuration = YAML.load_file(database_yml_file)['test']
51
+ ActiveRecord::Base.establish_connection(active_record_configuration)
52
+
53
+ File.truncate(File.join(File.dirname(__FILE__), 'log', 'test.log'), 0)
54
+
55
+ ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), 'log', 'test.log'))
56
+
57
+ ActiveRecord::Base.silence do
58
+ ActiveRecord::Migration.verbose = false
59
+
60
+ load File.join(File.dirname(__FILE__), 'db', 'schema.rb')
61
+ load File.join(File.dirname(__FILE__), 'db' , 'models.rb')
62
+ end
63
+
64
+ # The config/database.yml is in the spec folder
65
+ DatabaseCleaner.app_root = File.expand_path('spec')
66
+ else
67
+ raise "Please create #{database_yml} first. Take a look at the database.sample.yml in the config folder."
68
+ end
69
+ rescue StandardError => e
70
+ puts "Something went wrong while attempting to load your database file, or while configuring ActiveRecord."
71
+ throw e
72
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_random
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ - rc
10
+ version: 1.0.0.rc
11
+ platform: ruby
12
+ authors:
13
+ - Gnomeslab, Lda.
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-12 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 1
33
+ version: 3.0.1
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: mysql2
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 2
46
+ - 0
47
+ version: "2.0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: ruby-debug19
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ - 11
61
+ - 0
62
+ version: 0.11.0
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: rspec
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 2
75
+ - 0
76
+ version: "2.0"
77
+ type: :development
78
+ version_requirements: *id004
79
+ - !ruby/object:Gem::Dependency
80
+ name: factory_girl_rails
81
+ prerelease: false
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 1
89
+ - 0
90
+ - 0
91
+ version: 1.0.0
92
+ type: :development
93
+ version_requirements: *id005
94
+ - !ruby/object:Gem::Dependency
95
+ name: database_cleaner
96
+ prerelease: false
97
+ requirement: &id006 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ segments:
103
+ - 0
104
+ - 6
105
+ - 0
106
+ version: 0.6.0
107
+ type: :development
108
+ version_requirements: *id006
109
+ description: ActiveRecord Random is a Ruby Gem that empowers your ActiveRecord Models with the ability to return a random record without using SQL RAND().
110
+ email:
111
+ - mail@gnomeslab.com
112
+ executables: []
113
+
114
+ extensions: []
115
+
116
+ extra_rdoc_files: []
117
+
118
+ files:
119
+ - lib/activerecord_random.rb
120
+ - lib/ext/active_record/finder_methods/random.rb
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - MIT-LICENSE
124
+ - Rakefile
125
+ - README.md
126
+ - spec/db/models.rb
127
+ - spec/db/schema.rb
128
+ - spec/factories/questions_factory.rb
129
+ - spec/lib/activerecord_random_spec.rb
130
+ - spec/spec_helper.rb
131
+ has_rdoc: true
132
+ homepage: http://github.com/GnomesLab/activerecord_random/
133
+ licenses: []
134
+
135
+ post_install_message:
136
+ rdoc_options: []
137
+
138
+ require_paths:
139
+ - lib
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ segments:
155
+ - 1
156
+ - 3
157
+ - 7
158
+ version: 1.3.7
159
+ requirements: []
160
+
161
+ rubyforge_project:
162
+ rubygems_version: 1.3.7
163
+ signing_key:
164
+ specification_version: 3
165
+ summary: Returns a random instance of an ActiveRecord Model
166
+ test_files: []
167
+