dry_crud 1.2.0 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/README.rdoc +28 -11
  2. data/Rakefile +2 -1
  3. data/VERSION +1 -1
  4. data/lib/generators/dry_crud/templates/INSTALL +0 -1
  5. data/lib/generators/dry_crud/templates/app/controllers/crud_controller.rb +11 -183
  6. data/lib/generators/dry_crud/templates/app/controllers/list_controller.rb +212 -0
  7. data/lib/generators/dry_crud/templates/app/helpers/crud_helper.rb +10 -20
  8. data/lib/generators/dry_crud/templates/app/helpers/list_helper.rb +25 -0
  9. data/lib/generators/dry_crud/templates/app/helpers/standard_helper.rb +5 -4
  10. data/lib/generators/dry_crud/templates/app/helpers/standard_table_builder.rb +2 -2
  11. data/lib/generators/dry_crud/templates/app/views/crud/edit.html.erb +1 -1
  12. data/lib/generators/dry_crud/templates/app/views/crud/new.html.erb +1 -1
  13. data/lib/generators/dry_crud/templates/app/views/crud/show.html.erb +1 -1
  14. data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.erb +5 -1
  15. data/lib/generators/dry_crud/templates/app/views/list/_actions_index.html.erb +0 -0
  16. data/lib/generators/dry_crud/templates/app/views/list/_list.html.erb +1 -0
  17. data/lib/generators/dry_crud/templates/app/views/list/_search.html.erb +7 -0
  18. data/lib/generators/dry_crud/templates/app/views/{crud → list}/index.html.erb +1 -1
  19. data/lib/generators/dry_crud/templates/public/stylesheets/crud.css +13 -10
  20. data/lib/generators/dry_crud/templates/test/crud_test_model.rb +50 -25
  21. data/lib/generators/dry_crud/templates/test/custom_assertions.rb +8 -0
  22. data/lib/generators/dry_crud/templates/test/functional/crud_test_models_controller_test.rb +23 -3
  23. data/lib/generators/dry_crud/templates/test/unit/custom_assertions_test.rb +18 -0
  24. data/lib/generators/dry_crud/templates/test/unit/helpers/crud_helper_test.rb +22 -75
  25. data/lib/generators/dry_crud/templates/test/unit/helpers/list_helper_test.rb +139 -0
  26. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_form_builder_test.rb +4 -7
  27. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_helper_test.rb +6 -10
  28. data/test/templates/app/controllers/people_controller.rb +1 -0
  29. data/test/templates/app/controllers/vips_controller.rb +24 -0
  30. data/test/templates/app/views/cities/_list.html.erb +3 -3
  31. data/test/templates/app/views/layouts/application.html.erb +33 -0
  32. data/test/templates/app/views/people/_list.html.erb +1 -0
  33. data/test/templates/config/routes.rb +2 -0
  34. data/test/templates/db/seeds.rb +1 -1
  35. data/test/templates/public/stylesheets/demo.css +113 -0
  36. metadata +15 -7
  37. data/lib/generators/dry_crud/templates/app/views/crud/_search.html.erb +0 -8
  38. data/test/templates/app/views/layouts/crud.html.erb +0 -26
@@ -1,4 +1,4 @@
1
- <% @title = "Edit #{full_entry_label}" -%>
1
+ <% @title ||= "Edit #{full_entry_label}" -%>
2
2
 
3
3
  <%= render :partial => 'form' %>
4
4
 
@@ -1,4 +1,4 @@
1
- <% @title = "New #{models_label.singularize}" -%>
1
+ <% @title ||= "New #{models_label.singularize}" -%>
2
2
 
3
3
  <%= render :partial => 'form' %>
4
4
 
@@ -1,4 +1,4 @@
1
- <% @title = full_entry_label -%>
1
+ <% @title ||= full_entry_label -%>
2
2
 
3
3
  <%= render :partial => 'attrs' %>
4
4
 
@@ -13,7 +13,11 @@
13
13
 
14
14
  <h1><%= @title %></h1>
15
15
 
16
- <p id="flash_notice"><%= flash[:notice] %></p>
16
+ <% if flash[:notice].present? %>
17
+ <div id="flash_notice"><%= flash[:notice] %></div>
18
+ <% elsif flash[:alert].present? %>
19
+ <div id="flash_alert"><%= flash[:alert] %></div>
20
+ <% end %>
17
21
 
