dancroak-sortable_table 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Dan Croak
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.textile ADDED
@@ -0,0 +1,136 @@
1
+ h1. Sortable Table
2
+
3
+ Sort HTML tables in a Rails app.
4
+
5
+ h2. Install
6
+
7
+ script/plugin install git://github.com/dancroak/sortable_table.git
8
+
9
+ In app/controllers/application_controller.rb:
10
+
11
+ class ApplicationController < ActionController::Base
12
+ include SortableTable::App::Controllers::ApplicationController
13
+ end
14
+
15
+ In app/helpers/application_helper.rb:
16
+
17
+ module ApplicationHelper
18
+ include SortableTable::App::Helpers::ApplicationHelper
19
+ end
20
+
21
+ h2. Testing
22
+
23
+ context "enough Users to sort" do
24
+ setup do
25
+ 5.times { Factory :user }
26
+ end
27
+
28
+ should_sort_by_attributes :name, :email, :age, :group => "groups.name"
29
+
30
+ context "GET to #index" do
31
+ setup { get :index }
32
+
33
+ should_display_sortable_table_header_for :name, :email, :age, :group
34
+ end
35
+ end
36
+
37
+ This is the common case for a RESTful UsersController.
38
+
39
+ * should_sort_by_attributes tests that the controller's index action can sort by the attributes.
40
+ * should_display_sortable_header_for tests that a sortable header displays for the attributes.
41
+
42
+ h2. Controller
43
+
44
+ class UsersController < Admin::BaseController
45
+ sortable_attributes :name, :email, :age, :group => "groups.name"
46
+
47
+ def index
48
+ @users = User.paginate :page => params[:page], :order => sort_order
49
+ end
50
+ end
51
+
52
+ sortable_attributes defines a sort_order method that gets called in your action.
53
+
54
+ If the index action is rendered without a params[:sort] option, @users will be sorted by :name, the first option in the list of sortable_attributes.
55
+
56
+ h2. View
57
+
58
+ <pre><code>
59
+ <h1>Users</h1>
60
+ <table>
61
+ <tr>
62
+ <%= sortable_table_header :name => "Name", :sort => "name" %>
63
+ <%= sortable_table_header :name => "Email", :sort => "email" %>
64
+ <%= sortable_table_header :name => "Age", :sort => "age" %>
65
+ <%= sortable_table_header :name => "Group", :sort => "group" %>
66
+ </tr>
67
+ <% @users.each do |user| %>
68
+ <tr>
69
+ <td><%= html_escape(user.name) %></td>
70
+ <td><%= html_escape(user.email) %></td>
71
+ <td><%= html_escape(user.age) %></td>
72
+ <td><%= html_escape(user.group.name) %></td>
73
+ </tr>
74
+ <% end %>
75
+ </table>
76
+ </code></pre>
77
+
78
+ sortable_table_header creates a table header containing a link with the correct :sort and :order params. It also has a class of "ascending" or "descending" so you can add styles with arrows.
79
+
80
+ h2. Example styles
81
+
82
+ th.ascending a {
83
+ background: url(/images/sort-ascending-arrow.gif) 0% 50% no-repeat;
84
+ padding-left: 15px;
85
+ }
86
+
87
+ th.descending a {
88
+ background: url(/images/sort-descending-arrow.gif) 0% 50% no-repeat;
89
+ padding-left: 15px;
90
+ }
91
+
92
+ h2. Overriding defaults
93
+
94
+ h3. should_sort_by_attributes
95
+
96
+ Opinionated defaults:
97
+
98
+ * GET to :index
99
+ * collection same name as controller (@users for UsersController)
100
+ * model name same name as controller (User for UsersController
101
+
102
+ If you need to test another action (or a nested controller), pass a block:
103
+
104
+ should_sort_by_attributes :age do |sort, order|
105
+ get :show, :sort => sort, :order => order, :group_id => @group.id
106
+ end
107
+
108
+ If you need to test another collection or model name, use should_sort_by.
109
+
110
+ h3. should_sort_by
111
+
112
+ The :collection, :model_name, and :action options of should_sort_by.
113
+
114
+ context "with a non-standard collection name" do
115
+ action = lambda { |sort, order| get :members, :sort => sort, :order => order }
116
+ should_sort_by :name, { :collection => "members",
117
+ :model_name => "user",
118
+ :action => action } do |user|
119
+ user.name
120
+ end
121
+ end
122
+
123
+ h3. sort_order
124
+
125
+ The default sort order is descending. This applies to the first time you click on a table header. You can override this to be ascending:
126
+
127
+ def index
128
+ @users = User.find :all, :order => sort_order("ascending")
129
+ end
130
+
131
+ Authors
132
+ -------
133
+
134
+ Dan Croak, Joe Ferris, Jason Morrison and Boston.rb.
135
+
136
+ Copyright (c) 2008 Dan Croak, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'date'
4
+
5
+ test_files_pattern = 'test/rails_root/test/{unit,functional,other}/**/*_test.rb'
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'lib'
8
+ t.pattern = test_files_pattern
9
+ t.verbose = false
10
+ end
11
+
12
+ desc "Run the test suite"
13
+ task :default => :test
14
+
15
+ spec = Gem::Specification.new do |s|
16
+ s.version = '0.0.4'
17
+ s.name = "sortable_table"
18
+ s.summary = "Sort HTML tables in a Rails app."
19
+ s.email = "dcroak@thoughtbot.com"
20
+ s.homepage = "http://github.com/dancroak/sortable_table"
21
+ s.description = "Sort HTML tables in a Rails app."
22
+ s.authors = ["Dan Croak", "Joe Ferris", "Boston.rb"]
23
+ s.files = FileList["[A-Z]*", "{lib,rails}/**/*"]
24
+ end
25
+
26
+ desc "Generate a gemspec file"
27
+ task :gemspec do
28
+ File.open("#{spec.name}.gemspec", 'w') do |f|
29
+ f.write spec.to_yaml
30
+ end
31
+ end
data/TODO.textile ADDED
@@ -0,0 +1,2 @@
1
+ * replace created_on with created_at
2
+
@@ -0,0 +1,82 @@
1
+ module SortableTable
2
+ module App
3
+ module Controllers
4
+ module ApplicationController
5
+
6
+ def self.included(base)
7
+ base.class_eval do
8
+ include InstanceMethods
9
+ extend ClassMethods
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def sortable_attributes(*args)
15
+ mappings = pop_hash_from_list(args)
16
+ acceptable_columns = join_array_and_hash_values(args, mappings)
17
+ define_sort_order(acceptable_columns, mappings)
18
+ end
19
+
20
+ def pop_hash_from_list(*args)
21
+ if args.last.is_a?(Hash)
22
+ args.pop
23
+ else
24
+ {}
25
+ end
26
+ end
27
+
28
+ def join_array_and_hash_values(array, hash)
29
+ array.collect { |each| each.to_s } +
30
+ hash.values.collect { |each| each.to_s }
31
+ end
32
+
33
+ def define_sort_order(acceptable_columns, mappings)
34
+ define_method(:sort_order) do |*default|
35
+ direction = params[:order] == 'ascending' ? 'asc' : 'desc'
36
+ column = params[:sort] || acceptable_columns.first
37
+ if params[:sort] && acceptable_columns.include?(column)
38
+ column = mappings[column.to_sym] || column
39
+ handle_compound_sorting(column, direction)
40
+ else
41
+ "#{acceptable_columns.first} #{default_sort_direction(default)}"
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ module InstanceMethods
48
+ def default_sort_direction(default)
49
+ if hash_with_default_key?(default)
50
+ case default.first[:default]
51
+ when "ascending", "asc" then "asc"
52
+ when "descending", "asc" then "desc"
53
+ else
54
+ raise RuntimeError,
55
+ "valid :default sort orders are 'ascending' & 'descending'"
56
+ end
57
+ else
58
+ "desc"
59
+ end
60
+ end
61
+
62
+ def hash_with_default_key?(object)
63
+ object.any? &&
64
+ object.first.is_a?(Hash) &&
65
+ object.first.has_key?(:default)
66
+ end
67
+
68
+ def handle_compound_sorting(column, direction)
69
+ if column.is_a?(Array)
70
+ column.collect { |each| "#{each} #{direction}" }.join(',')
71
+ else
72
+ "#{column} #{direction}"
73
+ end
74
+ end
75
+ end
76
+
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+
@@ -0,0 +1,70 @@
1
+ module SortableTable
2
+ module App
3
+ module Helpers
4
+ module ApplicationHelper
5
+
6
+ def self.included(base)
7
+ base.class_eval do
8
+ include InstanceMethods
9
+ end
10
+ end
11
+
12
+ module InstanceMethods
13
+ def sortable_table_header(opts = {})
14
+ raise ArgumentError if opts[:name].nil? || opts[:sort].nil?
15
+ anchor = opts[:anchor].blank? ? "" : "##{opts[:anchor]}"
16
+ content_tag :th,
17
+ link_to(opts[:name],
18
+ sortable_url(opts) + anchor,
19
+ :title => opts[:title]),
20
+ :class => sortable_table_header_classes(opts)
21
+ end
22
+
23
+ def sortable_table_header_classes(opts)
24
+ class_names = []
25
+ class_names << sortable_table_header_class(opts)
26
+ class_names << opts[:class]
27
+ class_names.compact.blank? ? nil : class_names.compact.join(" ")
28
+ end
29
+
30
+ def sortable_table_header_class(opts)
31
+ if default_sort_to_most_recent? opts
32
+ 'descending'
33
+ elsif re_sort? opts
34
+ params[:order]
35
+ else
36
+ nil
37
+ end
38
+ end
39
+
40
+ def default_sort_to_most_recent?(opts)
41
+ params[:sort].nil? && opts[:sort] == 'date'
42
+ end
43
+
44
+ def re_sort?(opts)
45
+ params[:sort] == opts[:sort]
46
+ end
47
+
48
+ def reverse_order(order)
49
+ order == 'ascending' ? 'descending' : 'ascending'
50
+ end
51
+
52
+ def sortable_url(opts)
53
+ url_for(params.merge(:sort => opts[:sort], :order => link_sort_order(opts), :page => 1))
54
+ end
55
+
56
+ def link_sort_order(opts)
57
+ if default_sort_to_most_recent? opts
58
+ 'ascending'
59
+ elsif re_sort? opts
60
+ reverse_order params[:order]
61
+ else
62
+ 'ascending'
63
+ end
64
+ end
65
+ end
66
+
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,2 @@
1
+ require "sortable_table/app/controllers/application_controller"
2
+ require "sortable_table/app/helpers/application_helper"
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "sortable_table"
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dancroak-sortable_table
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Dan Croak
8
+ - Joe Ferris
9
+ - Boston.rb
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-01-01 21:00:00 -08:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description: Sort HTML tables in a Rails app.
19
+ email: dcroak@thoughtbot.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - MIT-LICENSE
28
+ - Rakefile
29
+ - README.textile
30
+ - TODO.textile
31
+ - lib/sortable_table
32
+ - lib/sortable_table/app
33
+ - lib/sortable_table/app/controllers
34
+ - lib/sortable_table/app/controllers/application_controller.rb
35
+ - lib/sortable_table/app/helpers
36
+ - lib/sortable_table/app/helpers/application_helper.rb
37
+ - lib/sortable_table.rb
38
+ - rails/init.rb
39
+ has_rdoc: false
40
+ homepage: http://github.com/dancroak/sortable_table
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: Sort HTML tables in a Rails app.
65
+ test_files: []
66
+