dry_crud 1.7.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/MIT-LICENSE +1 -1
- data/README.rdoc +126 -87
- data/VERSION +1 -1
- data/lib/generators/dry_crud/dry_crud_generator.rb +42 -22
- data/lib/generators/dry_crud/templates/INSTALL +5 -5
- data/lib/generators/dry_crud/templates/app/assets/stylesheets/crud.scss +0 -20
- data/lib/generators/dry_crud/templates/app/assets/stylesheets/sample.scss +24 -4
- data/lib/generators/dry_crud/templates/app/controllers/crud/generic_model.rb +89 -0
- data/lib/generators/dry_crud/templates/app/controllers/crud/nestable.rb +70 -0
- data/lib/generators/dry_crud/templates/app/controllers/crud/rememberable.rb +64 -0
- data/lib/generators/dry_crud/templates/app/controllers/crud/render_callbacks.rb +46 -0
- data/lib/generators/dry_crud/templates/app/controllers/crud/responder.rb +31 -0
- data/lib/generators/dry_crud/templates/app/controllers/crud/searchable.rb +55 -0
- data/lib/generators/dry_crud/templates/app/controllers/crud/sortable.rb +63 -0
- data/lib/generators/dry_crud/templates/app/controllers/crud_controller.rb +66 -69
- data/lib/generators/dry_crud/templates/app/controllers/list_controller.rb +23 -326
- data/lib/generators/dry_crud/templates/app/helpers/actions_helper.rb +64 -0
- data/lib/generators/dry_crud/templates/app/helpers/crud/form_builder.rb +331 -0
- data/lib/generators/dry_crud/templates/app/helpers/crud/table_builder.rb +280 -0
- data/lib/generators/dry_crud/templates/app/helpers/form_helper.rb +52 -0
- data/lib/generators/dry_crud/templates/app/helpers/format_helper.rb +164 -0
- data/lib/generators/dry_crud/templates/app/helpers/i18n_helper.rb +85 -0
- data/lib/generators/dry_crud/templates/app/helpers/table_helper.rb +83 -0
- data/lib/generators/dry_crud/templates/app/helpers/utility_helper.rb +84 -0
- data/lib/generators/dry_crud/templates/app/views/crud/_actions_edit.html.erb +3 -3
- data/lib/generators/dry_crud/templates/app/views/crud/_actions_edit.html.haml +3 -3
- data/lib/generators/dry_crud/templates/app/views/crud/_actions_index.html.erb +1 -1
- data/lib/generators/dry_crud/templates/app/views/crud/_actions_index.html.haml +1 -1
- data/lib/generators/dry_crud/templates/app/views/crud/_actions_show.html.erb +3 -3
- data/lib/generators/dry_crud/templates/app/views/crud/_actions_show.html.haml +3 -3
- data/lib/generators/dry_crud/templates/app/views/crud/_attrs.html.erb +1 -1
- data/lib/generators/dry_crud/templates/app/views/crud/_attrs.html.haml +1 -1
- data/lib/generators/dry_crud/templates/app/views/crud/_form.html.erb +1 -1
- data/lib/generators/dry_crud/templates/app/views/crud/_form.html.haml +1 -1
- data/lib/generators/dry_crud/templates/app/views/crud/edit.html.erb +1 -1
- data/lib/generators/dry_crud/templates/app/views/crud/edit.html.haml +1 -1
- data/lib/generators/dry_crud/templates/app/views/crud/new.html.erb +2 -2
- data/lib/generators/dry_crud/templates/app/views/crud/new.html.haml +2 -2
- data/lib/generators/dry_crud/templates/app/views/crud/show.html.erb +1 -1
- data/lib/generators/dry_crud/templates/app/views/crud/show.html.haml +1 -1
- data/lib/generators/dry_crud/templates/app/views/layouts/_flash.html.haml +1 -1
- data/lib/generators/dry_crud/templates/app/views/layouts/_nav.html.erb +1 -1
- data/lib/generators/dry_crud/templates/app/views/layouts/_nav.html.haml +1 -1
- data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.erb +15 -7
- data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.haml +18 -8
- data/lib/generators/dry_crud/templates/app/views/list/_search.html.erb +3 -3
- data/lib/generators/dry_crud/templates/app/views/list/_search.html.haml +3 -3
- data/lib/generators/dry_crud/templates/app/views/list/index.html.erb +1 -1
- data/lib/generators/dry_crud/templates/app/views/list/index.html.haml +1 -1
- data/lib/generators/dry_crud/templates/app/views/shared/_error_messages.html.erb +1 -1
- data/lib/generators/dry_crud/templates/app/views/shared/_error_messages.html.haml +1 -1
- data/lib/generators/dry_crud/templates/app/views/shared/_labeled.html.erb +2 -4
- data/lib/generators/dry_crud/templates/app/views/shared/_labeled.html.haml +2 -3
- data/lib/generators/dry_crud/templates/config/initializers/field_error_proc.rb +5 -1
- data/lib/generators/dry_crud/templates/config/locales/crud.de.yml +64 -0
- data/lib/generators/dry_crud/templates/config/locales/{en_crud.yml → crud.en.yml} +2 -2
- data/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb +241 -231
- data/lib/generators/dry_crud/templates/spec/helpers/crud/form_builder_spec.rb +226 -0
- data/lib/generators/dry_crud/templates/spec/helpers/{standard_table_builder_spec.rb → crud/table_builder_spec.rb} +36 -34
- data/lib/generators/dry_crud/templates/spec/helpers/form_helper_spec.rb +238 -0
- data/lib/generators/dry_crud/templates/spec/helpers/format_helper_spec.rb +244 -0
- data/lib/generators/dry_crud/templates/spec/helpers/i18n_helper_spec.rb +132 -0
- data/lib/generators/dry_crud/templates/spec/helpers/table_helper_spec.rb +265 -0
- data/lib/generators/dry_crud/templates/spec/helpers/utility_helper_spec.rb +74 -0
- data/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb +185 -100
- data/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb +58 -49
- data/lib/generators/dry_crud/templates/test/{functional → controllers}/crud_test_models_controller_test.rb +112 -91
- data/lib/generators/dry_crud/templates/test/{unit/helpers/standard_form_builder_test.rb → helpers/crud/form_builder_test.rb} +79 -62
- data/lib/generators/dry_crud/templates/test/{unit/helpers/standard_table_builder_test.rb → helpers/crud/table_builder_test.rb} +31 -28
- data/lib/generators/dry_crud/templates/test/helpers/custom_assertions_test.rb +85 -0
- data/lib/generators/dry_crud/templates/test/helpers/form_helper_test.rb +129 -0
- data/lib/generators/dry_crud/templates/test/helpers/format_helper_test.rb +163 -0
- data/lib/generators/dry_crud/templates/test/helpers/i18n_helper_test.rb +79 -0
- data/lib/generators/dry_crud/templates/test/helpers/table_helper_test.rb +217 -0
- data/lib/generators/dry_crud/templates/test/helpers/utility_helper_test.rb +63 -0
- data/lib/generators/dry_crud/templates/test/{functional → support}/crud_controller_test_helper.rb +70 -59
- data/lib/generators/dry_crud/templates/test/{crud_test_model.rb → support/crud_test_model.rb} +107 -75
- data/lib/generators/dry_crud/templates/test/support/custom_assertions.rb +83 -0
- metadata +83 -146
- data/Rakefile +0 -211
- data/lib/generators/dry_crud/templates/app/helpers/crud_helper.rb +0 -168
- data/lib/generators/dry_crud/templates/app/helpers/list_helper.rb +0 -27
- data/lib/generators/dry_crud/templates/app/helpers/standard_form_builder.rb +0 -261
- data/lib/generators/dry_crud/templates/app/helpers/standard_helper.rb +0 -304
- data/lib/generators/dry_crud/templates/app/helpers/standard_table_builder.rb +0 -178
- data/lib/generators/dry_crud/templates/spec/helpers/crud_helper_spec.rb +0 -146
- data/lib/generators/dry_crud/templates/spec/helpers/list_helper_spec.rb +0 -154
- data/lib/generators/dry_crud/templates/spec/helpers/standard_form_builder_spec.rb +0 -215
- data/lib/generators/dry_crud/templates/spec/helpers/standard_helper_spec.rb +0 -387
- data/lib/generators/dry_crud/templates/test/custom_assertions.rb +0 -78
- data/lib/generators/dry_crud/templates/test/unit/custom_assertions_test.rb +0 -117
- data/lib/generators/dry_crud/templates/test/unit/helpers/crud_helper_test.rb +0 -111
- data/lib/generators/dry_crud/templates/test/unit/helpers/list_helper_test.rb +0 -123
- data/lib/generators/dry_crud/templates/test/unit/helpers/standard_helper_test.rb +0 -281
- data/test/templates/Gemfile +0 -46
- data/test/templates/app/controllers/admin/cities_controller.rb +0 -7
- data/test/templates/app/controllers/admin/countries_controller.rb +0 -13
- data/test/templates/app/controllers/ajax_controller.rb +0 -9
- data/test/templates/app/controllers/people_controller.rb +0 -13
- data/test/templates/app/controllers/vips_controller.rb +0 -19
- data/test/templates/app/helpers/cities_helper.rb +0 -9
- data/test/templates/app/helpers/people_helper.rb +0 -8
- data/test/templates/app/models/city.rb +0 -28
- data/test/templates/app/models/country.rb +0 -16
- data/test/templates/app/models/person.rb +0 -12
- data/test/templates/app/views/admin/cities/_actions_index.html.erb +0 -2
- data/test/templates/app/views/admin/cities/_actions_index.html.haml +0 -2
- data/test/templates/app/views/admin/cities/_attrs.html.erb +0 -1
- data/test/templates/app/views/admin/cities/_attrs.html.haml +0 -1
- data/test/templates/app/views/admin/cities/_form.html.erb +0 -7
- data/test/templates/app/views/admin/cities/_form.html.haml +0 -5
- data/test/templates/app/views/admin/cities/_hello.html.erb +0 -1
- data/test/templates/app/views/admin/cities/_hello.html.haml +0 -1
- data/test/templates/app/views/admin/cities/_list.html.erb +0 -3
- data/test/templates/app/views/admin/cities/_list.html.haml +0 -3
- data/test/templates/app/views/admin/countries/_list.html.erb +0 -4
- data/test/templates/app/views/admin/countries/_list.html.haml +0 -3
- data/test/templates/app/views/ajax/_actions_index.html.erb +0 -8
- data/test/templates/app/views/ajax/_actions_index.html.haml +0 -8
- data/test/templates/app/views/ajax/_actions_show.html.erb +0 -4
- data/test/templates/app/views/ajax/_actions_show.html.haml +0 -4
- data/test/templates/app/views/ajax/_form.html.erb +0 -2
- data/test/templates/app/views/ajax/_form.html.haml +0 -2
- data/test/templates/app/views/ajax/_hello.html.erb +0 -1
- data/test/templates/app/views/ajax/_hello.html.haml +0 -1
- data/test/templates/app/views/ajax/ajax.js.erb +0 -1
- data/test/templates/app/views/ajax/ajax.js.haml +0 -1
- data/test/templates/app/views/ajax/edit.js.erb +0 -1
- data/test/templates/app/views/ajax/edit.js.haml +0 -1
- data/test/templates/app/views/ajax/show.js.erb +0 -1
- data/test/templates/app/views/ajax/show.js.haml +0 -1
- data/test/templates/app/views/ajax/update.js.erb +0 -5
- data/test/templates/app/views/ajax/update.js.haml +0 -5
- data/test/templates/app/views/layouts/_nav.html.erb +0 -6
- data/test/templates/app/views/layouts/_nav.html.haml +0 -5
- data/test/templates/app/views/layouts/bootstrap.html.erb +0 -68
- data/test/templates/app/views/layouts/bootstrap.html.haml +0 -49
- data/test/templates/app/views/people/_attrs.html.erb +0 -5
- data/test/templates/app/views/people/_attrs.html.haml +0 -4
- data/test/templates/app/views/people/_list.html.erb +0 -1
- data/test/templates/app/views/people/_list.html.haml +0 -1
- data/test/templates/config/database.yml +0 -21
- data/test/templates/config/locales/en_cities.yml +0 -56
- data/test/templates/config/routes.rb +0 -32
- data/test/templates/db/migrate/20100511174904_create_people_and_cities.rb +0 -26
- data/test/templates/db/seeds.rb +0 -74
- data/test/templates/spec/controllers/admin/cities_controller_spec.rb +0 -74
- data/test/templates/spec/controllers/admin/countries_controller_spec.rb +0 -56
- data/test/templates/spec/controllers/people_controller_spec.rb +0 -80
- data/test/templates/spec/routing/cities_routing_spec.rb +0 -11
- data/test/templates/spec/routing/countries_routing_spec.rb +0 -11
- data/test/templates/test/fixtures/cities.yml +0 -11
- data/test/templates/test/fixtures/countries.yml +0 -11
- data/test/templates/test/fixtures/people.yml +0 -14
- data/test/templates/test/functional/admin/cities_controller_test.rb +0 -59
- data/test/templates/test/functional/admin/countries_controller_test.rb +0 -42
- data/test/templates/test/functional/people_controller_test.rb +0 -68
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
1
3
|
# Contains assertions for testing common crud controller use cases.
|
2
4
|
# See crud_controller_examples for use cases.
|
3
5
|
module CrudControllerTestHelper
|
@@ -7,30 +9,32 @@ module CrudControllerTestHelper
|
|
7
9
|
def perform_request
|
8
10
|
m = example.metadata
|
9
11
|
example_params = respond_to?(:params) ? send(:params) : {}
|
10
|
-
params = scope_params.merge(:
|
11
|
-
params.merge!(:
|
12
|
+
params = scope_params.merge(format: m[:format])
|
13
|
+
params.merge!(id: test_entry.id) if m[:id]
|
12
14
|
params.merge!(example_params)
|
13
15
|
send(m[:method], m[:action], params)
|
14
16
|
end
|
15
17
|
|
16
|
-
# If a combine key is given in metadata, only the first request for all
|
17
|
-
# with the same key will be performed.
|
18
|
+
# If a combine key is given in metadata, only the first request for all
|
19
|
+
# examples with the same key will be performed.
|
18
20
|
def perform_combined_request
|
19
|
-
|
21
|
+
stack = example.metadata[:combine]
|
22
|
+
if stack
|
20
23
|
@@current_stack ||= nil
|
21
|
-
if stack == @@current_stack &&
|
24
|
+
if stack == @@current_stack &&
|
25
|
+
described_class == @@current_controller.class
|
22
26
|
@response = @@current_response
|
23
|
-
@templates = @@current_templates
|
27
|
+
@_templates = @templates = @@current_templates
|
24
28
|
@controller = @@current_controller
|
25
29
|
@request = @@current_request
|
26
30
|
else
|
27
31
|
perform_request
|
28
|
-
|
32
|
+
|
29
33
|
@@current_stack = stack
|
30
34
|
@@current_response = @response
|
31
35
|
@@current_request = @request
|
32
36
|
@@current_controller = @controller
|
33
|
-
@@current_templates = @templates
|
37
|
+
@@current_templates = @_templates || @templates
|
34
38
|
end
|
35
39
|
else
|
36
40
|
perform_request
|
@@ -41,7 +45,7 @@ module CrudControllerTestHelper
|
|
41
45
|
def scope_params
|
42
46
|
params = {}
|
43
47
|
# for nested controllers, add parent ids to each request
|
44
|
-
Array(controller.nesting).reverse.
|
48
|
+
Array(controller.nesting).reverse.reduce(test_entry) do |parent, p|
|
45
49
|
if p.is_a?(Class) && p < ActiveRecord::Base
|
46
50
|
assoc = p.name.underscore
|
47
51
|
params["#{assoc}_id"] = parent.send(:"#{assoc}_id")
|
@@ -52,57 +56,61 @@ module CrudControllerTestHelper
|
|
52
56
|
end
|
53
57
|
params
|
54
58
|
end
|
55
|
-
|
59
|
+
|
60
|
+
# Helper methods to describe contexts.
|
56
61
|
module ClassMethods
|
57
|
-
|
62
|
+
|
58
63
|
# Describe a certain action and provide some usefull metadata.
|
59
64
|
# Tests whether this action is configured to be skipped.
|
60
65
|
def describe_action(method, action, metadata = {}, &block)
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
+
action_defined = described_class.instance_methods
|
67
|
+
.map(&:to_s).include?(action.to_s)
|
68
|
+
describe("#{method.to_s.upcase} #{action}",
|
69
|
+
{ if: action_defined,
|
70
|
+
method: method,
|
71
|
+
action: action }.merge(metadata),
|
66
72
|
&block)
|
67
73
|
end
|
68
|
-
|
74
|
+
|
75
|
+
# Is the current context part of the skip list.
|
69
76
|
def skip?(options, *contexts)
|
70
77
|
options ||= {}
|
71
78
|
contexts = Array(contexts).flatten
|
72
79
|
skips = Array(options[:skip])
|
73
80
|
skips = [skips] if skips.blank? || !skips.first.is_a?(Array)
|
74
|
-
|
75
|
-
skips.flatten.present? &&
|
81
|
+
|
82
|
+
skips.flatten.present? &&
|
76
83
|
skips.any? { |skip| skip == contexts.take(skip.size) }
|
77
84
|
end
|
78
|
-
|
85
|
+
|
79
86
|
# Test the response status, default 200.
|
80
87
|
def it_should_respond(status = 200)
|
81
88
|
its(:status) { should == status }
|
82
89
|
end
|
83
|
-
|
90
|
+
|
84
91
|
# Test that entries are assigned.
|
85
92
|
def it_should_assign_entries
|
86
|
-
it
|
93
|
+
it 'should assign entries' do
|
87
94
|
entries.should be_present
|
88
95
|
end
|
89
96
|
end
|
90
|
-
|
97
|
+
|
91
98
|
# Test that entry is assigned.
|
92
99
|
def it_should_assign_entry
|
93
|
-
it
|
100
|
+
it 'should assign entry' do
|
94
101
|
entry.should == test_entry
|
95
102
|
end
|
96
103
|
end
|
97
|
-
|
98
|
-
# Test that the given template or the main template of the action under
|
104
|
+
|
105
|
+
# Test that the given template or the main template of the action under
|
106
|
+
# test is rendered.
|
99
107
|
def it_should_render(template = nil)
|
100
108
|
it { should render_template(template || example.metadata[:action]) }
|
101
109
|
end
|
102
|
-
|
110
|
+
|
103
111
|
# Test that test_entry_attrs are set on entry.
|
104
112
|
def it_should_set_attrs(action = nil)
|
105
|
-
it
|
113
|
+
it 'should set params as entry attributes' do
|
106
114
|
attrs = send("#{action}_entry_attrs")
|
107
115
|
actual = {}
|
108
116
|
attrs.keys.each do |key|
|
@@ -111,50 +119,51 @@ module CrudControllerTestHelper
|
|
111
119
|
actual.should == attrs
|
112
120
|
end
|
113
121
|
end
|
114
|
-
|
122
|
+
|
115
123
|
# Test that the response redirects to the index action.
|
116
124
|
def it_should_redirect_to_index
|
117
|
-
it
|
125
|
+
it do
|
126
|
+
should redirect_to scope_params.merge(action: 'index',
|
127
|
+
returning: true)
|
128
|
+
end
|
118
129
|
end
|
119
|
-
|
130
|
+
|
120
131
|
# Test that the response redirects to the show action of the current entry.
|
121
132
|
def it_should_redirect_to_show
|
122
|
-
it
|
133
|
+
it do
|
134
|
+
should redirect_to scope_params.merge(action: 'show',
|
135
|
+
id: entry.id)
|
136
|
+
end
|
123
137
|
end
|
124
|
-
|
138
|
+
|
125
139
|
# Test that the given flash type is present.
|
126
140
|
def it_should_have_flash(type, message = nil)
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
its([type]) do
|
131
|
-
should(message ? match(message) : be_present)
|
132
|
-
end
|
141
|
+
it "flash(#{type}) is set" do
|
142
|
+
flash[type].should(message ? match(message) : be_present)
|
133
143
|
end
|
134
144
|
end
|
135
145
|
|
136
146
|
# Test that not flash of the given type is present.
|
137
147
|
def it_should_not_have_flash(type)
|
138
|
-
|
139
|
-
|
140
|
-
its([type]) { should be_blank }
|
148
|
+
it "flash(#{type}) is nil" do
|
149
|
+
flash[type].should be_blank
|
141
150
|
end
|
142
151
|
end
|
143
|
-
|
152
|
+
|
144
153
|
# Test that the current entry is persistend and valid, or not.
|
145
154
|
def it_should_persist_entry(bool = true)
|
146
|
-
context
|
155
|
+
context 'entry' do
|
147
156
|
subject { entry }
|
148
|
-
|
157
|
+
|
149
158
|
if bool
|
150
|
-
it {
|
159
|
+
it { should_not be_new_record }
|
151
160
|
it { should be_valid }
|
152
161
|
else
|
153
|
-
it {
|
162
|
+
it { should be_new_record }
|
154
163
|
end
|
155
164
|
end
|
156
165
|
end
|
157
166
|
end
|
158
|
-
|
167
|
+
|
159
168
|
end
|
160
169
|
|
@@ -1,6 +1,7 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
require 'test_helper'
|
2
|
-
require 'crud_test_model'
|
3
|
-
require
|
3
|
+
require 'support/crud_test_model'
|
4
|
+
require 'support/crud_controller_test_helper'
|
4
5
|
|
5
6
|
# Tests all actions of the CrudController based on a dummy model
|
6
7
|
# (CrudTestModel). This is useful to test the general behavior
|
@@ -16,79 +17,81 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
16
17
|
|
17
18
|
teardown :reset_db
|
18
19
|
|
19
|
-
|
20
20
|
def test_setup
|
21
21
|
assert_equal 6, CrudTestModel.count
|
22
22
|
assert_equal CrudTestModelsController, @controller.class
|
23
|
-
assert_recognizes({:
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
assert_recognizes({ controller: 'crud_test_models',
|
24
|
+
action: 'index' },
|
25
|
+
'/crud_test_models')
|
26
|
+
assert_recognizes({ controller: 'crud_test_models',
|
27
|
+
action: 'show',
|
28
|
+
id: '1' },
|
29
|
+
'/crud_test_models/1')
|
27
30
|
end
|
28
31
|
|
29
32
|
def test_index
|
30
33
|
super
|
31
34
|
assert_equal 6, entries.size
|
32
35
|
assert_equal entries.sort_by(&:name), entries
|
33
|
-
assert_equal
|
36
|
+
assert_equal({}, session[:list_params])
|
34
37
|
assert_equal entries, assigns(:crud_test_models)
|
35
38
|
assert_respond_to assigns(:crud_test_models), :klass
|
36
39
|
end
|
37
40
|
|
38
41
|
def test_index_js
|
39
|
-
get :index, test_params(:
|
42
|
+
get :index, test_params(format: 'js')
|
40
43
|
assert_response :success
|
41
44
|
assert_equal 'index js', @response.body
|
42
|
-
|
45
|
+
assert entries.present?
|
43
46
|
end
|
44
47
|
|
45
48
|
def test_index_search
|
46
49
|
super
|
47
50
|
assert_equal 1, entries.size
|
48
|
-
assert_equal({:
|
51
|
+
assert_equal({ q: 'AAAA' }, session[:list_params]['/crud_test_models'])
|
49
52
|
end
|
50
53
|
|
51
54
|
def test_index_with_custom_options
|
52
|
-
get :index, :
|
55
|
+
get :index, filter: true
|
53
56
|
assert_response :success
|
54
57
|
assert_template 'index'
|
55
|
-
|
58
|
+
assert entries.present?
|
56
59
|
assert_equal 2, entries.size
|
57
60
|
assert_equal entries.sort_by(&:children).reverse, entries
|
58
61
|
end
|
59
62
|
|
60
63
|
def test_index_search_with_custom_options
|
61
|
-
get :index, :
|
64
|
+
get :index, q: 'DDD', filter: true
|
62
65
|
assert_response :success
|
63
66
|
assert_template 'index'
|
64
|
-
|
67
|
+
assert entries.present?
|
65
68
|
assert_equal 1, entries.size
|
66
69
|
assert_equal [CrudTestModel.find_by_name('BBBBB')], entries
|
67
|
-
assert_equal({:
|
70
|
+
assert_equal({ q: 'DDD' }, session[:list_params]['/crud_test_models'])
|
68
71
|
end
|
69
72
|
|
70
73
|
def test_sort_given_column
|
71
|
-
get :index, :
|
74
|
+
get :index, sort: 'children', sort_dir: 'asc'
|
72
75
|
assert_response :success
|
73
76
|
assert_template 'index'
|
74
|
-
|
77
|
+
assert entries.present?
|
75
78
|
assert_equal 6, entries.size
|
76
79
|
assert_equal CrudTestModel.all.sort_by(&:children), entries
|
77
|
-
assert_equal({:
|
80
|
+
assert_equal({ sort: 'children', sort_dir: 'asc' },
|
81
|
+
session[:list_params]['/crud_test_models'])
|
78
82
|
end
|
79
83
|
|
80
84
|
def test_sort_virtual_column
|
81
|
-
get :index, :
|
85
|
+
get :index, sort: 'chatty', sort_dir: 'desc'
|
82
86
|
assert_response :success
|
83
87
|
assert_template 'index'
|
84
|
-
|
88
|
+
assert entries.present?
|
85
89
|
assert_equal 6, entries.size
|
86
|
-
assert_equal({:
|
87
|
-
|
88
|
-
sorted = CrudTestModel.all.sort_by(&:chatty)
|
90
|
+
assert_equal({ sort: 'chatty', sort_dir: 'desc' },
|
91
|
+
session[:list_params]['/crud_test_models'])
|
89
92
|
|
90
93
|
# sort order is ambiguous, use index
|
91
|
-
names = entries.
|
94
|
+
names = entries.map(&:name)
|
92
95
|
assert names.index('BBBBB') < names.index('AAAAA')
|
93
96
|
assert names.index('BBBBB') < names.index('DDDDD')
|
94
97
|
assert names.index('EEEEE') < names.index('AAAAA')
|
@@ -98,24 +101,27 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
98
101
|
end
|
99
102
|
|
100
103
|
def test_sort_with_search
|
101
|
-
get :index, :
|
104
|
+
get :index, q: 'DDD', sort: 'chatty', sort_dir: 'asc'
|
102
105
|
assert_response :success
|
103
106
|
assert_template 'index'
|
104
|
-
|
107
|
+
assert entries.present?
|
105
108
|
assert_equal 3, entries.size
|
106
|
-
assert_equal
|
107
|
-
assert_equal({:
|
109
|
+
assert_equal %w(CCCCC DDDDD BBBBB), entries.map(&:name)
|
110
|
+
assert_equal({ sort: 'chatty', sort_dir: 'asc', q: 'DDD' },
|
111
|
+
session[:list_params]['/crud_test_models'])
|
108
112
|
end
|
109
113
|
|
110
114
|
def test_index_returning
|
111
115
|
session[:list_params] = {}
|
112
|
-
session[:list_params]['/crud_test_models'] = {:
|
113
|
-
|
116
|
+
session[:list_params]['/crud_test_models'] = { q: 'DDD',
|
117
|
+
sort: 'chatty',
|
118
|
+
sort_dir: 'desc' }
|
119
|
+
get :index, returning: true
|
114
120
|
assert_response :success
|
115
121
|
assert_template 'index'
|
116
|
-
|
122
|
+
assert entries.present?
|
117
123
|
assert_equal 3, entries.size
|
118
|
-
assert_equal
|
124
|
+
assert_equal %w(BBBBB DDDDD CCCCC), entries.map(&:name)
|
119
125
|
assert_equal 'DDD', @controller.params[:q]
|
120
126
|
assert_equal 'chatty', @controller.params[:sort]
|
121
127
|
assert_equal 'desc', @controller.params[:sort_dir]
|
@@ -125,7 +131,8 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
125
131
|
super
|
126
132
|
assert assigns(:companions)
|
127
133
|
assert_equal @controller.send(:entry), assigns(:crud_test_model)
|
128
|
-
assert_equal [:before_render_new, :before_render_form],
|
134
|
+
assert_equal [:before_render_new, :before_render_form],
|
135
|
+
@controller.called_callbacks
|
129
136
|
end
|
130
137
|
|
131
138
|
def test_show
|
@@ -134,7 +141,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
134
141
|
end
|
135
142
|
|
136
143
|
def test_show_with_custom
|
137
|
-
get :show, test_params(:
|
144
|
+
get :show, test_params(id: crud_test_models(:BBBBB).id)
|
138
145
|
assert_response :success
|
139
146
|
assert_equal 'custom html', @response.body
|
140
147
|
end
|
@@ -142,78 +149,87 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
142
149
|
def test_create
|
143
150
|
super
|
144
151
|
assert_match /model got created/, flash[:notice]
|
145
|
-
|
146
|
-
assert_equal [:before_create, :before_save, :after_save, :after_create],
|
152
|
+
assert flash[:alert].blank?
|
153
|
+
assert_equal [:before_create, :before_save, :after_save, :after_create],
|
154
|
+
@controller.called_callbacks
|
147
155
|
end
|
148
156
|
|
149
157
|
def test_edit
|
150
158
|
super
|
151
159
|
assert_equal @controller.send(:entry), assigns(:crud_test_model)
|
152
|
-
assert_equal [:before_render_edit, :before_render_form],
|
160
|
+
assert_equal [:before_render_edit, :before_render_form],
|
161
|
+
@controller.called_callbacks
|
153
162
|
end
|
154
163
|
|
155
164
|
def test_update
|
156
165
|
super
|
157
|
-
|
158
|
-
|
159
|
-
|
166
|
+
assert_equal I18n.t('crud.update.flash.success',
|
167
|
+
model: 'Crud Test Model <i>foo</i>'),
|
168
|
+
flash[:notice]
|
169
|
+
assert flash[:alert].blank?
|
160
170
|
assert_equal @controller.send(:entry), assigns(:crud_test_model)
|
161
|
-
assert_equal [:before_update, :before_save, :after_save, :after_update],
|
171
|
+
assert_equal [:before_update, :before_save, :after_save, :after_update],
|
172
|
+
@controller.called_callbacks
|
162
173
|
end
|
163
174
|
|
164
175
|
def test_destroy
|
165
176
|
super
|
166
|
-
assert_equal [:before_destroy, :after_destroy],
|
167
|
-
|
168
|
-
|
177
|
+
assert_equal [:before_destroy, :after_destroy],
|
178
|
+
@controller.called_callbacks
|
179
|
+
assert_equal I18n.t('crud.destroy.flash.success',
|
180
|
+
model: 'Crud Test Model <i>AAAAA</i>'),
|
181
|
+
flash[:notice]
|
169
182
|
end
|
170
183
|
|
171
184
|
def test_create_with_before_callback
|
172
|
-
assert_no_difference(
|
173
|
-
post :create, :
|
185
|
+
assert_no_difference('CrudTestModel.count') do
|
186
|
+
post :create, crud_test_model: { name: 'illegal', children: 2 }
|
174
187
|
end
|
175
188
|
assert_response :success
|
176
189
|
assert_template 'new'
|
177
190
|
assert entry.new_record?
|
178
|
-
assert assigns(:companions)
|
179
|
-
|
191
|
+
assert assigns(:companions).present?
|
192
|
+
assert flash[:alert].present?
|
180
193
|
assert_equal 'illegal', entry.name
|
181
|
-
assert_equal [:before_render_new, :before_render_form],
|
194
|
+
assert_equal [:before_render_new, :before_render_form],
|
195
|
+
@controller.called_callbacks
|
182
196
|
end
|
183
197
|
|
184
198
|
def test_create_with_before_callback_redirect
|
185
199
|
@controller.should_redirect = true
|
186
|
-
assert_no_difference(
|
187
|
-
post :create, :
|
200
|
+
assert_no_difference('CrudTestModel.count') do
|
201
|
+
post :create, crud_test_model: { name: 'illegal', children: 2 }
|
188
202
|
end
|
189
|
-
assert_redirected_to :
|
203
|
+
assert_redirected_to action: 'index'
|
190
204
|
assert_nil @controller.called_callbacks
|
191
205
|
end
|
192
206
|
|
193
207
|
def test_new_with_before_render_callback_redirect_does_not_set_companions
|
194
208
|
@controller.should_redirect = true
|
195
209
|
get :new
|
196
|
-
assert_redirected_to :
|
210
|
+
assert_redirected_to action: 'index'
|
197
211
|
assert_nil assigns(:companions)
|
198
212
|
end
|
199
213
|
|
200
214
|
def test_create_with_failure
|
201
|
-
assert_no_difference(
|
202
|
-
post :create, :
|
215
|
+
assert_no_difference('CrudTestModel.count') do
|
216
|
+
post :create, crud_test_model: { children: 2 }
|
203
217
|
end
|
204
218
|
assert_response :success
|
205
219
|
assert_template 'new'
|
206
220
|
assert entry.new_record?
|
207
221
|
assert assigns(:companions)
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
assert_equal [:before_create, :before_save,
|
222
|
+
assert flash[:notice].blank?, flash[:notice].to_s
|
223
|
+
assert flash[:alert].blank?, flash[:alert].to_s
|
224
|
+
assert entry.name.blank?
|
225
|
+
assert_equal [:before_create, :before_save,
|
226
|
+
:before_render_new, :before_render_form],
|
227
|
+
@controller.called_callbacks
|
212
228
|
end
|
213
229
|
|
214
230
|
def test_create_with_failure_json
|
215
|
-
assert_no_difference(
|
216
|
-
post :create, :
|
231
|
+
assert_no_difference('CrudTestModel.count') do
|
232
|
+
post :create, crud_test_model: { children: 2 }, format: 'json'
|
217
233
|
end
|
218
234
|
assert_response :unprocessable_entity
|
219
235
|
assert entry.new_record?
|
@@ -222,57 +238,62 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
222
238
|
end
|
223
239
|
|
224
240
|
def test_update_with_failure
|
225
|
-
put :update, :
|
241
|
+
put :update, id: test_entry.id, crud_test_model: { rating: 20 }
|
226
242
|
assert_response :success
|
227
243
|
assert_template 'edit'
|
228
244
|
assert entry.changed?
|
229
|
-
|
230
|
-
|
245
|
+
assert flash[:notice].blank?
|
246
|
+
assert flash[:alert].blank?
|
231
247
|
assert_equal 20, entry.rating
|
232
|
-
assert_equal [:before_update, :before_save,
|
248
|
+
assert_equal [:before_update, :before_save,
|
249
|
+
:before_render_edit, :before_render_form],
|
250
|
+
@controller.called_callbacks
|
233
251
|
end
|
234
252
|
|
235
253
|
def test_update_with_failure_json
|
236
|
-
put :update, :
|
254
|
+
put :update, id: test_entry.id,
|
255
|
+
crud_test_model: { rating: 20 },
|
256
|
+
format: 'json'
|
237
257
|
assert_response :unprocessable_entity
|
238
258
|
assert entry.changed?
|
239
259
|
assert_match /errors/, @response.body
|
240
|
-
|
260
|
+
assert flash[:notice].blank?
|
241
261
|
assert_equal 20, entry.rating
|
242
262
|
assert_equal [:before_update, :before_save], @controller.called_callbacks
|
243
263
|
end
|
244
264
|
|
245
265
|
def test_destroy_failure
|
246
266
|
assert_no_difference("#{model_class.name}.count") do
|
247
|
-
@request.env['HTTP_REFERER'] =
|
248
|
-
|
267
|
+
@request.env['HTTP_REFERER'] =
|
268
|
+
crud_test_model_url(crud_test_models(:BBBBB))
|
269
|
+
delete :destroy, test_params(id: crud_test_models(:BBBBB).id)
|
249
270
|
end
|
250
271
|
assert_redirected_to_show(entry)
|
251
272
|
assert_match /companion/, flash[:alert]
|
252
|
-
|
273
|
+
assert flash[:notice].blank?
|
253
274
|
end
|
254
275
|
|
255
276
|
def test_destroy_failure_callback
|
256
277
|
e = crud_test_models(:AAAAA)
|
257
278
|
e.update_attribute :name, 'illegal'
|
258
279
|
assert_no_difference("#{model_class.name}.count") do
|
259
|
-
delete :destroy, test_params(:
|
280
|
+
delete :destroy, test_params(id: e.id)
|
260
281
|
end
|
261
282
|
assert_redirected_to_index
|
262
283
|
assert_match /illegal name/, flash[:alert]
|
263
|
-
|
284
|
+
assert flash[:notice].blank?
|
264
285
|
end
|
265
286
|
|
266
287
|
def test_destroy_failure_json
|
267
288
|
assert_no_difference("#{model_class.name}.count") do
|
268
|
-
delete :destroy, test_params(:
|
289
|
+
delete :destroy, test_params(id: crud_test_models(:BBBBB).id,
|
290
|
+
format: 'json')
|
269
291
|
end
|
270
292
|
assert_response :unprocessable_entity
|
271
293
|
assert_match /errors/, @response.body
|
272
|
-
|
294
|
+
assert flash[:notice].blank?
|
273
295
|
end
|
274
296
|
|
275
|
-
|
276
297
|
def test_models_label
|
277
298
|
assert_equal 'Crud Test Models', @controller.models_label
|
278
299
|
assert_equal 'Crud Test Model', @controller.models_label(false)
|
@@ -285,23 +306,23 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
285
306
|
end
|
286
307
|
|
287
308
|
def new_entry_attrs
|
288
|
-
{:
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
309
|
+
{ name: 'foo',
|
310
|
+
children: 42,
|
311
|
+
companion_id: 3,
|
312
|
+
rating: 8.5,
|
313
|
+
income: 2.42,
|
314
|
+
birthdate: '31-12-1999'.to_date,
|
315
|
+
human: true,
|
316
|
+
remarks: "some custom\n\tremarks" }
|
296
317
|
end
|
297
318
|
|
298
319
|
def edit_entry_attrs
|
299
|
-
{:
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
320
|
+
{ name: 'foo',
|
321
|
+
children: 42,
|
322
|
+
rating: 8.5,
|
323
|
+
income: 2.42,
|
324
|
+
birthdate: '31-12-1999'.to_date,
|
325
|
+
human: true,
|
326
|
+
remarks: "some custom\n\tremarks" }
|
306
327
|
end
|
307
328
|
end
|