dry_crud 1.6.0 → 1.7.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.
- data/README.rdoc +25 -10
- data/Rakefile +30 -8
- data/VERSION +1 -1
- data/lib/generators/dry_crud/dry_crud_generator.rb +15 -2
- data/lib/generators/dry_crud/templates/app/controllers/crud_controller.rb +18 -10
- data/lib/generators/dry_crud/templates/app/controllers/list_controller.rb +14 -11
- data/lib/generators/dry_crud/templates/app/helpers/crud_helper.rb +71 -29
- data/lib/generators/dry_crud/templates/app/helpers/standard_form_builder.rb +40 -3
- data/lib/generators/dry_crud/templates/app/helpers/standard_helper.rb +8 -27
- data/lib/generators/dry_crud/templates/app/helpers/standard_table_builder.rb +13 -6
- 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/layouts/crud.html.erb +4 -4
- data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.haml +5 -5
- data/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb +433 -0
- data/lib/generators/dry_crud/templates/spec/helpers/crud_helper_spec.rb +146 -0
- data/lib/generators/dry_crud/templates/spec/helpers/list_helper_spec.rb +154 -0
- data/lib/generators/dry_crud/templates/spec/helpers/standard_form_builder_spec.rb +215 -0
- data/lib/generators/dry_crud/templates/spec/helpers/standard_helper_spec.rb +387 -0
- data/lib/generators/dry_crud/templates/spec/helpers/standard_table_builder_spec.rb +120 -0
- data/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb +244 -0
- data/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb +160 -0
- data/lib/generators/dry_crud/templates/test/crud_test_model.rb +45 -18
- data/lib/generators/dry_crud/templates/test/functional/crud_controller_test_helper.rb +19 -9
- data/lib/generators/dry_crud/templates/test/functional/crud_test_models_controller_test.rb +11 -14
- data/lib/generators/dry_crud/templates/test/unit/helpers/crud_helper_test.rb +26 -13
- data/lib/generators/dry_crud/templates/test/unit/helpers/list_helper_test.rb +0 -4
- data/lib/generators/dry_crud/templates/test/unit/helpers/standard_form_builder_test.rb +6 -0
- data/lib/generators/dry_crud/templates/test/unit/helpers/standard_helper_test.rb +9 -35
- data/lib/generators/dry_crud/templates/test/unit/helpers/standard_table_builder_test.rb +5 -4
- data/test/templates/Gemfile +3 -1
- data/test/templates/app/controllers/people_controller.rb +1 -1
- data/test/templates/app/controllers/vips_controller.rb +1 -1
- data/test/templates/app/models/city.rb +1 -1
- data/test/templates/app/views/admin/cities/_form.html.erb +1 -1
- data/test/templates/app/views/admin/cities/_form.html.haml +1 -1
- data/test/templates/app/views/admin/countries/_list.html.erb +3 -4
- data/test/templates/app/views/admin/countries/_list.html.haml +3 -4
- data/test/templates/app/views/ajax/_form.html.erb +1 -1
- data/test/templates/app/views/ajax/_form.html.haml +1 -1
- data/test/templates/spec/controllers/admin/cities_controller_spec.rb +74 -0
- data/test/templates/spec/controllers/admin/countries_controller_spec.rb +56 -0
- data/test/templates/spec/controllers/people_controller_spec.rb +80 -0
- data/test/templates/spec/routing/cities_routing_spec.rb +11 -0
- data/test/templates/spec/routing/countries_routing_spec.rb +11 -0
- data/test/templates/test/functional/admin/cities_controller_test.rb +1 -1
- data/test/templates/test/functional/admin/countries_controller_test.rb +1 -1
- data/test/templates/test/functional/people_controller_test.rb +3 -3
- metadata +18 -7
@@ -0,0 +1,154 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ListHelper do
|
4
|
+
|
5
|
+
include StandardHelper
|
6
|
+
include CrudTestHelper
|
7
|
+
|
8
|
+
|
9
|
+
before(:all) do
|
10
|
+
reset_db
|
11
|
+
setup_db
|
12
|
+
create_test_data
|
13
|
+
end
|
14
|
+
|
15
|
+
after(:all) { reset_db }
|
16
|
+
|
17
|
+
describe "#list_table" do
|
18
|
+
let(:entries) { CrudTestModel.all }
|
19
|
+
|
20
|
+
context "default" do
|
21
|
+
subject do
|
22
|
+
with_test_routing { list_table }
|
23
|
+
end
|
24
|
+
|
25
|
+
it "has 7 rows" do
|
26
|
+
subject.scan(REGEXP_ROWS).size.should == 7
|
27
|
+
end
|
28
|
+
|
29
|
+
it "has 13 sortable headers" do
|
30
|
+
subject.scan(REGEXP_SORT_HEADERS).size.should == 13
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "with custom attributes" do
|
35
|
+
subject do
|
36
|
+
with_test_routing { list_table(:name, :children, :companion_id) }
|
37
|
+
end
|
38
|
+
|
39
|
+
it "has 7 rows" do
|
40
|
+
subject.scan(REGEXP_ROWS).size.should == 7
|
41
|
+
end
|
42
|
+
|
43
|
+
it "has 3 sortable headers" do
|
44
|
+
subject.scan(REGEXP_SORT_HEADERS).size.should == 3
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "with custom block" do
|
49
|
+
subject do
|
50
|
+
with_test_routing do
|
51
|
+
list_table do |t|
|
52
|
+
t.attrs :name, :children, :companion_id
|
53
|
+
t.col('head') {|e| content_tag(:span, e.income.to_s) }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "has 7 rows" do
|
59
|
+
subject.scan(REGEXP_ROWS).size.should == 7
|
60
|
+
end
|
61
|
+
|
62
|
+
it "has 4 headers" do
|
63
|
+
subject.scan(REGEXP_HEADERS).size.should == 4
|
64
|
+
end
|
65
|
+
|
66
|
+
it "has 0 sortable headers" do
|
67
|
+
subject.scan(REGEXP_SORT_HEADERS).size.should == 0
|
68
|
+
end
|
69
|
+
|
70
|
+
it "has 6 spans" do
|
71
|
+
subject.scan(/<span>.+?<\/span>/).size.should == 6
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "with custom attributes and block" do
|
76
|
+
subject do
|
77
|
+
with_test_routing do
|
78
|
+
list_table(:name, :children, :companion_id) do |t|
|
79
|
+
t.col('head') {|e| content_tag(:span, e.income.to_s) }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
it "has 7 rows" do
|
85
|
+
subject.scan(REGEXP_ROWS).size.should == 7
|
86
|
+
end
|
87
|
+
|
88
|
+
it "has 4 headers" do
|
89
|
+
subject.scan(REGEXP_HEADERS).size.should == 4
|
90
|
+
end
|
91
|
+
|
92
|
+
it "has 3 sortable headers" do
|
93
|
+
subject.scan(REGEXP_SORT_HEADERS).size.should == 3
|
94
|
+
end
|
95
|
+
|
96
|
+
it "has 6 spans" do
|
97
|
+
subject.scan(/<span>.+?<\/span>/).size.should == 6
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context "with ascending sort params" do
|
102
|
+
let(:params) { {:sort => 'children', :sort_dir => 'asc'} }
|
103
|
+
subject do
|
104
|
+
with_test_routing { list_table }
|
105
|
+
end
|
106
|
+
|
107
|
+
it "has 12 sortable headers" do
|
108
|
+
subject.scan(REGEXP_SORT_HEADERS).size.should == 12
|
109
|
+
end
|
110
|
+
|
111
|
+
it "has 1 ascending sort headers" do
|
112
|
+
subject.scan(/<th><a .*?sort_dir=desc.*?>Children<\/a> ↓<\/th>/).size.should == 1
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
context "with descending sort params" do
|
117
|
+
let(:params) { {:sort => 'children', :sort_dir => 'desc'} }
|
118
|
+
subject do
|
119
|
+
with_test_routing { list_table }
|
120
|
+
end
|
121
|
+
|
122
|
+
it "has 12 sortable headers" do
|
123
|
+
subject.scan(REGEXP_SORT_HEADERS).size.should == 12
|
124
|
+
end
|
125
|
+
|
126
|
+
it "has 1 descending sort headers" do
|
127
|
+
subject.scan(/<th><a .*?sort_dir=asc.*?>Children<\/a> ↑<\/th>/).size.should == 1
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "with custom column sort params" do
|
132
|
+
let(:params) { {:sort => 'chatty', :sort_dir => 'asc'} }
|
133
|
+
subject do
|
134
|
+
with_test_routing { list_table(:name, :children, :chatty) }
|
135
|
+
end
|
136
|
+
|
137
|
+
it "has 2 sortable headers" do
|
138
|
+
subject.scan(REGEXP_SORT_HEADERS).size.should == 2
|
139
|
+
end
|
140
|
+
|
141
|
+
it "has 1 ascending sort headers" do
|
142
|
+
subject.scan(/<th><a .*?sort_dir=desc.*?>Chatty<\/a> ↓<\/th>/).size.should == 1
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "#default_attrs" do
|
148
|
+
it "do not contain id" do
|
149
|
+
default_attrs.should == [:name, :whatever, :children, :companion_id, :rating, :income,
|
150
|
+
:birthdate, :gets_up_at, :last_seen, :human, :remarks,
|
151
|
+
:created_at, :updated_at]
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,215 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'StandardFormBuilder' do
|
4
|
+
|
5
|
+
include StandardHelper
|
6
|
+
include ListHelper
|
7
|
+
include CrudTestHelper
|
8
|
+
|
9
|
+
before(:all) do
|
10
|
+
reset_db
|
11
|
+
setup_db
|
12
|
+
create_test_data
|
13
|
+
end
|
14
|
+
|
15
|
+
after(:all) { reset_db }
|
16
|
+
|
17
|
+
let(:entry) { CrudTestModel.first }
|
18
|
+
let(:form) { StandardFormBuilder.new(:entry, entry, self, {}, lambda {|form| form }) }
|
19
|
+
|
20
|
+
describe "#input_field" do
|
21
|
+
|
22
|
+
{:name => :string_field,
|
23
|
+
:password => :password_field,
|
24
|
+
:remarks => :text_area,
|
25
|
+
:children => :integer_field,
|
26
|
+
:human => :boolean_field,
|
27
|
+
:birthdate => :date_field,
|
28
|
+
:gets_up_at => :time_field,
|
29
|
+
:companion_id => :belongs_to_field,
|
30
|
+
:other_ids => :has_many_field,
|
31
|
+
:more_ids => :has_many_field,
|
32
|
+
}.each do |attr, method|
|
33
|
+
it "dispatches #{attr} attr to #{method}" do
|
34
|
+
form.should_receive(method).with(attr, {})
|
35
|
+
form.input_field(attr)
|
36
|
+
end
|
37
|
+
|
38
|
+
it { form.input_field(attr).should be_html_safe }
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#labeled_input_fields" do
|
44
|
+
subject { form.labeled_input_fields(:name, :remarks, :children) }
|
45
|
+
|
46
|
+
it { should be_html_safe }
|
47
|
+
it { should include(form.input_field(:name)) }
|
48
|
+
it { should include(form.input_field(:remarks)) }
|
49
|
+
it { should include(form.input_field(:children)) }
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "#labeled_input_field" do
|
53
|
+
context "when required" do
|
54
|
+
subject { form.labeled_input_field(:name) }
|
55
|
+
it { should include(StandardFormBuilder::REQUIRED_MARK) }
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when not required" do
|
59
|
+
subject { form.labeled_input_field(:remarks) }
|
60
|
+
it { should_not include(StandardFormBuilder::REQUIRED_MARK) }
|
61
|
+
end
|
62
|
+
|
63
|
+
context "with help text" do
|
64
|
+
subject { form.labeled_input_field(:name, :help => 'Some Help') }
|
65
|
+
it { should include(form.help_block('Some Help')) }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#belongs_to_field" do
|
70
|
+
it "has all options by default" do
|
71
|
+
f = form.belongs_to_field(:companion_id)
|
72
|
+
f.scan('</option>').should have(7).items
|
73
|
+
end
|
74
|
+
|
75
|
+
it "with has options from :list option" do
|
76
|
+
list = CrudTestModel.all
|
77
|
+
f = form.belongs_to_field(:companion_id, :list => [list.first, list.second])
|
78
|
+
f.scan('</option>').should have(3).items
|
79
|
+
end
|
80
|
+
|
81
|
+
it "with empty instance list has no select" do
|
82
|
+
assign(:companions, [])
|
83
|
+
@companions = []
|
84
|
+
f = form.belongs_to_field(:companion_id)
|
85
|
+
f.should match(/none available/m)
|
86
|
+
f.scan('</option>').should have(0).items
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "#has_and_belongs_to_many_field" do
|
91
|
+
let(:others) { OtherCrudTestModel.all[0..1] }
|
92
|
+
|
93
|
+
it "has all options by default" do
|
94
|
+
f = form.has_many_field(:other_ids)
|
95
|
+
f.scan('</option>').should have(6).items
|
96
|
+
end
|
97
|
+
|
98
|
+
it "uses options from :list option if given" do
|
99
|
+
f = form.has_many_field(:other_ids, :list => others)
|
100
|
+
f.scan('</option>').should have(2).items
|
101
|
+
end
|
102
|
+
|
103
|
+
it "uses options form instance variable if given" do
|
104
|
+
assign(:others, others)
|
105
|
+
@others = others
|
106
|
+
f = form.has_many_field(:other_ids)
|
107
|
+
f.scan('</option>').should have(2).items
|
108
|
+
end
|
109
|
+
|
110
|
+
it "displays a message for an empty list" do
|
111
|
+
@others = []
|
112
|
+
f = form.has_many_field(:other_ids)
|
113
|
+
f.should match /none available/m
|
114
|
+
f.scan('</option>').should have(0).items
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "#string_field" do
|
119
|
+
it "sets maxlength if attr has a limit" do
|
120
|
+
form.string_field(:name).should match /maxlength="50"/
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "#label" do
|
125
|
+
context "only with attr" do
|
126
|
+
subject { form.label(:gugus_dada) }
|
127
|
+
|
128
|
+
it { should be_html_safe }
|
129
|
+
it "provides the same interface as rails" do
|
130
|
+
should match /label [^>]*for.+Gugus dada/
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context "with attr and text" do
|
135
|
+
subject { form.label(:gugus_dada, "hoho") }
|
136
|
+
|
137
|
+
it { should be_html_safe }
|
138
|
+
it "provides the same interface as rails" do
|
139
|
+
should match /label [^>]*for.+hoho/
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "#labeled" do
|
146
|
+
context "in labeled_ method" do
|
147
|
+
subject { form.labeled_string_field(:name) }
|
148
|
+
|
149
|
+
it { should be_html_safe }
|
150
|
+
it "provides the same interface as rails" do
|
151
|
+
should match /label [^>]*for.+input/m
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context "with custom content in argument" do
|
156
|
+
subject { form.labeled("gugus", "<input type='text' name='gugus' />".html_safe) }
|
157
|
+
|
158
|
+
it { should be_html_safe }
|
159
|
+
it { should match /label [^>]*for.+<input/m }
|
160
|
+
end
|
161
|
+
|
162
|
+
context "with custom content in block" do
|
163
|
+
subject { form.labeled("gugus") { "<input type='text' name='gugus' />".html_safe } }
|
164
|
+
|
165
|
+
it { should be_html_safe }
|
166
|
+
it { should match /label [^>]*for.+<input/m }
|
167
|
+
end
|
168
|
+
|
169
|
+
context "with caption and content in argument" do
|
170
|
+
subject { form.labeled("gugus", 'Caption', "<input type='text' name='gugus' />".html_safe) }
|
171
|
+
|
172
|
+
it { should be_html_safe }
|
173
|
+
it { should match /label [^>]*for.+>Caption<\/label>.*<input/m }
|
174
|
+
end
|
175
|
+
|
176
|
+
context "with caption and content in block" do
|
177
|
+
subject { form.labeled("gugus", 'Caption') { "<input type='text' name='gugus' />".html_safe } }
|
178
|
+
|
179
|
+
it { should be_html_safe }
|
180
|
+
it { should match /label [^>]*for.+>Caption<\/label>.*<input/m }
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
describe "#required_mark" do
|
185
|
+
it "is shown for required attrs" do
|
186
|
+
form.required_mark(:name).should == StandardFormBuilder::REQUIRED_MARK
|
187
|
+
end
|
188
|
+
|
189
|
+
it "is not shown for optional attrs" do
|
190
|
+
form.required_mark(:rating).should be_empty
|
191
|
+
end
|
192
|
+
|
193
|
+
it "is not shown for non existing attrs" do
|
194
|
+
form.required_mark(:not_existing).should be_empty
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
it "handles missing methods" do
|
199
|
+
expect { form.blabla }.to raise_error(NoMethodError)
|
200
|
+
end
|
201
|
+
|
202
|
+
context "#respond_to?" do
|
203
|
+
it "returns false for non existing methods" do
|
204
|
+
form.respond_to?(:blabla).should be_false
|
205
|
+
end
|
206
|
+
|
207
|
+
it "returns true for existing methods" do
|
208
|
+
form.respond_to?(:text_field).should be_true
|
209
|
+
end
|
210
|
+
|
211
|
+
it "returns true for labeled_ methods" do
|
212
|
+
form.respond_to?(:labeled_text_field).should be_true
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|