public
Description: Not scaffolding. Resourcing. Creates extremely customizable resource controllers with one line of code.
Clone URL: git://github.com/jnewland/resource_this.git
resource_this / test / resource_this_sorting_test.rb
100644 52 lines (44 sloc) 1.411 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require File.join(File.dirname(__FILE__), 'test_helper')
 
class ResourceThisSortingTest < Test::Unit::TestCase
  def setup
    @controller = WidgetsController.new
    @request = ActionController::TestRequest.new
    @request.accept = 'application/xml'
    @response = ActionController::TestResponse.new
    @a = Widget.create(:title => "aaa", :body => "zzz")
    @z = Widget.create(:title => "zzz", :body => "aaa")
    100.times do
      Widget.create(:title => "foo", :body => "bar")
    end
    ActionController::Routing::Routes.draw do |map|
      map.resources :widgets
    end
  end
  
  def teardown
    Widget.find(:all).each { |w| w.destroy }
  end
  
  def test_should_get_index
    get :index
    assert_response :success
    assert assigns(:widgets)
    assert_equal @a, assigns(:widgets).first
  end
  
  def test_should_get_index_sorted
    @controller.resource_this_finder_options = {:order => 'body'}
    get :index
    assert_response :success
    assert assigns(:widgets)
    assert_equal @z, assigns(:widgets).first
  end
  
  def test_should_get_index_sorted_with_proc
    @controller.resource_this_finder_options = Proc.new { finder_options }
    get :index
    assert_response :success
    assert assigns(:widgets)
    assert_equal 2, assigns(:widgets).size
    assert_equal @z, assigns(:widgets).first
  end
  
  def finder_options
    {:order => 'body', :limit => 2}
  end
  
end