ryana-sortable_table 0.2.0

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.
Files changed (79) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.textile +86 -0
  3. data/Rakefile +31 -0
  4. data/VERSION.yml +4 -0
  5. data/lib/sortable_table.rb +3 -0
  6. data/lib/sortable_table/app/controllers/application_controller.rb +46 -0
  7. data/lib/sortable_table/app/helpers/application_helper.rb +69 -0
  8. data/lib/sortable_table/test/test_helper.rb +50 -0
  9. data/rails/init.rb +1 -0
  10. data/test/rails_root/Rakefile +10 -0
  11. data/test/rails_root/app/controllers/application.rb +5 -0
  12. data/test/rails_root/app/controllers/users_controller.rb +9 -0
  13. data/test/rails_root/app/helpers/application_helper.rb +3 -0
  14. data/test/rails_root/app/helpers/users_helper.rb +2 -0
  15. data/test/rails_root/app/models/user.rb +2 -0
  16. data/test/rails_root/app/views/layouts/users.html.erb +17 -0
  17. data/test/rails_root/app/views/users/index.html.erb +19 -0
  18. data/test/rails_root/config/boot.rb +109 -0
  19. data/test/rails_root/config/database.yml +19 -0
  20. data/test/rails_root/config/environment.rb +26 -0
  21. data/test/rails_root/config/environments/development.rb +17 -0
  22. data/test/rails_root/config/environments/test.rb +28 -0
  23. data/test/rails_root/config/initializers/new_rails_defaults.rb +15 -0
  24. data/test/rails_root/config/initializers/sortable_table.rb +8 -0
  25. data/test/rails_root/config/routes.rb +6 -0
  26. data/test/rails_root/db/development.sqlite3 +0 -0
  27. data/test/rails_root/db/migrate/20080819225020_create_users.rb +15 -0
  28. data/test/rails_root/db/production.sqlite3 +0 -0
  29. data/test/rails_root/db/schema.rb +23 -0
  30. data/test/rails_root/db/test.sqlite3 +0 -0
  31. data/test/rails_root/log/development.log +2710 -0
  32. data/test/rails_root/log/production.log +0 -0
  33. data/test/rails_root/log/server.log +0 -0
  34. data/test/rails_root/log/test.log +764 -0
  35. data/test/rails_root/public/404.html +30 -0
  36. data/test/rails_root/public/422.html +30 -0
  37. data/test/rails_root/public/500.html +30 -0
  38. data/test/rails_root/public/dispatch.cgi +10 -0
  39. data/test/rails_root/public/dispatch.fcgi +24 -0
  40. data/test/rails_root/public/dispatch.rb +10 -0
  41. data/test/rails_root/public/favicon.ico +0 -0
  42. data/test/rails_root/public/images/rails.png +0 -0
  43. data/test/rails_root/public/images/sort-ascending-arrow.gif +0 -0
  44. data/test/rails_root/public/images/sort-descending-arrow.gif +0 -0
  45. data/test/rails_root/public/index.html +274 -0
  46. data/test/rails_root/public/javascripts/application.js +2 -0
  47. data/test/rails_root/public/javascripts/controls.js +963 -0
  48. data/test/rails_root/public/javascripts/dragdrop.js +972 -0
  49. data/test/rails_root/public/javascripts/effects.js +1120 -0
  50. data/test/rails_root/public/javascripts/prototype.js +4225 -0
  51. data/test/rails_root/public/robots.txt +5 -0
  52. data/test/rails_root/public/stylesheets/scaffold.css +53 -0
  53. data/test/rails_root/public/stylesheets/sortable.css +10 -0
  54. data/test/rails_root/script/about +3 -0
  55. data/test/rails_root/script/console +3 -0
  56. data/test/rails_root/script/dbconsole +3 -0
  57. data/test/rails_root/script/destroy +3 -0
  58. data/test/rails_root/script/generate +3 -0
  59. data/test/rails_root/script/performance/benchmarker +3 -0
  60. data/test/rails_root/script/performance/profiler +3 -0
  61. data/test/rails_root/script/performance/request +3 -0
  62. data/test/rails_root/script/plugin +3 -0
  63. data/test/rails_root/script/process/inspector +3 -0
  64. data/test/rails_root/script/process/reaper +3 -0
  65. data/test/rails_root/script/process/spawner +3 -0
  66. data/test/rails_root/script/runner +3 -0
  67. data/test/rails_root/script/server +3 -0
  68. data/test/rails_root/test/factories/user_factory.rb +17 -0
  69. data/test/rails_root/test/functional/users_controller_test.rb +15 -0
  70. data/test/rails_root/test/helper_testcase.rb +38 -0
  71. data/test/rails_root/test/test_helper.rb +22 -0
  72. data/test/rails_root/test/unit/helpers/application_helper_test.rb +53 -0
  73. data/test/rails_root/vendor/plugins/helper_test/MIT-LICENSE +20 -0
  74. data/test/rails_root/vendor/plugins/helper_test/README +33 -0
  75. data/test/rails_root/vendor/plugins/helper_test/about.yml +7 -0
  76. data/test/rails_root/vendor/plugins/helper_test/generators/helper_test/helper_test_generator.rb +22 -0
  77. data/test/rails_root/vendor/plugins/helper_test/generators/helper_test/templates/helper_test.rb +13 -0
  78. data/test/rails_root/vendor/plugins/helper_test/generators/helper_test/templates/helper_testcase.rb +38 -0
  79. metadata +176 -0
