rankitize 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .DS_Store
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ ## v0.1
2
+
3
+ * Initial release
4
+ * No tests! Sorry. with_model wasn't working and I am lazy.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rankitize.gemspec
4
+ gemspec
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
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
data/lib/rankitize.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "rankitize/version"
2
+ require "rankitize/model_additions"
3
+ require "rankitize/railtie" if defined? Rails
@@ -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
@@ -0,0 +1,9 @@
1
+ module Rankitize
2
+ class Railtie < Rails::Railtie
3
+ initializer 'rankitize.model_additions' do
4
+ ActiveSupport.on_load :active_record do
5
+ extend ModelAdditions
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Rankitize
2
+ VERSION = "0.1"
3
+ 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,5 @@
1
+ require 'rankitize'
2
+
3
+ class ActiveRecord::Base
4
+ extend Rankitize
5
+ end
@@ -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
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rankitize do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class RankitizeTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
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
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Rankitize::ModelAdditions do
5
+
6
+ end
@@ -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