sorted 0.3.9 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +5 -92
- data/lib/sorted/orms/active_record.rb +4 -6
- data/lib/sorted/railtie.rb +8 -6
- data/lib/sorted/version.rb +1 -1
- data/sorted.gemspec +1 -0
- data/spec/sorted/orms/active_record_spec.rb +19 -3
- metadata +20 -9
data/README.rdoc
CHANGED
@@ -5,105 +5,18 @@ sort string to let you sort large datasets over many pages (using
|
|
5
5
|
{will_paginate}[http://github.com/mislav/will_paginatea] or
|
6
6
|
{kaminari}[https://github.com/amatsuda/kaminari]) without loosing state.
|
7
7
|
|
8
|
-
If you would like to see it in action clone the
|
9
|
-
{example app}[https://github.com/mynameisrufus/sorted_app]
|
10
|
-
|
11
8
|
=== Gemfile
|
12
9
|
|
13
|
-
gem 'sorted' '~> 0.
|
10
|
+
gem 'sorted', '~> 0.4.0'
|
14
11
|
|
15
12
|
=== View
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
This will generate a link with a url like this:
|
20
|
-
|
21
|
-
http://myapp/users?sort=email_asc
|
14
|
+
Generate a sorted link with the email attribute:
|
22
15
|
|
23
|
-
|
24
|
-
|
25
|
-
http://myapp/users?sort=name_asc!email_asc
|
16
|
+
link_to_sorted "Email", :email
|
26
17
|
|
27
18
|
=== Model
|
28
19
|
|
29
|
-
|
30
|
-
default order argument:
|
31
|
-
|
32
|
-
@users = User.sort(params[:sort], "email ASC").page(params[:page])
|
33
|
-
|
34
|
-
==== Joins and includes
|
35
|
-
|
36
|
-
If you want to sort by a belongs to relationship add the table name to
|
37
|
-
the order argument. For example assuming a user belongs to a company:
|
38
|
-
|
39
|
-
@users = User.joins(:company).sort(params[:sort], "companies.name ASC").page(params[:page])
|
40
|
-
|
41
|
-
When generating links using the +link_to_sorted+ method you should
|
42
|
-
specify the table for every attribute you use as well otherwise you
|
43
|
-
will probably get an ambiguous column name error.
|
44
|
-
|
45
|
-
<th class="ui-state-default"><%= link_to_sorted "Company name", 'companies.name' %></th>
|
46
|
-
<th class="ui-state-default"><%= link_to_sorted "Users name", 'users.name' %></th>
|
47
|
-
|
48
|
-
==== Typecasting and DB functions
|
49
|
-
|
50
|
-
Do your type casting and function calls in your select statement with an
|
51
|
-
alias, for example:
|
52
|
-
|
53
|
-
# controller
|
54
|
-
@users = User.select("income::BigInt as bg_income").sort(params[:sort], "bg_income ASC")
|
55
|
-
|
56
|
-
# view
|
57
|
-
<th class="ui-state-default"><%= link_to_sorted "Income", 'bg_income' %></th>
|
58
|
-
|
59
|
-
or using a DB function:
|
60
|
-
|
61
|
-
# controller
|
62
|
-
@users = User.select("inet_aton(`ip_address`) AS in_ip").sort(params[:sort], "in_ip ASC")
|
63
|
-
|
64
|
-
# view
|
65
|
-
<th class="ui-state-default"><%= link_to_sorted "IP address", 'in_ip' %></th>
|
66
|
-
|
67
|
-
== Presentation
|
68
|
-
|
69
|
-
You might want to roll your own +link_to_sorted+ method to use jQuery ui
|
70
|
-
css classes for example, all you need is the sorted object.
|
71
|
-
|
72
|
-
def link_to_sorted(name, order)
|
73
|
-
dup_params = (request.get? && !params.nil?) ? params.dup : nil
|
74
|
-
sorter = ActionView::Base::SortedViewHelper.new(order, dup_params)
|
75
|
-
css_class = case sorter.css
|
76
|
-
when "sorted asc"
|
77
|
-
"ui-icon ui-icon-triangle-1-n"
|
78
|
-
when "sorted desc"
|
79
|
-
"ui-icon ui-icon-triangle-1-s"
|
80
|
-
when "sorted"
|
81
|
-
"ui-icon ui-icon-carat-2-n-s"
|
82
|
-
end
|
83
|
-
link_to(content_tag(:span, nil, {:class => css_class}) + name.to_s, sorter.params)
|
84
|
-
end
|
85
|
-
|
86
|
-
Tables are best displayed with alternating shades for each row, so add
|
87
|
-
an alternating class to you table rows using the rails +cycle+ method:
|
88
|
-
|
89
|
-
<tr class="<%= cycle 'odd', 'even' %>">
|
90
|
-
|
91
|
-
== Rails 2.3.x
|
92
|
-
|
93
|
-
Sorted works with rails 2.3.x but you will have to roll your own scope
|
94
|
-
and view helper.
|
95
|
-
|
96
|
-
Here is the named scope for your model(s):
|
97
|
-
|
98
|
-
named_scope :sort, lambda { |sort, order|
|
99
|
-
{ :order => Sorted::Parser.new(sort, order).to_sql }
|
100
|
-
}
|
101
|
-
|
102
|
-
and the application helper method:
|
20
|
+
Using the +sort+ method with the optional default order argument:
|
103
21
|
|
104
|
-
|
105
|
-
dup_params = (request.get? && !params.nil?) ? params.dup : nil
|
106
|
-
sorter = Sorted::ViewHelpers::ActionView::SortedViewHelper.new(order, dup_params)
|
107
|
-
options[:class] = [options[:class], sorter.css].join(' ').strip
|
108
|
-
link_to(name.to_s, sorter.params, options)
|
109
|
-
end
|
22
|
+
@users = User.sorted(params[:sort], "email ASC").page(params[:page])
|
@@ -4,12 +4,10 @@ require 'sorted'
|
|
4
4
|
module Sorted
|
5
5
|
module Orms
|
6
6
|
module ActiveRecord
|
7
|
-
def self.
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
order sorter.to_sql
|
12
|
-
end
|
7
|
+
def self.included(base)
|
8
|
+
def base.sorted(sort, default_order = nil)
|
9
|
+
sorter = ::Sorted::Parser.new(sort, default_order)
|
10
|
+
order sorter.to_sql
|
13
11
|
end
|
14
12
|
end
|
15
13
|
end
|
data/lib/sorted/railtie.rb
CHANGED
@@ -2,16 +2,18 @@ require 'sorted'
|
|
2
2
|
|
3
3
|
module Sorted
|
4
4
|
class Railtie < Rails::Railtie
|
5
|
-
|
6
|
-
|
5
|
+
if defined? ::ActiveRecord
|
6
|
+
initializer "sorted.active_record" do |app|
|
7
7
|
require 'sorted/orms/active_record'
|
8
|
-
Sorted::Orms::ActiveRecord
|
8
|
+
::ActiveRecord::Base.send(:include, Sorted::Orms::ActiveRecord)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
if defined? ::ActiveRecord
|
13
|
+
initializer "sorted.action_view" do |app|
|
14
|
+
require 'sorted/view_helpers/action_view'
|
15
|
+
::ActionView::Base.send(:include, Sorted::ViewHelpers::ActionView)
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
data/lib/sorted/version.rb
CHANGED
data/sorted.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.add_development_dependency "bundler", ">= 1.0.0"
|
18
18
|
s.add_development_dependency "rails", ">= 3.1.2"
|
19
19
|
s.add_development_dependency "rspec", ">= 2.0.0"
|
20
|
+
s.add_development_dependency "sqlite3", ">= 1.3.5"
|
20
21
|
|
21
22
|
s.files = `git ls-files`.split("\n")
|
22
23
|
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
@@ -1,11 +1,27 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Sorted::Orms::ActiveRecord do
|
4
|
-
|
5
|
-
|
4
|
+
ActiveRecord::Base.send(:include, Sorted::Orms::ActiveRecord)
|
5
|
+
|
6
|
+
class SortedActiveRecordTest < ActiveRecord::Base
|
7
|
+
establish_connection :adapter => 'sqlite3', :database => '/tmp/foobar.db'
|
8
|
+
|
9
|
+
connection.create_table table_name, :force => true do |t|
|
10
|
+
t.string :name
|
11
|
+
end
|
12
|
+
|
13
|
+
scope :page, lambda {
|
14
|
+
limit(50)
|
15
|
+
}
|
6
16
|
end
|
7
17
|
|
8
18
|
it "should integrate with ActiveRecord::Base" do
|
9
|
-
|
19
|
+
SortedActiveRecordTest.should respond_to(:sorted)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should play nice with other scopes" do
|
23
|
+
sql = "SELECT \"sorted_active_record_tests\".* FROM \"sorted_active_record_tests\" WHERE \"sorted_active_record_tests\".\"name\" = 'bob' ORDER BY name ASC LIMIT 50"
|
24
|
+
SortedActiveRecordTest.where(:name => 'bob').page.sorted(nil, 'name ASC').to_sql.should == sql
|
25
|
+
SortedActiveRecordTest.page.sorted(nil, 'name ASC').where(:name => 'bob').to_sql.should == sql
|
10
26
|
end
|
11
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sorted
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &70297045252360 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.0.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70297045252360
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rails
|
27
|
-
requirement: &
|
27
|
+
requirement: &70297045251840 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 3.1.2
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70297045251840
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70297045251140 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,18 @@ dependencies:
|
|
43
43
|
version: 2.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70297045251140
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3
|
49
|
+
requirement: &70297045247200 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.3.5
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70297045247200
|
47
58
|
description: lets you sort large data sets using view helpers and a scope
|
48
59
|
email:
|
49
60
|
- rufuspost@gmail.com
|
@@ -84,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
95
|
version: '0'
|
85
96
|
segments:
|
86
97
|
- 0
|
87
|
-
hash: -
|
98
|
+
hash: -603896101193403601
|
88
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
100
|
none: false
|
90
101
|
requirements:
|