Skip to content
Browse files

first extractions from Rails app

  • Loading branch information...
0 parents commit c7a9c521746bc4fe2f4de2bfc793efbdbc4a2f8d @croaky croaky committed
Showing with 108 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +6 −0 README.markdown
  3. +22 −0 Rakefile
  4. +1 −0 init.rb
  5. +20 −0 lib/sortable_table.rb
  6. +26 −0 lib/sortable_table/shoulda.rb
  7. +5 −0 rails/init.rb
  8. +8 −0 test/sortable_table_test.rb
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Dan Croak
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 README.markdown
@@ -0,0 +1,6 @@
+Sortable Table
+==============
+
+Thanks to Joe Ferris and Boston.rb.
+
+Copyright (c) 2008 Dan Croak, released under the MIT license
22 Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the sortable_table plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the sortable_table plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'SortableTable'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1 init.rb
@@ -0,0 +1 @@
+require File.join(File.dirname(__FILE__), 'rails', 'init')
20 lib/sortable_table.rb
@@ -0,0 +1,20 @@
+# SortableTable
+
+class ActionController::Base
+
+ def self.sanitizes_order(*args)
+ mappings = args.last.is_a?(Hash) ? args.pop : {}
+ acceptable_columns = args.collect(&:to_s) + mappings.keys.collect(&:to_s)
+ define_method(:sanitized_order) do
+ direction = params[:order] == 'ascending' ? 'asc' : 'desc'
+ column = params[:sort] || 'created_on'
+ if acceptable_columns.include? column
+ column = mappings[column.to_sym] || column
+ "#{column} #{direction}"
+ else
+ sanitized_order acceptable_columns.first, 'descending'
+ end
+ end
+ end
+
+end
26 lib/sortable_table/shoulda.rb
@@ -0,0 +1,26 @@
+class Test::Unit::TestCase
+
+ def self.should_sort_by(attribute, &block)
+ collection = self.name.underscore.gsub(/_controller_test/, '')
+ collection.slice!(0..collection.rindex('/'))
+ collection = collection.to_sym
+ block ||= attribute
+
+ %w(ascending descending).each do |direction|
+ should "sort by #{attribute.to_s} #{direction}" do
+ get :index, :sort => attribute.to_s, :order => direction
+
+ assert assigns(collection).size >= 2,
+ "cannot test sorting without at least 2 sortable objects"
+
+ expected = assigns(collection).sort_by(&block)
+ expected = expected.reverse if direction == 'descending'
+
+ assert expected == assigns(collection) #,
+ "expected - #{expected.map(&block).inspect}," <<
+ " but was - #{assigns(collection).map(&block).inspect}"
+ end
+ end
+ end
+
+end
5 rails/init.rb
@@ -0,0 +1,5 @@
+require 'sortable_table'
+
+if defined?(Rails) && Rails.env.test?
+ require 'sortable_table/shoulda'
+end
8 test/sortable_table_test.rb
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class SortableTableTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def test_this_plugin
+ flunk
+ end
+end

0 comments on commit c7a9c52

Please sign in to comment.
Something went wrong with that request. Please try again.