diff --git a/MIT-LICENSE b/MIT-LICENSE new file mode 100644 index 0000000..9517cdd --- /dev/null +++ b/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. diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..8fd419d --- /dev/null +++ b/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 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..59bcb79 --- /dev/null +++ b/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 diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..6dbfa4c --- /dev/null +++ b/init.rb @@ -0,0 +1 @@ +require File.join(File.dirname(__FILE__), 'rails', 'init') diff --git a/lib/sortable_table.rb b/lib/sortable_table.rb new file mode 100644 index 0000000..a0cebf5 --- /dev/null +++ b/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 diff --git a/lib/sortable_table/shoulda.rb b/lib/sortable_table/shoulda.rb new file mode 100644 index 0000000..f2075cb --- /dev/null +++ b/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 \ No newline at end of file diff --git a/rails/init.rb b/rails/init.rb new file mode 100644 index 0000000..46b7093 --- /dev/null +++ b/rails/init.rb @@ -0,0 +1,5 @@ +require 'sortable_table' + +if defined?(Rails) && Rails.env.test? + require 'sortable_table/shoulda' +end diff --git a/test/sortable_table_test.rb b/test/sortable_table_test.rb new file mode 100644 index 0000000..1eb9ee6 --- /dev/null +++ b/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