dry_crud 1.2.7 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. data/README.rdoc +60 -27
  2. data/Rakefile +3 -1
  3. data/VERSION +1 -1
  4. data/lib/generators/dry_crud/dry_crud_generator.rb +3 -3
  5. data/lib/generators/dry_crud/templates/INSTALL +3 -1
  6. data/lib/generators/dry_crud/templates/app/controllers/crud_controller.rb +106 -90
  7. data/lib/generators/dry_crud/templates/app/controllers/list_controller.rb +90 -74
  8. data/lib/generators/dry_crud/templates/app/controllers/render_inheritable.rb +34 -33
  9. data/lib/generators/dry_crud/templates/app/helpers/crud_helper.rb +39 -23
  10. data/lib/generators/dry_crud/templates/app/helpers/list_helper.rb +11 -9
  11. data/lib/generators/dry_crud/templates/app/helpers/standard_form_builder.rb +55 -47
  12. data/lib/generators/dry_crud/templates/app/helpers/standard_helper.rb +134 -86
  13. data/lib/generators/dry_crud/templates/app/helpers/standard_table_builder.rb +41 -35
  14. data/lib/generators/dry_crud/templates/app/views/crud/_actions_edit.html.erb +1 -0
  15. data/lib/generators/dry_crud/templates/app/views/crud/edit.html.erb +3 -3
  16. data/lib/generators/dry_crud/templates/app/views/crud/new.html.erb +2 -2
  17. data/lib/generators/dry_crud/templates/app/views/crud/show.html.erb +3 -3
  18. data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.erb +9 -7
  19. data/lib/generators/dry_crud/templates/app/views/list/_search.html.erb +1 -1
  20. data/lib/generators/dry_crud/templates/app/views/list/index.html.erb +4 -4
  21. data/lib/generators/dry_crud/templates/app/views/shared/_error_messages.html.erb +3 -1
  22. data/lib/generators/dry_crud/templates/config/locales/en_crud.yml +63 -0
  23. data/lib/generators/dry_crud/templates/test/crud_test_model.rb +93 -58
  24. data/lib/generators/dry_crud/templates/test/custom_assertions.rb +24 -13
  25. data/lib/generators/dry_crud/templates/test/functional/crud_controller_test_helper.rb +26 -56
  26. data/lib/generators/dry_crud/templates/test/functional/crud_test_models_controller_test.rb +47 -41
  27. data/lib/generators/dry_crud/templates/test/unit/custom_assertions_test.rb +28 -24
  28. data/lib/generators/dry_crud/templates/test/unit/helpers/crud_helper_test.rb +20 -34
  29. data/lib/generators/dry_crud/templates/test/unit/helpers/list_helper_test.rb +39 -53
  30. data/lib/generators/dry_crud/templates/test/unit/helpers/render_inheritable_test.rb +33 -33
  31. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_form_builder_test.rb +27 -27
  32. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_helper_test.rb +103 -50
  33. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_table_builder_test.rb +52 -24
  34. data/test/templates/Gemfile +34 -0
  35. data/test/templates/app/controllers/ajax_controller.rb +3 -3
  36. data/test/templates/app/controllers/application_controller.rb +1 -1
  37. data/test/templates/app/controllers/cities_controller.rb +2 -5
  38. data/test/templates/app/controllers/people_controller.rb +5 -5
  39. data/test/templates/app/controllers/vips_controller.rb +6 -11
  40. data/test/templates/app/helpers/people_helper.rb +2 -2
  41. data/test/templates/app/models/city.rb +9 -9
  42. data/test/templates/app/models/person.rb +5 -4
  43. data/test/templates/app/views/ajax/_actions_index.html.erb +2 -2
  44. data/test/templates/app/views/cities/_form.html.erb +5 -1
  45. data/test/templates/app/views/layouts/_menu.html.erb +3 -3
  46. data/test/templates/app/views/people/_attrs.html.erb +3 -3
  47. data/test/templates/config/database.yml +22 -0
  48. data/test/templates/config/locales/en_cities.yml +56 -0
  49. data/test/templates/config/routes.rb +5 -5
  50. data/test/templates/db/migrate/20100511174904_create_people_and_cities.rb +5 -2
  51. data/test/templates/db/seeds.rb +38 -29
  52. data/test/templates/test/functional/cities_controller_test.rb +12 -12
  53. data/test/templates/test/functional/people_controller_test.rb +10 -10
  54. metadata +11 -7