18
22
  <%= yield %>
19
23
 
@@ -0,0 +1 @@
1
+ <%= list_table %>
@@ -0,0 +1,7 @@
1
+ <%= form_tag(nil, {:method => :get}) do %>
2
+ <%= hidden_field_tag :returning, true %>
3
+ <%= search_field_tag :q, params[:q] %>
4
+ <%= submit_tag 'Search' %>
5
+ <% end %>
6
+
7
+ <br/>
@@ -1,4 +1,4 @@
1
- <% @title = "Listing #{models_label}" -%>
1
+ <% @title ||= "Listing #{models_label}" -%>
2
2
 
3
3
  <%= render :partial => 'search' if search_support? %>
4
4
 
@@ -28,11 +28,11 @@ td p {
28
28
  margin: 0;
29
29
  }
30
30
 
31
- .right_align {
31
+ .right {
32
32
  text-align: right;
33
33
  }
34
34
 
35
- .center_align {
35
+ .center {
36
36
  text-align: center;
37
37
  }
38
38
 
@@ -72,18 +72,21 @@ a:visited.action {
72
72
  }
73
73
 
74
74
  #flash_notice {
75
- color: #663333;
75
+ border: solid 2px #6a6;
76
+ background-color: #afa;
77
+ margin: 20pt;
78
+ padding: 5pt 10pt;
76
79
  }
77
80
 
78
81
  #error_explanation {
79
- border: solid 1px #999999;
80
- margin: 10pt;
81
- padding: 5pt;
82
- background-color: #FFBBBB;
82
+ border: solid 2pt #da9;
83
+ margin: 20pt;
84
+ padding: 5pt 10pt;
85
+ background-color: #fec;
83
86
  }
84
87
 
85
88
  #error_explanation h2 {
86
- font-size: 12pt;
89
+ font-size: 11pt;
87
90
  margin-top: 0pt;
88
91
  }
89
92
 
@@ -92,8 +95,8 @@ a:visited.action {
92
95
  }
93
96
 
94
97
  .value div.field_with_errors {
95
- background-color: #FFBBBB;
98
+ background-color: #da9;
96
99
  display: inline-block;
97
- padding: 1px;
100
+ padding: 1px 1px;
98
101
  }
99
102
 
@@ -1,9 +1,10 @@
1
+ # A dummy model used for general testing.
1
2
  class CrudTestModel < ActiveRecord::Base #:nodoc:
2
3
 
3
4
  validates :name, :presence => true
4
5
  validates :rating, :inclusion => { :in => 1..10 }
5
6
 
6
- #default_scope :order => 'name'
7
+ default_scope order('name')
7
8
 
8
9
  belongs_to :companion, :class_name => 'CrudTestModel'
9
10
 
@@ -16,6 +17,7 @@ class CrudTestModel < ActiveRecord::Base #:nodoc:
16
17
  end
17
18
  end
18
19
 
20
+ # Controller for the dummy model.
19
21
  class CrudTestModelsController < CrudController #:nodoc:
20
22
  HANDLE_PREFIX = 'handle_'
21
23
 
@@ -42,7 +44,7 @@ class CrudTestModelsController < CrudController #:nodoc:
42
44
  def list_entries
43
45
  entries = super
44
46
  if params[:filter]
45
- entries = entries.where(['rating < ?', 3]).order('children DESC')
47
+ entries = entries.where(['rating < ?', 3]).except(:order).order('children DESC')
46
48
  end
47
49
  entries
48
50
  end
@@ -95,6 +97,8 @@ class CrudTestModelsController < CrudController #:nodoc:
95
97
  end
96
98
 
97
99
  # A simple test helper to prepare the test database with a CrudTestModel model.
100
+ # This helper is used to test the CrudController and various helpers
101
+ # without the need for an application based model.
98
102
  module CrudTestHelper
99
103
 
100
104
  protected
@@ -102,29 +106,34 @@ module CrudTestHelper
102
106
  # Sets up the test database with a crud_test_models table.
103
107
  # Look at the source to view the column definition.
104
108
  def setup_db
