rankitize 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +26 -0
- data/LICENSE +20 -0
- data/README.md +32 -0
- data/Rakefile +5 -0
- data/lib/rankitize.rb +3 -0
- data/lib/rankitize/model_additions.rb +16 -0
- data/lib/rankitize/railtie.rb +9 -0
- data/lib/rankitize/version.rb +3 -0
- data/old/LICENSE +20 -0
- data/old/README.rdoc +26 -0
- data/old/Rakefile +23 -0
- data/old/init.rb +5 -0
- data/old/lib/rankitize.rb +12 -0
- data/old/tasks/rankitize_tasks.rake +4 -0
- data/old/test/rankitize_test.rb +8 -0
- data/old/test/test_helper.rb +3 -0
- data/pkg/scoped_traversal-0.1.gem +0 -0
- data/rankitize.gemspec +24 -0
- data/spec/rankitize/model_additions_spec.rb +6 -0
- data/spec/spec_helper.rb +1 -0
- metadata +99 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.DS_Store
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rankitize (0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rake (0.9.2.2)
|
11
|
+
rspec (2.8.0)
|
12
|
+
rspec-core (~> 2.8.0)
|
13
|
+
rspec-expectations (~> 2.8.0)
|
14
|
+
rspec-mocks (~> 2.8.0)
|
15
|
+
rspec-core (2.8.0)
|
16
|
+
rspec-expectations (2.8.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.8.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
rake
|
25
|
+
rankitize!
|
26
|
+
rspec
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Wil Gieseler
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Rankitize
|
2
|
+
|
3
|
+
A quick and easy plugin to allow models to find their rank for a particular attribute.
|
4
|
+
|
5
|
+
For example, if you want to find the ranking of a particular user based on their score. ("You are #22 on the high score list!")
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add to your Gemfile and run the `bundle` command to install it.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "rankitize"
|
13
|
+
```
|
14
|
+
|
15
|
+
**Requires Ruby 1.9.2 or later.**
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
In your model, call rank_by and pass in a column name.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class User < ActiveRecord::Base
|
23
|
+
rank_by :score
|
24
|
+
end
|
25
|
+
```
|
26
|
+
You can then find the rank by calling {column name}_ranking.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
@user.score_ranking
|
30
|
+
```
|
31
|
+
|
32
|
+
This gem is made to work with MySQL, which does not have a built in ranking function. It uses the technique mentioned here: http://arjen-lentz.livejournal.com/56292.html
|
data/Rakefile
ADDED
data/lib/rankitize.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Rankitize
|
2
|
+
module ModelAdditions
|
3
|
+
|
4
|
+
def rank_by(*columns)
|
5
|
+
# Loop through the arguments.
|
6
|
+
columns.each do |column|
|
7
|
+
# Define the method to get the ranking.
|
8
|
+
# This is a workaround meant for mysql
|
9
|
+
define_method "#{column}_ranking" do
|
10
|
+
connection.select_value("SELECT COUNT(*) + 1 AS ranking FROM #{self.class.table_name} WHERE #{column} > (SELECT #{column} FROM #{self.class.table_name} WHERE id = #{self.id})").to_i
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
data/old/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Wil Gieseler
|
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.
|
data/old/README.rdoc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
= Rankitize
|
2
|
+
|
3
|
+
A quick and easy plugin to allow models to find their rank for a particular attribute.
|
4
|
+
|
5
|
+
For example, if you want to find the ranking of a particular user based on their score. ("You are #22 on the high score list!")
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
You can install this as a plugin into your Rails app.
|
10
|
+
|
11
|
+
script/plugin install git://github.com/supapuerco/rankitize.git
|
12
|
+
|
13
|
+
|
14
|
+
== Usage
|
15
|
+
|
16
|
+
In your model, call rank_by and pass in a column name.
|
17
|
+
|
18
|
+
class User < ActiveRecord::Base
|
19
|
+
rank_by :score
|
20
|
+
end
|
21
|
+
|
22
|
+
You can then find the rank by calling {column name}_ranking.
|
23
|
+
|
24
|
+
@user.score_ranking
|
25
|
+
|
26
|
+
This plugin is made to work with MySQL, which does not have a built in ranking function. It uses the technique mentioned here: http://arjen-lentz.livejournal.com/56292.html
|
data/old/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the rankitize plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the rankitize plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Rankitize'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
data/old/init.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module Rankitize
|
2
|
+
def rank_by(*columns)
|
3
|
+
# Loop through the arguments.
|
4
|
+
columns.each do |column|
|
5
|
+
# Define the method to get the ranking.
|
6
|
+
# This is a workaround meant for mysql
|
7
|
+
define_method "#{column}_ranking" do
|
8
|
+
connection.select_value("SELECT COUNT(*) + 1 AS ranking FROM #{self.class.table_name} WHERE #{column} > (SELECT #{column} FROM #{self.class.table_name} WHERE id = #{self.id})").to_i
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
Binary file
|
data/rankitize.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rankitize/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rankitize"
|
7
|
+
s.version = Rankitize::VERSION
|
8
|
+
s.authors = ["Wil Gieseler"]
|
9
|
+
s.email = ["supapuerco@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/supapuerco/rankitize"
|
11
|
+
s.summary = %q{Gem to allow ActiveRecord models to find their ranking in a list.}
|
12
|
+
s.description = %q{Gem to allow ActiveRecord models to find their ranking in a list.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "rankitize"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "rspec"
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rankitize'
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rankitize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "0.1"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Wil Gieseler
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-01-21 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: Gem to allow ActiveRecord models to find their ranking in a list.
|
38
|
+
email:
|
39
|
+
- supapuerco@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- CHANGELOG.md
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/rankitize.rb
|
55
|
+
- lib/rankitize/model_additions.rb
|
56
|
+
- lib/rankitize/railtie.rb
|
57
|
+
- lib/rankitize/version.rb
|
58
|
+
- old/LICENSE
|
59
|
+
- old/README.rdoc
|
60
|
+
- old/Rakefile
|
61
|
+
- old/init.rb
|
62
|
+
- old/lib/rankitize.rb
|
63
|
+
- old/tasks/rankitize_tasks.rake
|
64
|
+
- old/test/rankitize_test.rb
|
65
|
+
- old/test/test_helper.rb
|
66
|
+
- pkg/scoped_traversal-0.1.gem
|
67
|
+
- rankitize.gemspec
|
68
|
+
- spec/rankitize/model_additions_spec.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
homepage: https://github.com/supapuerco/rankitize
|
71
|
+
licenses: []
|
72
|
+
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project: rankitize
|
93
|
+
rubygems_version: 1.8.15
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Gem to allow ActiveRecord models to find their ranking in a list.
|
97
|
+
test_files:
|
98
|
+
- spec/rankitize/model_additions_spec.rb
|
99
|
+
- spec/spec_helper.rb
|