@@ -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.
@@ -0,0 +1,86 @@
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 test/test_helper.rb:
10
+
11
+ class Test::Unit::TestCase
12
+ include SortableTable::Test::TestHelper
13
+ end
14
+
15
+ In app/controllers/application_controller.rb:
16
+
17
+ class ApplicationController < ActionController::Base
18
+ include SortableTable::App::Controllers::ApplicationController
19
+ end
20
+
21
+ In app/helpers/application_helper.rb:
22
+
23
+ module ApplicationHelper
24
+ include SortableTable::App::Helpers::ApplicationHelper
25
+ end
26
+
27
+ h2. Conventions
28
+
29
+ params[:sort] and params[:order]
30
+
31
+ h2. Examples
32
+
33
+ Use the sortable_attributes macro to list the... sortable attributes.
34
+
35
+ Then, in your index action, pass the sort_order method to your usual
36
+ order parameter for will paginate or named scope call.
37
+
38
+ class UsersController < Admin::BaseController
39
+
40
+ sortable_attributes :name, :email
41
+
42
+ def index
43
+ @users = User.paginate :page => params[:page], :order => sort_order
44
+ end
45
+
46
+ end
47
+
48
+ Time-saving Shoulda macros for your tests:
49
+
50
+ class UsersControllerTest < ActionController::TestCase
51
+
52
+ context 'GET to index with sort and order params' do
53
+ setup do
54
+ 5.times do |each|
55
+ Factory :user,
56
+ :name => "name #{each}",
57
+ :email => "email#{each}@example.com"
58
+ end
59
+ end
60
+
61
+ should_sort_by :name
62
+ should_sort_by :email
63
+ end
64
+
65
+ end
66
+
67
+ And some sugar for your views:
68
+
69
+ %h1 Users
70
+
71
+ %table
72
+ %tr
73
+ = sortable_table_header :name => 'Name', :sort => 'name'
74
+ = sortable_table_header :name => 'E-mail', :sort => 'email'
75
+
76
+ - @users.each do |each|
77
+ %tr
78
+ %td= each.name
79
+ %td= each.email
80
+
81
+ Authors
82
+ -------
83
+
84
+ Dan Croak, Joe Ferris, and Boston.rb.
85
+
86
+ Copyright (c) 2008 Dan Croak, released under the MIT license
@@ -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.name = "sortable_table"
17
+ s.summary = "Sort HTML tables in a Rails app."
18
+ s.email = "dcroak@thoughtbot.com"
19
+ s.homepage = "http://github.com/dancroak/sortable_table"
20
+ s.description = "Sort HTML tables in a Rails app."
21
+ s.authors = ["Dan Croak", "Joe Ferris", "Boston.rb"]
22
+ s.files = FileList["[A-Z]*", "{lib,rails,test}/**/*"]
23
+ end
24
+
25
+ begin
26
+ require 'rubygems'
27
+ require 'jeweler'
28
+ Jeweler.gemspec = spec
29
+ rescue LoadError
30
+ puts "Jeweler not available. sudo gem install technicalpickles-jeweler --source=http://gems.github.com"
31
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ patch: 0
3
+ major: 0
4
+ minor: 2
@@ -0,0 +1,3 @@
1
+ require "sortable_table/app/controllers/application_controller"
2
+ require "sortable_table/app/helpers/application_helper"
3
+ require "sortable_table/test/test_helper"
@@ -0,0 +1,46 @@
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 = args.last.is_a?(Hash) ? args.pop : {}
16
+ acceptable_columns = args.collect(&:to_s) + mappings.keys.collect(&:to_s)
17
+
18
+ define_method(:sort_order) do |*default|
19
+ direction = params[:order] == 'ascending' ? 'asc' : 'desc'
20
+ column = params[:sort] || 'created_on'
21
+ if params[:sort] && acceptable_columns.include?(column)
22
+ column = mappings[column.to_sym] || column
23
+ "#{column} #{direction}"
24
+ else
25
+ "#{acceptable_columns.first} #{default_sort_direction(default)}"
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ module InstanceMethods
32
+ def default_sort_direction(default)
33
+ if default.any? && default.first.is_a?(Hash) && default.first.has_key?(:default)
34
+ default.first[:default] == 'ascending' ? 'asc' : 'desc'
35
+ else
36
+ 'desc'
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+
@@ -0,0 +1,69 @@
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(name_with_desc(opts),
18
+ sortable_url(opts) + anchor,
19
+ :title => opts[:title]),
20
+ :class => class_name_for_sortable_table_header_tag(opts)
21
+ end
22
+
23
+ def class_name_for_sortable_table_header_tag(opts)
24
+ if default_sort_to_most_recent? opts
25
+ 'descending'
26
+ elsif re_sort? opts
27
+ params[:order]
28
+ else
29
+ nil
30
+ end
31
+ end
32
+
33
+ def default_sort_to_most_recent?(opts)
34
+ params[:sort].nil? && opts[:sort] == 'date'
35
+ end
36
+
37
+ def re_sort?(opts)
38
+ params[:sort] == opts[:sort]
39
+ end
40
+
41
+ def reverse_order(order)
42
+ order == 'ascending' ? 'descending' : 'ascending'
43
+ end
44
+
45
+ def name_with_desc(opts)
46
+ return opts[:name] unless opts[:sort] == params[:sort]
47
+
48
+ link_sort_order(opts) == 'descending' ? opts[:name] + " (asc)" : opts[:name] + " (desc)"
49
+ end
50
+
51
+ def sortable_url(opts)
52
+ url_for(params.merge(:sort => opts[:sort], :order => link_sort_order(opts), :page => 1))
53
+ end
54
+
55
+ def link_sort_order(opts)
56
+ if default_sort_to_most_recent? opts
57
+ 'ascending'
58
+ elsif re_sort? opts
59
+ reverse_order params[:order]
60
+ else
61
+ 'ascending'
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,50 @@
1
+ module SortableTable
2
+ module Test
3
+ module TestHelper
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+ extend ClassMethods
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def should_sort_by(attribute, &block)
13
+ collection = self.name.underscore.gsub(/_controller_test/, '')
14
+ collection.slice!(0..collection.rindex('/')) if collection.include?('/')
15
+ collection = collection.to_sym
16
+ model_name = collection.to_s.singularize.camelize.constantize
17
+
18
+ if !block
19
+ if model_name.columns.select{|c| c.name == attribute.to_s }.first.type == :boolean
20
+ block = lambda{|x| x.send(attribute).to_s }
21
+ end
22
+ block ||= attribute
23
+ end
24
+
25
+ %w(ascending descending).each do |direction|
26
+ should "sort by #{attribute.to_s} #{direction}" do
27
+ assert_not_nil model_name.find(:all).any?,
28
+ "#{model_name}.find(:all) is nil"
29
+
30
+ get :index, :sort => attribute.to_s, :order => direction
31
+
32
+ assert_not_nil assigns(collection),
33
+ "assigns(:#{collection}) is nil"
34
+ assert assigns(collection).size >= 2,
35
+ "cannot test sorting without at least 2 sortable objects"
36
+
37
+ expected = assigns(collection).sort_by(&block)
38
+ expected = expected.reverse if direction == 'descending'
39
+
40
+ assert expected.map(&block) == assigns(collection).map(&block),
41
+ "expected - #{expected.map(&block).inspect}," <<
42
+ " but was - #{assigns(collection).map(&block).inspect}"
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1 @@
1
+ require "sortable_table"
@@ -0,0 +1,10 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require(File.join(File.dirname(__FILE__), 'config', 'boot'))
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+
10
+ require 'tasks/rails'
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ helper :all
3
+ protect_from_forgery
4
+ include SortableTable::App::Controllers::ApplicationController
5
+ end
@@ -0,0 +1,9 @@
1
+ class UsersController < ApplicationController
2
+
3
+ sortable_attributes :name, :age, :email => 'users.email'
4
+
5
+ def index
6
+ @users = User.find :all, :order => sort_order('ascending')
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ module ApplicationHelper
2
+ include SortableTable::App::Helpers::ApplicationHelper
3
+ end
@@ -0,0 +1,2 @@
1
+ module UsersHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class User < ActiveRecord::Base
2
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
7
+ <title>Users: <%= controller.action_name %></title>
8
+ <%= stylesheet_link_tag 'scaffold', 'sortable' %>
9
+ </head>
10
+ <body>
11
+
12
+ <p style="color: green"><%= flash[:notice] %></p>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
@@ -0,0 +1,19 @@
1
+ <h1>Users</h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <%= sortable_table_header :name => 'Name', :sort => 'name' %>
6
+ <%= sortable_table_header :name => 'Email', :sort => 'email' %>
7
+ <%= sortable_table_header :name => 'Age', :sort => 'age' %>
8
+ <th>Created On</th>
9
+ </tr>
10
+
11
+ <% @users.each do |user| %>
12
+ <tr>
13
+ <td><%=h user.name %></td>
14
+ <td><%=h user.email %></td>
15
+ <td><%=h user.age %></td>
16
+ <td><%=h user.created_at.to_s %></td>
17
+ </tr>
18
+ <% end %>
19
+ </table>
@@ -0,0 +1,109 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ end
48
+ end
49
+
50
+ class GemBoot < Boot
51
+ def load_initializer
52
+ self.class.load_rubygems
53
+ load_rails_gem
54
+ require 'initializer'
55
+ end
56
+
57
+ def load_rails_gem
58
+ if version = self.class.gem_version
59
+ gem 'rails', version
60
+ else
61
+ gem 'rails'
62
+ end
63
+ rescue Gem::LoadError => load_error
64
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
65
+ exit 1
66
+ end
67
+
68
+ class << self
69
+ def rubygems_version
70
+ Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
71
+ end
72
+
73
+ def gem_version
74
+ if defined? RAILS_GEM_VERSION
75
+ RAILS_GEM_VERSION
76
+ elsif ENV.include?('RAILS_GEM_VERSION')
77
+ ENV['RAILS_GEM_VERSION']
78
+ else
79
+ parse_gem_version(read_environment_rb)
80
+ end
81
+ end
82
+
83
+ def load_rubygems
84
+ require 'rubygems'
85
+
86
+ unless rubygems_version >= '0.9.4'
87
+ $stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)
88
+ exit 1
89
+ end
90
+
91
+ rescue LoadError
92
+ $stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)
93
+ exit 1
94
+ end
95
+
96
+ def parse_gem_version(text)
97
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
98
+ end
99
+
100
+ private
101
+ def read_environment_rb
102
+ File.read("#{RAILS_ROOT}/config/environment.rb")
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ # All that for this:
109
+ Rails.boot!