@@ -2,57 +2,57 @@ require 'test_helper'
2
2
  require 'crud_test_model'
3
3
 
4
4
  class StandardFormBuilderTest < ActionView::TestCase
5
-
5
+
6
6
  include CrudTestHelper
7
-
7
+
8
8
  # set dummy helper class for ActionView::TestCase
9
9
  self.helper_class = StandardHelper
10
-
10
+
11
11
  attr_reader :form, :entry
12
-
12
+
13
13
  setup :reset_db, :setup_db, :create_test_data, :create_form
14
14
  teardown :reset_db
15
-
15
+
16
16
  def create_form
17
17
  @entry = CrudTestModel.first
18
18
  @form = StandardFormBuilder.new(:entry, @entry, self, {}, lambda {|form| form })
19
19
  end
20
-
20
+
21
21
  test "input_field dispatches string attr to string_field" do
22
22
  assert_equal form.string_field(:name), form.input_field(:name)
23
23
  assert form.string_field(:name).html_safe?
24
24
  end
25
-
25
+
26
26
  test "input_field dispatches password attr to password_field" do
27
27
  assert_equal form.password_field(:password), form.input_field(:password)
28
28
  assert form.password_field(:name).html_safe?
29
29
  end
30
-
30
+
31
31
  test "input_field dispatches text attr to text_area" do
32
32
  assert_equal form.text_area(:remarks), form.input_field(:remarks)
33
33
  assert form.text_area(:remarks).html_safe?
34
34
  end
35
-
35
+
36
36
  test "input_field dispatches integer attr to integer_field" do
37
37
  assert_equal form.integer_field(:children), form.input_field(:children)
38
38
  assert form.integer_field(:children).html_safe?
39
39
  end
40
-
40
+
41
41
  test "input_field dispatches boolean attr to boolean_field" do
42
42
  assert_equal form.boolean_field(:human), form.input_field(:human)
43
43
  assert form.boolean_field(:human).html_safe?
44
- end
45
-
44
+ end
45
+
46
46
  test "input_field dispatches date attr to date_field" do
47
47
  assert_equal form.date_field(:birthdate), form.input_field(:birthdate)
48
48
  assert form.date_field(:birthdate).html_safe?
49
49
  end
50
-
50
+
51
51
  test "input_field dispatches belongs_to attr to select field" do
52
52
  assert_equal form.belongs_to_field(:companion_id), form.input_field(:companion_id)
53
53
  assert form.belongs_to_field(:companion_id).html_safe?
54
54
  end
55
-
55
+
56
56
  test "input_fields concats multiple fields" do
57
57
  result = form.labeled_input_fields(:name, :remarks, :children)
58
58
  assert result.html_safe?
@@ -60,75 +60,75 @@ class StandardFormBuilderTest < ActionView::TestCase
60
60
  assert result.include?(form.input_field(:remarks))
61
61
  assert result.include?(form.input_field(:children))
62
62
  end
63
-
63
+
64
64
  test "labeld_input_field adds required mark" do
65
65
  result = form.labeled_input_field(:name)
66
66
  assert result.include?(StandardFormBuilder::REQUIRED_MARK)
67
67
  result = form.labeled_input_field(:remarks)
68
68
  assert !result.include?(StandardFormBuilder::REQUIRED_MARK)
69
69
  end
70
-
70
+
71
71
  test "belongs_to_field has all options by default" do
72
72
  f = form.belongs_to_field(:companion_id)
73
73
  assert_equal 7, f.scan('</option>').size
74
74
  end
75
-
75
+
76
76
  test "belongs_to_field with :list option" do
77
77
  list = CrudTestModel.all
78
78
  f = form.belongs_to_field(:companion_id, :list => [list.first, list.second])
79
79
  assert_equal 3, f.scan('</option>').size
80
80
  end
81
-
81
+
82
82
  test "belongs_to_field with instance variable" do
83
83
  list = CrudTestModel.all
84
84
  @companions = [list.first, list.second]
85
85
  f = form.belongs_to_field(:companion_id)
86
86
  assert_equal 3, f.scan('</option>').size
87
87
  end
88
-
88
+
89
89
  test "belongs_to_field with empty list" do
90
90
  @companions = []
91
91
  f = form.belongs_to_field(:companion_id)
92
92
  assert_match /none available/m, f
93
93
  assert_equal 0, f.scan('</option>').size
94
94
  end
95
-
95
+
96
96
  test "string_field sets maxlength attribute if limit" do
97
97
  assert_match /maxlength="50"/, form.string_field(:name)
98
98
  end
99
-
99
+
100
100
  test "label creates captionized label" do
101
101
  assert_match /label for.+Gugus dada/, form.label(:gugus_dada)
102
102
  assert form.label(:gugus_dada).html_safe?
103
103
  end
104
-
104
+
105
105
  test "classic label still works" do
106
106
  assert_match /label for.+hoho/, form.label(:gugus_dada, "hoho")
107
107
  assert form.label(:gugus_dada, "hoho").html_safe?
108
108
  end
109
-
109
+
110
110
  test "labeled_text_field create label" do
111
111
  assert_match /label for.+input/m, form.labeled_string_field(:name)
112
112
  assert form.labeled_string_field(:name).html_safe?
113
113
  end
114
-
114
+
115
115
  test "labeled field creates label" do
116
116
  result = form.labeled("gugus", "<input type='text' name='gugus' />")
117
117
  assert result.html_safe?
118
118
  assert_match /label for.+input/m, result
119
119
  end
120
-
120
+
121
121
  test "required mark is shown" do
122
122
  assert_equal StandardFormBuilder::REQUIRED_MARK, form.required_mark(:name)
123
123
  assert_equal "", form.required_mark(:rating)
124
124
  assert_equal "", form.required_mark(:anything)
125
125
  end
126
-
126
+
127
127
  test "method missing still works" do
128
128
  assert_raise(NoMethodError) do
129
129
  form.blabla
130
130
  end
131
- end
131
+ end
132
132
 
133
133
  test "respond to still works" do
134
134
  assert !form.respond_to?(:blalba)
@@ -5,41 +5,41 @@ class StandardHelperTest < ActionView::TestCase
5
5
 
6
6
  include StandardHelper
7
7
  include CrudTestHelper
8
-
8
+
9
9
  setup :reset_db, :setup_db, :create_test_data
10
10
  teardown :reset_db
11
-
11
+
12
12
  def format_size(obj)
13
13
  "#{f(obj.size)} chars"
14
14
  end
15
-
15
+
16
16
  test "labeled text as block" do
17
17
  result = labeled("label") { "value" }
18
-
18
+
19
19
  assert result.html_safe?
20
20
  assert_dom_equal "<div class='labeled'> <div class='caption'>label</div> <div class='value'>value</div> </div>", result.squish
21
21
  end
22
22
 
23
23
  test "labeled text empty" do
24
24
  result = labeled("label", "")
25
-
25
+
26
26
  assert result.html_safe?
27
27
  assert_dom_equal "<div class='labeled'> <div class='caption'>label</div> <div class='value'>#{EMPTY_STRING}</div> </div>", result.squish
28
28
  end
29
29
 
30
30
  test "labeled text as content" do
31
31
  result = labeled("label", "value <unsafe>")
32
-
32
+
33
33
  assert result.html_safe?
34
34
  assert_dom_equal "<div class='labeled'> <div class='caption'>label</div> <div class='value'>value &lt;unsafe&gt;</div> </div>", result.squish
35
35
  end
36
-
36
+
37
37
  test "labeled attr" do
38
- result = labeled_attr('foo', :size)
39
- assert result.html_safe?
40
- assert_dom_equal "<div class='labeled'> <div class='caption'>Size</div> <div class='value'>3 chars</div> </div>", result.squish
38
+ result = labeled_attr('foo', :size)
39
+ assert result.html_safe?
40
+ assert_dom_equal "<div class='labeled'> <div class='caption'>Size</div> <div class='value'>3 chars</div> </div>", result.squish
41
41
  end
42
-
42
+
43
43
  test "alternate row" do
44
44
  result_1 = tr_alt { "(test row content)" }
45
45
  result_2 = tr_alt { "(test row content)" }
@@ -49,29 +49,29 @@ class StandardHelperTest < ActionView::TestCase
49
49
  assert_dom_equal "<tr class='even'>(test row content)</tr>", result_1
50
50
  assert_dom_equal "<tr class='odd'>(test row content)</tr>", result_2
51
51
  end
52
-
52
+
53
53
  test "format Fixnums" do
54
54
  assert_equal "0", f(0)
55
55
  assert_equal "10", f(10)
56
- assert_equal "10,000,000", f(10000000)
56
+ assert_equal "10,000,000", f(10000000)
57
57
  end
58
-
58
+
59
59
  test "format Floats" do
60
60
  assert_equal "1.00", f(1.0)
61
61
  assert_equal "1.20", f(1.2)
62
62
  assert_equal "3.14", f(3.14159)
63
63
  end
64
-
64
+
65
65
  test "format Booleans" do
66
66
  assert_equal "yes", f(true)
67
67
  assert_equal "no", f(false)
68
68
  end
69
-
69
+
70
70
  test "format nil" do
71
71
  assert EMPTY_STRING.html_safe?
72
72
  assert_equal EMPTY_STRING, f(nil)
73
73
  end
74
-
74
+
75
75
  test "format Strings" do
76
76
  assert_equal "blah blah", f("blah blah")
77
77
  assert_equal "<injection>", f("<injection>")
@@ -81,112 +81,123 @@ class StandardHelperTest < ActionView::TestCase
81
81
  test "format attr with fallthrough to f" do
82
82
  assert_equal "12.23", format_attr("12.23424", :to_f)
83
83
  end
84
-
84
+
85
85
  test "format attr with custom format_size method" do
86
86
  assert_equal "4 chars", format_attr("abcd", :size)
87
87
  end
88
-
88
+
89
89
  test "column types" do
90
90
  m = crud_test_models(:AAAAA)
91
91
  assert_equal :string, column_type(m, :name)
92
92
  assert_equal :integer, column_type(m, :children)
93
93
  assert_equal :integer, column_type(m, :companion_id)
94
94
  assert_equal nil, column_type(m, :companion)
95
- assert_equal :float, column_type(m, :rating)
95
+ if ActiveRecord::Base.connection.adapter_name == 'SQLite' && defined?(JRUBY_VERSION)
96
+ assert_equal :decimal, column_type(m, :rating)
97
+ else
98
+ assert_equal :float, column_type(m, :rating)
99
+ end
96
100
  assert_equal :decimal, column_type(m, :income)
97
101
  assert_equal :date, column_type(m, :birthdate)
102
+ assert_equal :time, column_type(m, :gets_up_at)
103
+ assert_equal :datetime, column_type(m, :last_seen)
98
104
  assert_equal :boolean, column_type(m, :human)
99
105
  assert_equal :text, column_type(m, :remarks)
100
106
  end
101
-
107
+
102
108
  test "format integer column" do
103
109
  m = crud_test_models(:AAAAA)
104
110
  assert_equal '9', format_type(m, :children)
105
-
111
+
106
112
  m.children = 10000
107
113
  assert_equal '10,000', format_type(m, :children)
108
114
  end
109
-
115
+
110
116
  test "format float column" do
111
117
  m = crud_test_models(:AAAAA)
112
118
  assert_equal '1.10', format_type(m, :rating)
113
-
119
+
114
120
  m.rating = 3.145001 # you never know with these floats..
115
121
  assert_equal '3.15', format_type(m, :rating)
116
122
  end
117
-
123
+
118
124
  test "format decimal column" do
119
125
  m = crud_test_models(:AAAAA)
120
126
  assert_equal '10000000.10', format_type(m, :income)
121
127
  end
122
-
128
+
123
129
  test "format date column" do
124
130
  m = crud_test_models(:AAAAA)