105
- silence_stream(STDOUT) do
106
- ActiveRecord::Base.connection.create_table :crud_test_models, :force => true do |t|
107
- t.string :name, :null => false, :limit => 50
108
- t.string :whatever
109
- t.integer :children
110
- t.integer :companion_id
111
- t.float :rating
112
- t.decimal :income, :precision => 14, :scale => 2
113
- t.date :birthdate
114
- t.boolean :human, :default => true
115
- t.text :remarks
116
-
117
- t.timestamps
109
+ without_transaction do
110
+ silence_stream(STDOUT) do
111
+ ActiveRecord::Base.connection.create_table :crud_test_models, :force => true do |t|
112
+ t.string :name, :null => false, :limit => 50
113
+ t.string :whatever
114
+ t.integer :children
115
+ t.integer :companion_id
116
+ t.float :rating
117
+ t.decimal :income, :precision => 14, :scale => 2
118
+ t.date :birthdate
119
+ t.boolean :human, :default => true
120
+ t.text :remarks
121
+
122
+ t.timestamps
123
+ end
118
124
  end
125
+
126
+ CrudTestModel.reset_column_information
119
127
  end
120
-
121
- CrudTestModel.reset_column_information
122
128
  end
123
129
 
124
130
  # Removes the crud_test_models table from the database.
125
131
  def reset_db
132
+ c = ActiveRecord::Base.connection
126
133
  [:crud_test_models].each do |table|
127
- ActiveRecord::Base.connection.drop_table(table) rescue nil
134
+ if c.table_exists?(table)
135
+ c.drop_table(table) rescue nil
136
+ end
128
137
  end
129
138
  end
130
139
 
@@ -137,6 +146,16 @@ module CrudTestHelper
137
146
  def crud_test_models(name)
138
147
  CrudTestModel.find_by_name(name.to_s)
139
148
  end
149
+
150
+ def with_test_routing
151
+ with_routing do |set|
152
+ set.draw { resources :crud_test_models }
153
+ # used to define a controller in these tests
154
+ set.default_url_options = {:controller => 'crud_test_models'}
155
+ yield
156
+ end
157
+ end
158
+
140
159
 
141
160
  private
142
161
 
@@ -156,13 +175,19 @@ module CrudTestHelper
156
175
  (index + 64).chr * 5
157
176
  end
158
177
 
159
- def with_test_routing
160
- with_routing do |set|
161
- set.draw { resources :crud_test_models }
162
- # used to define a controller in these tests
163
- set.default_url_options = {:controller => 'crud_test_models'}
164
- yield
178
+ # hack to avoid ddl in transaction issues with mysql.
179
+ def without_transaction
180
+ c = ActiveRecord::Base.connection
181
+ start_transaction = false
182
+ if c.adapter_name.downcase.include?('mysql') && c.open_transactions > 0
183
+ # in transactional tests, we may simply rollback
184
+ c.execute("ROLLBACK")
185
+ start_transaction = true
165
186
  end
187
+
188
+ yield
189
+
190
+ c.execute("BEGIN") if start_transaction
166
191
  end
167
-
192
+
168
193
  end
@@ -23,6 +23,14 @@ module CustomAssertions
23
23
  assert_block(full_message) { !collection.include?(element) }
24
24
  end
25
25
 
26
+ # Asserts that <regexp> occurs exactly <expected> times in <string>.
27
+ def assert_count(expected, regexp, string, message = "")
28
+ actual = string.scan(regexp).size
29
+ full_message = build_message(message, "<?> expected to occur ? time(s), but occured ? time(s) in \n<?>.",
30
+ regexp, expected, actual, string)
31
+ assert_block(full_message) { expected == actual }
32
+ end
33
+
26
34
  # Asserts that the given active model record is valid.
27
35
  # This method used to be part of Rails but was deprecated, no idea why.
28
36
  def assert_valid(record, message = "")
@@ -2,6 +2,9 @@ require 'test_helper'
2
2
  require 'crud_test_model'
3
3
  require File.join(File.dirname(__FILE__), 'crud_controller_test_helper')
4
4
 
5
+ # Tests all actions of the CrudController based on a dummy model
6
+ # (CrudTestModel). This is useful to test the general behavior
7
+ # of CrudController.
5
8
  class CrudTestModelsControllerTest < ActionController::TestCase
6
9
 
7
10
  include CrudControllerTestHelper
@@ -27,11 +30,13 @@ class CrudTestModelsControllerTest < ActionController::TestCase
27
30
  super
28
31
  assert_equal 6, assigns(:entries).size
