forked from ryana/sortable_table
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
should_display_sortable_table_header_for macro, shoulda_macros approa…
…ch for one less include
- Loading branch information
Dan Croak
committed
Oct 30, 2008
1 parent
b0f8439
commit bdd021e
Showing
7 changed files
with
94 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
require "sortable_table/app/controllers/application_controller" | ||
require "sortable_table/app/helpers/application_helper" | ||
require "sortable_table/test/test_helper" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require File.join(File.dirname(__FILE__), '..', '..', '..', 'shoulda_macros', 'sortable_table') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters