Skip to content

Commit

Permalink
first extractions from Rails app
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Croak committed Aug 18, 2008
0 parents commit c7a9c52
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 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 changes: 6 additions & 0 deletions 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 changes: 22 additions & 0 deletions 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 change: 1 addition & 0 deletions init.rb
@@ -0,0 +1 @@
require File.join(File.dirname(__FILE__), 'rails', 'init')
20 changes: 20 additions & 0 deletions 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 changes: 26 additions & 0 deletions 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 changes: 5 additions & 0 deletions rails/init.rb
@@ -0,0 +1,5 @@
require 'sortable_table'

if defined?(Rails) && Rails.env.test?
require 'sortable_table/shoulda'
end
8 changes: 8 additions & 0 deletions 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.