29
32
  assert_equal assigns(:entries).sort_by(&:name), assigns(:entries)
33
+ assert_equal Hash.new, session[:list_params]
30
34
  end
31
35
 
32
36
  def test_index_search
33
37
  super
34
38
  assert_equal 1, assigns(:entries).size
39
+ assert_equal({:q => 'AAAA'}, session[:list_params]['/crud_test_models'])
35
40
  end
36
41
 
37
42
  def test_index_with_custom_options
@@ -50,6 +55,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
50
55
  assert_present assigns(:entries)
51
56
  assert_equal 1, assigns(:entries).size
52
57
  assert_equal [CrudTestModel.find_by_name('BBBBB')], assigns(:entries)
58
+ assert_equal({:q => 'DDD'}, session[:list_params]['/crud_test_models'])
53
59
  end
54
60
 
55
61
  def test_sort_given_column
@@ -58,7 +64,8 @@ class CrudTestModelsControllerTest < ActionController::TestCase
58
64
  assert_template 'index'
59
65
  assert_present assigns(:entries)
60
66
  assert_equal 6, assigns(:entries).size
61
- assert_equal CrudTestModel.order(:children).all, assigns(:entries)
67
+ assert_equal CrudTestModel.all.sort_by(&:children), assigns(:entries)
68
+ assert_equal({:sort => 'children', :sort_dir => 'asc'}, session[:list_params]['/crud_test_models'])
62
69
  end
63
70
 
64
71
  def test_sort_virtual_column
@@ -67,8 +74,20 @@ class CrudTestModelsControllerTest < ActionController::TestCase
67
74
  assert_template 'index'
68
75
  assert_present assigns(:entries)
69
76
  assert_equal 6, assigns(:entries).size
70
- sorted = CrudTestModel.all.sort_by &:chatty
71
- assert_equal sorted.reverse.collect(&:name), assigns(:entries).collect(&:name)
77
+ assert_equal({:sort => 'chatty', :sort_dir => 'desc'}, session[:list_params]['/crud_test_models'])
78
+
79
+ sorted = CrudTestModel.all.sort_by(&:chatty)
80
+
81
+ # sort order is ambiguous, use index
82
+ names = assigns(:entries).collect(&:name)
83
+ assert names.index('BBBBB') < names.index('AAAAA')
84
+ assert names.index('BBBBB') < names.index('DDDDD')
85
+ assert names.index('EEEEE') < names.index('AAAAA')
86
+ assert names.index('EEEEE') < names.index('DDDDD')
87
+ assert names.index('AAAAA') < names.index('CCCCC')
88
+ assert names.index('AAAAA') < names.index('FFFFF')
89
+ assert names.index('DDDDD') < names.index('CCCCC')
90
+ assert names.index('DDDDD') < names.index('FFFFF')
72
91
  end
73
92
 
74
93
  def test_sort_with_search
@@ -78,6 +97,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
78
97
  assert_present assigns(:entries)
79
98
  assert_equal 3, assigns(:entries).size
80
99
  assert_equal ['CCCCC', 'DDDDD', 'BBBBB'], assigns(:entries).collect(&:name)
100
+ assert_equal({:sort => 'chatty', :sort_dir => 'asc', :q => 'DDD'}, session[:list_params]['/crud_test_models'])
81
101
  end
82
102
 
83
103
  def test_new
@@ -41,6 +41,24 @@ class CustomAssertionsTest < ActiveSupport::TestCase
41
41
  end
42
42
  end
43
43
 
44
+ test "assert count succeeds if count matches" do
45
+ assert_nothing_raised do
46
+ assert_count 3, "ba", "barbabapa"
47
+ end
48
+ end
49
+
50
+ test "assert count succeeds if count is zero" do
51
+ assert_nothing_raised do
52
+ assert_count 0, "bo", "barbabapa"
53
+ end
54
+ end
55
+
56
+ test "assert count fails if count does not match" do
57
+ assert_raise(Test::Unit::AssertionFailedError) do
58
+ assert_count 2, "ba", "barbabapa"
59
+ end
60
+ end
61
+
44
62
  test "assert valid record succeeds" do
45
63
  assert_nothing_raised do
46
64
  assert_valid crud_test_models("AAAAA")
@@ -1,9 +1,17 @@
1
1
  require 'test_helper'
2
2
  require 'crud_test_model'
3
+ require 'custom_assertions'
3
4
 
4
5
  class CrudHelperTest < ActionView::TestCase
5
6
 
7
+ REGEXP_ROWS = /<tr.+?<\/tr>/m
8
+ REGEXP_HEADERS = /<th.+?<\/th>/m
9
+ REGEXP_SORT_HEADERS = /<th><a .*?sort_dir=asc.*?>.*?<\/a><\/th>/m
10
+ REGEXP_ACTION_CELL = /<td class=\"center\"><a href.+?<\/a><\/td>/m
11
+
12
+ include CustomAssertions
6
13
  include StandardHelper
14
+ include ListHelper
7
15
  include CrudTestHelper
8
16
 
9
17
  setup :reset_db, :setup_db, :create_test_data
@@ -16,9 +24,9 @@ class CrudHelperTest < ActionView::TestCase
16
24
  crud_table
17
25
  end
18
26
 
19
- assert_match /(<tr.+?<\/tr>){7}/m, t
20
- assert_match /(<th><a .*asc.*>.*<\/a><\/th>){4}/m, t
21
- assert_match /(<td><a href.+?<\/a><\/td>.*?){18}/m, t # show, edit, delete links
27
+ assert_count 7, REGEXP_ROWS, t
28
+ assert_count 11, REGEXP_SORT_HEADERS, t
29
+ assert_count 18, REGEXP_ACTION_CELL, t # show, edit, delete links
22
30
  end
23
31
 
24
32
  test "custom crud table with attributes" do
@@ -28,9 +36,9 @@ class CrudHelperTest < ActionView::TestCase
28
36
  crud_table :name, :children, :companion_id
29
37
  end
30
38
 
31
- assert_match /(<tr.+?<\/tr>){7}/m, t
32
- assert_match /(<th.+?<\/th>){4}/m, t
33
- assert_match /(<td><a href.+?<\/a><\/td>.*?){18}/m, t # show, edit, delete links
39
+ assert_count 7, REGEXP_ROWS, t
40
+ assert_count 3, REGEXP_SORT_HEADERS, t
41
+ assert_count 18, REGEXP_ACTION_CELL, t # show, edit, delete links
34
42
  end
35
43
 
36
44
  test "custom crud table with block" do
@@ -43,10 +51,10 @@ class CrudHelperTest < ActionView::TestCase
43
51
  end
44
52
  end
45
53
 
46
- assert_match /(<tr.+?<\/tr>){7}/m, t
47
- assert_match /(<th.+?<\/th>){4}/m, t
48
- assert_match /(<span>.+?<\/span>.*?){6}/m, t
49
- assert_no_match /(<td><a href.+?<\/a><\/td>.*?)/m, t
54
+ assert_count 7, REGEXP_ROWS, t
55
+ assert_count 4, REGEXP_HEADERS, t
56
+ assert_count 6, /<span>.+?<\/span>/m, t
57
+ assert_count 0, REGEXP_ACTION_CELL, t # no show, edit, delete links
50
58
  end
51
59
 
52
60
  test "custom crud table with attributes and block" do
@@ -58,62 +66,12 @@ class CrudHelperTest < ActionView::TestCase
58
66
  end
59
67
  end
60
68
 
61
- assert_match /(<tr.+?<\/tr>){7}/m, t
62
- assert_match /(<th.+?<\/th>){4}/m, t
63
- assert_match /(<span>.+?<\/span>.*?){6}/m, t
64
- assert_no_match /(<td><a href.+?<\/a><\/td>.*?)/m, t
69
+ assert_count 7, REGEXP_ROWS, t
70
+ assert_count 4, REGEXP_HEADERS, t
71
+ assert_count 6, /<span>.+?<\/span>/m, t
72
+ assert_count 0, REGEXP_ACTION_CELL, t # no show, edit, delete links
65
73
  end
66
-
67
- test "standard crud table with ascending sort params" do
68
- def params
69
- {:sort => 'children', :sort_dir => 'asc'}
70
- end
71
74
 
72
- @entries = CrudTestModel.all
73
-
74
- t = with_test_routing do
75
- crud_table
76
- end
77
-
78
- assert_match /(<tr.+?<\/tr>){7}/m, t
79
- assert_match /(<th><a .*desc.*>Children<\/a> &darr;<\/th>){1}/m, t
80
- assert_match /(<th><a .*asc.*>.*<\/a><\/th>){3}/m, t
81
- assert_match /(<td><a href.+?<\/a><\/td>.*?){18}/m, t
82
- end
83
-
84
- test "standard crud table with descending sort params" do
85
- def params
86
- {:sort => 'children', :sort_dir => 'desc'}
87
- end
88
-
89
- @entries = CrudTestModel.all
90
-
91
- t = with_test_routing do
92
- crud_table
93
- end
94
-
95
- assert_match /(<tr.+?<\/tr>){7}/m, t
96
- assert_match /(<th><a .*asc.*>Children<\/a> &uarr;<\/th>){1}/m, t
97
- assert_match /(<th><a .*asc.*>.*<\/a><\/th>){4}/m, t
98
- assert_match /(<td><a href.+?<\/a><\/td>.*?){18}/m, t
99
- end
100
-
101
- test "crud table with custom column sort params" do
102
- def params
103
- {:sort => 'chatty', :sort_dir => 'asc'}
104
- end
105
-
106
- @entries = CrudTestModel.all
107
-
108
- t = with_test_routing do
109
- crud_table :name, :children, :chatty
110
- end
111
-
112
- assert_match /(<tr.+?<\/tr>){7}/m, t
113
- assert_match /(<th><a .*desc.*>Chatty<\/a> &darr;<\/th>){1}/m, t
114
- assert_match /(<th><a .*asc.*>.*<\/a><\/th>){2}/m, t
115
- assert_match /(<td><a href.+?<\/a><\/td>.*?){18}/m, t
116
- end
117
75
 
118
76
  test "crud form" do
119
77
  @entry = CrudTestModel.first
@@ -152,15 +110,4 @@ class CrudHelperTest < ActionView::TestCase
152
110
  true
153
111
  end
154
112
 
155
-
156
- private
157
-
158
- def with_test_routing
159
- with_routing do |set|
160
- set.draw { resources :crud_test_models }
161
- # used to define a controller in these tests
162
- set.default_url_options = {:controller => 'crud_test_models'}
163
- yield
164
- end
165
- end
166
113
  end