125
131
  assert_equal '1910-01-01', format_type(m, :birthdate)
126
132
  end
127
-
128
- test "format datetime column" do
133
+
134
+ test "format time column" do
129
135
  m = crud_test_models(:AAAAA)
130
- assert_equal f(m.created_at.to_date) + " " + f(m.created_at.to_time), format_type(m, :created_at)
136
+ assert_equal '01:01', format_type(m, :gets_up_at)
131
137
  end
132
138
 
139
+ test "format datetime column" do
140
+ m = crud_test_models(:AAAAA)
141
+ assert_equal "2010-01-01 11:21", format_type(m, :last_seen)
142
+ end
143
+
133
144
  test "format text column" do
134
145
  m = crud_test_models(:AAAAA)
135
146
  assert_equal "<p>AAAAA BBBBB CCCCC\n<br />AAAAA BBBBB CCCCC\n</p>", format_type(m, :remarks)
136
147
  assert format_type(m, :remarks).html_safe?
137
148
  end
138
-
149
+
139
150
  test "empty table should render message" do
140
151
  result = table([]) { }
141
152
  assert result.html_safe?
142
- assert_dom_equal "<div class='list'>#{NO_LIST_ENTRIES_MESSAGE}</div>", result
143
- end
144
-
153
+ assert_dom_equal "<div class='list'>No entries found.</div>", result
154
+ end
155
+
145
156
  test "non empty table should render table" do
146
157
  result = table(['foo', 'bar']) {|t| t.attrs :size, :upcase }
147
158
  assert result.html_safe?
148
159
  assert_match(/^\<table.*\<\/table\>$/, result)
149
160
  end
150
-
161
+
151
162
  test "table with attrs" do
152
163
  expected = StandardTableBuilder.table(['foo', 'bar'], self) { |t| t.attrs :size, :upcase }
153
164
  actual = table(['foo', 'bar'], :size, :upcase)
154
165
  assert actual.html_safe?
155
166
  assert_equal expected, actual
156
167
  end
157
-
168
+
158
169
  test "captionize" do
159
170
  assert_equal "Camel Case", captionize(:camel_case)
160
171
  assert_equal "All Upper Case", captionize("all upper case")
161
172
  assert_equal "With Object", captionize("With object", Object.new)
162
173
  assert !captionize('bad <title>').html_safe?
163
174
  end
164
-
175
+
165
176
  test "standard form for existing entry" do
166
177
  e = crud_test_models('AAAAA')
167
- f = with_test_routing do
168
- f = capture { standard_form(e, [:name, :children, :birthdate, :human], :class => 'special') }
178
+ f = with_test_routing do
179
+ f = capture { standard_form(e, :name, :children, :birthdate, :human, :class => 'special') }
169
180
  end
170
-
181
+
171
182
  assert_match /form .*?action="\/crud_test_models\/#{e.id}" .*?method="post"/, f
172
183
  assert_match /input .*?name="_method" .*?type="hidden" .*?value="put"/, f
173
184
  assert_match /input .*?name="crud_test_model\[name\]" .*?type="text" .*?value="AAAAA"/, f
174
185
  assert_match /select .*?name="crud_test_model\[birthdate\(1i\)\]"/, f
175
- assert_match /option selected="selected" value="1910">1910<\/option>/, f
186
+ assert_match /option selected="selected" value="1910">1910<\/option>/, f
176
187
  assert_match /option selected="selected" value="1">January<\/option>/, f
177
188
  assert_match /option selected="selected" value="1">1<\/option>/, f
178
189
  assert_match /input .*?name="crud_test_model\[children\]" .*?type="number" .*?value=\"9\"/, f
179
190
  assert_match /input .*?name="crud_test_model\[human\]" .*?type="checkbox"/, f
180
191
  assert_match /input .*?type="submit" .*?value="Save"/, f
181
192
  end
182
-
193
+
183
194
  test "standard form for new entry" do
184
195
  e = CrudTestModel.new
185
- f = with_test_routing do
186
- f = capture { standard_form(e, [:name, :children, :birthdate, :human], :class => 'special') }
196
+ f = with_test_routing do
197
+ f = capture { standard_form(e, :name, :children, :birthdate, :human, :html => {:class => 'special'}) }
187
198
  end
