Skip to content

Commit

Permalink
should_display_sortable_table_header_for macro, shoulda_macros approa…
Browse files Browse the repository at this point in the history
…ch for one less include
  • Loading branch information
Dan Croak committed Oct 30, 2008
1 parent b0f8439 commit bdd021e
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 64 deletions.
1 change: 0 additions & 1 deletion lib/sortable_table.rb
@@ -1,3 +1,2 @@
require "sortable_table/app/controllers/application_controller"
require "sortable_table/app/helpers/application_helper"
require "sortable_table/test/test_helper"
60 changes: 0 additions & 60 deletions lib/sortable_table/test/test_helper.rb

This file was deleted.

73 changes: 73 additions & 0 deletions shoulda_macros/sortable_table.rb
@@ -0,0 +1,73 @@
module SortableTable
module Shoulda
def should_sort_by(attribute, options = {}, &block)
collection = self.name.underscore.gsub(/_controller_test/, '')
collection.slice!(0..collection.rindex('/')) if collection.include?('/')
collection = collection.to_sym
model_name = collection.to_s.singularize.camelize.constantize

if !block
if model_name.columns.select{|c| c.name == attribute.to_s }.first.type == :boolean
block = lambda{|x| x.send(attribute).to_s }
end
block ||= attribute
end

action = options[:action] || lambda do |sort, direction|
get :index, :sort => sort, :order => direction
end

%w(ascending descending).each do |direction|
should "sort by #{attribute.to_s} #{direction}" do
assert_not_nil model_name.find(:all).any?,
"#{model_name}.find(:all) is nil"

action.bind(self).call(attribute.to_s, direction)

assert_not_nil assigns(collection),
"assigns(:#{collection}) is nil"
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.map(&block) == assigns(collection).map(&block),
"expected - #{expected.map(&block).inspect}," <<
" but was - #{assigns(collection).map(&block).inspect}"
end
end
end

def should_sort_by_attributes(*attributes, &block)
attributes.each do |attr|
should_sort_by attr, :action => block
end
end

def should_display_sortable_table_header_for(*valid_sorts)
valid_sorts.each do |attr|
should "have a link to sort by #{attr}" do
assert_select 'a[href*=?]', "sort=#{attr}", true,
"link not found to sort by #{attr}. Try adding this to the view: " <<
"<%= sortable_table_header :name => '#{attr}', :sort => '#{attr}' %>"
end
end

should "not link to any invalid sorting options" do
assert_select 'a[href*=?]', 'sort=' do |elements|
sortings = elements.collect {|element|
element.attributes['href'].match(/sort=([^&]*)/)[1]
}
sortings.each {|sorting|
assert !valid_sorts.include?(sorting),
"link found for sortion option which is not in valid list: #{sorting}."
}
end
end
end
end
end

Test::Unit::TestCase.extend(SortableTable::Shoulda)

9 changes: 9 additions & 0 deletions test/rails_root/log/development.log
Expand Up @@ -2708,3 +2708,12 @@ Completed in 0.01353 (73 reqs/sec) | Rendering: 0.00568 (42%) | DB: 0.00204 (15%

SQL (0.000114) SELECT version FROM "schema_migrations"
SQL (0.001163) INSERT INTO "schema_migrations" (version) VALUES ('20080819225020')


Processing UsersController#index (for 127.0.0.1 at 2008-10-30 11:14:18) [GET]
Session ID: 8d1f719f4d76387c2d2d1e9a6d7f5a9f
Parameters: {"action"=>"index", "controller"=>"users"}
User Load (0.002897) SELECT * FROM "users" ORDER BY name desc
Rendering template within layouts/users
Rendering users/index
Completed in 0.14385 (6 reqs/sec) | Rendering: 0.13151 (91%) | DB: 0.00290 (2%) | 200 OK [http://localhost/users]
11 changes: 10 additions & 1 deletion test/rails_root/test/functional/users_controller_test.rb
Expand Up @@ -2,7 +2,7 @@

class UsersControllerTest < ActionController::TestCase

context 'GET to index with sort and order params' do
context "GET to #index with sort and order params" do
setup do
2.times { |each| Factory :user }
end
Expand All @@ -13,4 +13,13 @@ class UsersControllerTest < ActionController::TestCase
end
end

context "GET to #index" do
setup do
2.times { |each| Factory :user }
get :index
end

should_display_sortable_table_header_for :name, :email, :age
end

end
1 change: 1 addition & 0 deletions test/rails_root/test/shoulda_macros.rb
@@ -0,0 +1 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'shoulda_macros', 'sortable_table')
3 changes: 1 addition & 2 deletions test/rails_root/test/test_helper.rb
Expand Up @@ -4,6 +4,7 @@
require 'rubygems'

require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
require File.expand_path(File.dirname(__FILE__) + '/shoulda_macros')

Dir[File.join(RAILS_ROOT, 'test', 'factories', '*.rb')].each do |file|
require file
Expand All @@ -17,6 +18,4 @@ class Test::Unit::TestCase
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = false

include SortableTable::Test::TestHelper

end

0 comments on commit bdd021e

Please sign in to comment.