@@ -0,0 +1,139 @@
1
+ require 'test_helper'
2
+ require 'crud_test_model'
3
+ require 'custom_assertions'
4
+
5
+ class ListHelperTest < ActionView::TestCase
6
+
7
+ REGEXP_ROWS = /<tr.+?<\/tr>/m
8
+ REGEXP_HEADERS = /<th.+?<\/th>/m
9
+ REGEXP_SORT_HEADERS = /<th><a .*?sort_dir=asc.*?>.*?<\/a><\/th>/m
10
+
11
+ include StandardHelper
12
+ include CrudTestHelper
13
+ include CustomAssertions
14
+
15
+ setup :reset_db, :setup_db, :create_test_data
16
+ teardown :reset_db
17
+
18
+ test "standard list table" do
19
+ @entries = CrudTestModel.all
20
+
21
+ t = with_test_routing do
22
+ list_table
23
+ end
24
+
25
+ assert_count 7, REGEXP_ROWS, t
26
+ assert_count 11, REGEXP_SORT_HEADERS, t
27
+ end
28
+
29
+ test "custom list table with attributes" do
30
+ @entries = CrudTestModel.all
31
+
32
+ t = with_test_routing do
33
+ list_table :name, :children, :companion_id
34
+ end
35
+
36
+ assert_count 7, REGEXP_ROWS, t
37
+ assert_count 3, REGEXP_SORT_HEADERS, t
38
+ end
39
+
40
+ test "custom list table with block" do
41
+ @entries = CrudTestModel.all
42
+
43
+ t = with_test_routing do
44
+ list_table do |t|
45
+ t.attrs :name, :children, :companion_id
46
+ t.col("head") {|e| content_tag :span, e.income.to_s }
47
+ end
48
+ end
49
+
50
+ assert_count 7, REGEXP_ROWS, t
51
+ assert_count 4, REGEXP_HEADERS, t
52
+ assert_count 0, REGEXP_SORT_HEADERS, t
53
+ assert_count 6, /<span>.+?<\/span>/, t
54
+ end
55
+
56
+ test "custom list table with attributes and block" do
57
+ @entries = CrudTestModel.all
58
+
59
+ t = with_test_routing do
60
+ list_table :name, :children, :companion_id do |t|
61
+ t.col("head") {|e| content_tag :span, e.income.to_s }
62
+ end
63
+ end
64
+
65
+ assert_count 7, REGEXP_ROWS, t
66
+ assert_count 3, REGEXP_SORT_HEADERS, t
67
+ assert_count 4, REGEXP_HEADERS, t
68
+ assert_count 6, /<span>.+?<\/span>/, t
69
+ end
70
+
71
+ test "standard list table with ascending sort params" do
72
+ def params
73
+ {:sort => 'children', :sort_dir => 'asc'}
74
+ end
75
+
76
+ @entries = CrudTestModel.all
77
+
78
+ t = with_test_routing do
79
+ list_table
80
+ end
81
+
82
+ assert_count 7, REGEXP_ROWS, t
83
+ assert_count 10, REGEXP_SORT_HEADERS, t
84
+ assert_count 1, /<th><a .*?sort_dir=desc.*?>Children<\/a> &darr;<\/th>/, t
85
+ end
86
+
87
+ test "standard list table with descending sort params" do
88
+ def params
89
+ {:sort => 'children', :sort_dir => 'desc'}
90
+ end
91
+
92
+ @entries = CrudTestModel.all
93
+
94
+ t = with_test_routing do
95
+ list_table
96
+ end
97
+
98
+ assert_count 7, REGEXP_ROWS, t
99
+ assert_count 10, REGEXP_SORT_HEADERS, t
100
+ assert_count 1, /<th><a .*?sort_dir=asc.*?>Children<\/a> &uarr;<\/th>/, t
101
+ end
102
+
103
+ test "list table with custom column sort params" do
104
+ def params
105
+ {:sort => 'chatty', :sort_dir => 'asc'}
106
+ end
107
+
108
+ @entries = CrudTestModel.all
109
+
110
+ t = with_test_routing do
111
+ list_table :name, :children, :chatty
112
+ end
113
+
114
+ assert_count 7, REGEXP_ROWS, t
115
+ assert_count 2, REGEXP_SORT_HEADERS, t
116
+ assert_count 1, /<th><a .*?sort_dir=desc.*?>Chatty<\/a> &darr;<\/th>/, t
117
+ end
118
+
119
+ test "default attributes do not include id" do
120
+ assert_equal [:name, :whatever, :children, :companion_id, :rating, :income,
121
+ :birthdate, :human, :remarks, :created_at, :updated_at], default_attrs
122
+ end
123
+
124
+ # Controller helper methods for the tests
125
+
126
+ def model_class
127
+ CrudTestModel
128
+ end
129
+
130
+ def params
131
+ {}
132
+ end
133
+
134
+ def sortable?(attr)
135
+ true
136
+ end
137
+
138
+
139
+ end
@@ -58,30 +58,27 @@ class StandardFormBuilderTest < ActionView::TestCase
58
58
 
59
59
  test "belongs_to_field has all options by default" do
60
60
  f = form.belongs_to_field(:companion_id)
61
- assert_match /(\<option .*?){7}/m, f
62
- assert_no_match /(\<option .*?){8}/m, f
61
+ assert_equal 7, f.scan('</option>').size
63
62
  end
64
63
 
65
64
  test "belongs_to_field with :list option" do
66
65
  list = CrudTestModel.all
67
66
  f = form.belongs_to_field(:companion_id, :list => [list.first, list.second])
68
- assert_match /(\<option .*?){3}/m, f
69
- assert_no_match /(\<option .*?){4}/m, f
67
+ assert_equal 3, f.scan('</option>').size
70
68
  end
71
69
 
72
70
  test "belongs_to_field with instance variable" do
73
71
  list = CrudTestModel.all
74
72
  @companions = [list.first, list.second]
75
73
  f = form.belongs_to_field(:companion_id)
76
- assert_match /(\<option .*?){3}/m, f
77
- assert_no_match /(\<option .*?){4}/m, f
74
+ assert_equal 3, f.scan('</option>').size
78
75
  end
79
76
 
80
77
  test "belongs_to_field with empty list" do
81
78
  @companions = []
82
79
  f = form.belongs_to_field(:companion_id)
83
80
  assert_match /none available/m, f
84
- assert_no_match /(\<option .*?)/m, f
81
+ assert_equal 0, f.scan('</option>').size
85
82
  end
86
83
 
87
84
  test "string_field sets maxlength attribute if limit" do