188
-
189
- assert_match /form .*?action="\/crud_test_models" .*?method="post"/, f
199
+
200
+ assert_match /form .*?action="\/crud_test_models" .?class="special" .*?method="post"/, f
190
201
  assert_match /input .*?name="crud_test_model\[name\]" .*?type="text"/, f
191
202
  assert_no_match /input .*?name="crud_test_model\[name\]" .*?type="text" .*?value=/, f
192
203
  assert_match /select .*?name="crud_test_model\[birthdate\(1i\)\]"/, f
@@ -194,16 +205,16 @@ class StandardHelperTest < ActionView::TestCase
194
205
  assert_no_match /input .*?name="crud_test_model\[children\]" .*?type="text" .*?value=/, f
195
206
  assert_match /input .*?type="submit" .*?value="Save"/, f
196
207
  end
197
-
208
+
198
209
  test "standard form with errors" do
199
210
  e = crud_test_models('AAAAA')
200
211
  e.name = nil
201
212
  assert !e.valid?
202
-
203
- f = with_test_routing do
204
- f = capture { standard_form(e, [:name, :children, :birthdate, :human], :class => 'special') }
213
+
214
+ f = with_test_routing do
215
+ f = capture { standard_form(e, :name, :children, :birthdate, :human) }
205
216
  end
206
-
217
+
207
218
  assert_match /form .*?action="\/crud_test_models\/#{e.id}" .*?method="post"/, f
208
219
  assert_match /input .*?name="_method" .*?type="hidden" .*?value="put"/, f
209
220
  assert_match /div id="error_explanation"/, f
@@ -217,4 +228,46 @@ class StandardHelperTest < ActionView::TestCase
217
228
  assert_match /input .*?type="submit" .*?value="Save"/, f
218
229
  end
219
230
 
231
+ test "translate inheritable lookup" do
232
+ # current controller is :crud_test_models, action is :index
233
+ @controller = CrudTestModelsController.new
234
+
235
+ I18n.backend.store_translations :en, :global => { :test_key => 'global' }
236
+ assert_equal 'global', ti(:test_key)
237
+
238
+ I18n.backend.store_translations :en, :list => { :global => {:test_key => 'list global'} }
239
+ assert_equal 'list global', ti(:test_key)
240
+
241
+ I18n.backend.store_translations :en, :list => { :index => {:test_key => 'list index'} }
242
+ assert_equal 'list index', ti(:test_key)
243
+
244
+ I18n.backend.store_translations :en, :crud => { :global => {:test_key => 'crud global'} }
245
+ assert_equal 'crud global', ti(:test_key)
246
+
247
+ I18n.backend.store_translations :en, :crud => { :index => {:test_key => 'crud index'} }
248
+ assert_equal 'crud index', ti(:test_key)
249
+
250
+ I18n.backend.store_translations :en, :crud_test_models => { :global => {:test_key => 'test global'} }
251
+ assert_equal 'test global', ti(:test_key)
252
+
253
+ I18n.backend.store_translations :en, :crud_test_models => { :index => {:test_key => 'test index'} }
254
+ assert_equal 'test index', ti(:test_key)
255
+ end
256
+
257
+ test "translate association lookup" do
258
+ assoc = CrudTestModel.reflect_on_association(:companion)
259
+
260
+ I18n.backend.store_translations :en, :global => { :associations => {:test_key => 'global'} }
261
+ assert_equal 'global', ta(:test_key, assoc)
262
+
263
+ I18n.backend.store_translations :en, :activerecord => { :associations => { :crud_test_model => {:test_key => 'model'} } }
264
+ assert_equal 'model', ta(:test_key, assoc)
265
+
266
+ I18n.backend.store_translations :en, :activerecord => { :associations => { :models => {
267
+ :crud_test_model => { :companion => {:test_key => 'companion'} } } } }
268
+ assert_equal 'companion', ta(:test_key, assoc)
269
+
270
+ assert_equal 'global', ta(:test_key)
271
+ end
272
+
220
273
  end