sorted 0.2.1
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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +19 -0
- data/VERSION +1 -0
- data/lib/sorted.rb +6 -0
- data/lib/sorted/action_view.rb +62 -0
- data/lib/sorted/active_record.rb +18 -0
- data/lib/sorted/railtie.rb +15 -0
- data/sorted.gemspec +57 -0
- data/spec/sorted_spec.rb +58 -0
- data/spec/spec_helper.rb +6 -0
- metadata +92 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Rufus Post
|
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.rdoc
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
= sorted
|
2
|
+
|
3
|
+
This wee gem lets you sort tables (or somthing else) using a view helper and a scope.
|
4
|
+
|
5
|
+
It will toggle your sort order for you and will even work across pages with {will_paginate}[http://github.com/mislav/will_paginate]
|
6
|
+
|
7
|
+
== Example
|
8
|
+
|
9
|
+
The view:
|
10
|
+
|
11
|
+
link_to_sorted "Email", :email
|
12
|
+
|
13
|
+
The model:
|
14
|
+
|
15
|
+
@users = User.sorted(params[:order]).paginate(:page => params[:page])
|
16
|
+
|
17
|
+
This will make a url like this:
|
18
|
+
|
19
|
+
http://myapp/users?order=email_asc
|
20
|
+
|
21
|
+
Or this when you then sort by somthing else....
|
22
|
+
|
23
|
+
http://myapp/users?order=name_asc|email_desc
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "sorted"
|
8
|
+
gem.summary = %Q{sort a table}
|
9
|
+
gem.description = %Q{lets you sort tables (or somthing else) using a view helper and a custom scope}
|
10
|
+
gem.email = "rufuspost@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/mynameisrufus/sorted"
|
12
|
+
gem.authors = ["Rufus Post"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
data/lib/sorted.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
|
3
|
+
module Sorted
|
4
|
+
module ActionView
|
5
|
+
def sorted_params(attribute)
|
6
|
+
get_params = request.get? ? params.dup : {:order => nil}
|
7
|
+
get_params[:order] = sorted_order(get_params[:order], attribute)
|
8
|
+
get_params
|
9
|
+
end
|
10
|
+
|
11
|
+
def link_to_sorted(name, attribute, options = nil)
|
12
|
+
@sorted_params = sorted_params(attribute)
|
13
|
+
if options.nil?
|
14
|
+
options = {:class => sorted_css_class}
|
15
|
+
elsif options[:class].nil?
|
16
|
+
options.merge(:class => sorted_css_class)
|
17
|
+
else
|
18
|
+
options[:class] = "#{options[:class]} #{sorted_css_class}"
|
19
|
+
end
|
20
|
+
link_to(name, @sorted_params.merge(:order => sorted_to_string), options)
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def sorted_css_class
|
26
|
+
"sorted-#{@sorted_params[:order].first[1]}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def sorted_order(sort_string, attribute, order_rest = {})
|
30
|
+
order_first = {attribute => 'asc'}
|
31
|
+
if sort_string.class == String
|
32
|
+
sort_string.split(/!/).each do |order|
|
33
|
+
order.match(/(\w+)_(asc|desc)/) do |match_data|
|
34
|
+
if match_data[1] == attribute.to_s
|
35
|
+
order_first = {attribute => sorted_sql_reverse(match_data[2])}
|
36
|
+
else
|
37
|
+
order_rest = order_rest.merge(match_data[1] => match_data[2])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
order_first.merge(order_rest).symbolize_keys
|
43
|
+
end
|
44
|
+
|
45
|
+
def sorted_to_string
|
46
|
+
@sorted_params[:order].map do |order|
|
47
|
+
order.join('_')
|
48
|
+
end.join('!')
|
49
|
+
end
|
50
|
+
|
51
|
+
def sorted_sql_reverse(order)
|
52
|
+
case order
|
53
|
+
when 'asc'
|
54
|
+
'desc'
|
55
|
+
when 'desc'
|
56
|
+
'asc'
|
57
|
+
else
|
58
|
+
'asc'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module Sorted
|
4
|
+
module ActiveRecord
|
5
|
+
def self.enable!
|
6
|
+
::ActiveRecord::Base.class_eval do
|
7
|
+
scope :sorted, lambda{|params|
|
8
|
+
return if params.nil?
|
9
|
+
order = []
|
10
|
+
params.split(/!/).each do |param|
|
11
|
+
order << param.gsub(/_asc/, ' ASC').gsub(/_desc/, ' DESC')
|
12
|
+
end
|
13
|
+
{:order => order.join(', ')}
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'sorted'
|
2
|
+
|
3
|
+
module Sorted
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer "sorted.active_record" do |app|
|
6
|
+
require 'sorted/active_record'
|
7
|
+
Sorted::ActiveRecord.enable!
|
8
|
+
end
|
9
|
+
|
10
|
+
initializer "sorted.action_view" do |app|
|
11
|
+
require 'sorted/action_view'
|
12
|
+
::ActionView::Base.send(:include, Sorted::ActionView)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/sorted.gemspec
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{sorted}
|
8
|
+
s.version = "0.2.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Rufus Post"]
|
12
|
+
s.date = %q{2010-07-19}
|
13
|
+
s.description = %q{lets you sort tables (or somthing else) using a view helper and a custom scope}
|
14
|
+
s.email = %q{rufuspost@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/sorted.rb",
|
27
|
+
"lib/sorted/action_view.rb",
|
28
|
+
"lib/sorted/active_record.rb",
|
29
|
+
"lib/sorted/railtie.rb",
|
30
|
+
"sorted.gemspec",
|
31
|
+
"spec/sorted_spec.rb",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/mynameisrufus/sorted}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{sort a table}
|
39
|
+
s.test_files = [
|
40
|
+
"spec/spec_helper.rb",
|
41
|
+
"spec/sorted_spec.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
data/spec/sorted_spec.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'action_controller'
|
3
|
+
|
4
|
+
describe Sorted::ActiveRecord do
|
5
|
+
before(:each) do
|
6
|
+
Sorted::ActiveRecord.enable!
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should integrate with ActiveRecord::Base" do
|
10
|
+
ActiveRecord::Base.should respond_to(:sorted)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe Sorted::ActionView do
|
15
|
+
before(:each) do
|
16
|
+
class TestController
|
17
|
+
def params
|
18
|
+
@params ||= {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def params=(params)
|
22
|
+
@params = params
|
23
|
+
end
|
24
|
+
|
25
|
+
def request
|
26
|
+
Request.new
|
27
|
+
end
|
28
|
+
end
|
29
|
+
class Request
|
30
|
+
def get?; true end
|
31
|
+
end
|
32
|
+
@controller = TestController.new
|
33
|
+
ActionView::Base.send(:include, Sorted::ActionView)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should integrate with ActionView::Base" do
|
37
|
+
ActionView::Base.new.should respond_to(:sorted_params)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should return a hash for url" do
|
41
|
+
@controller.params = {:order => "name_desc"}
|
42
|
+
ActionView::Base.new([], {}, @controller).sorted_params(:email).should == {:order => {:email => "asc", :name => "desc"}}
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should return a hash for url" do
|
46
|
+
@controller.params = {:order => "email_asc!name_desc"}
|
47
|
+
ActionView::Base.new([], {}, @controller).sorted_params(:email).should == {:order => {:email => "desc", :name => "desc"}}
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return a query string for link options" do
|
51
|
+
@controller.params = {:order => "email_asc!name_desc", :page => 2}
|
52
|
+
ActionView::Base.new([], {}, @controller).sorted_params(:email).should == {:order => {:email => "desc", :name => "desc"}, :page => 2}
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should not care if params[:order] nil" do
|
56
|
+
ActionView::Base.new([], {}, @controller).sorted_params({:email => "desc", :name => "desc"}).should_not be_nil
|
57
|
+
end
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sorted
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Rufus Post
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-19 00:00:00 +10:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 2
|
31
|
+
- 9
|
32
|
+
version: 1.2.9
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: lets you sort tables (or somthing else) using a view helper and a custom scope
|
36
|
+
email: rufuspost@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/sorted.rb
|
52
|
+
- lib/sorted/action_view.rb
|
53
|
+
- lib/sorted/active_record.rb
|
54
|
+
- lib/sorted/railtie.rb
|
55
|
+
- sorted.gemspec
|
56
|
+
- spec/sorted_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/mynameisrufus/sorted
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --charset=UTF-8
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.3.7
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: sort a table
|
90
|
+
test_files:
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
- spec/sorted_spec.rb
|