snusnu-merb_resource_controller 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/LICENSE +20 -0
  2. data/README.textile +306 -0
  3. data/Rakefile +81 -0
  4. data/TODO +7 -0
  5. data/lib/merb_resource_controller/action_timeout_support.rb +53 -0
  6. data/lib/merb_resource_controller/actions.rb +169 -0
  7. data/lib/merb_resource_controller/identity_map_support.rb +20 -0
  8. data/lib/merb_resource_controller/resource_controller.rb +160 -0
  9. data/lib/merb_resource_controller/resource_proxy.rb +317 -0
  10. data/lib/merb_resource_controller.rb +29 -0
  11. data/spec/mrc_test_app/Rakefile +52 -0
  12. data/spec/mrc_test_app/app/controllers/application.rb +6 -0
  13. data/spec/mrc_test_app/app/controllers/articles.rb +3 -0
  14. data/spec/mrc_test_app/app/controllers/community/comments.rb +9 -0
  15. data/spec/mrc_test_app/app/controllers/community/ratings.rb +9 -0
  16. data/spec/mrc_test_app/app/controllers/editors.rb +7 -0
  17. data/spec/mrc_test_app/app/models/article.rb +19 -0
  18. data/spec/mrc_test_app/app/models/editor.rb +11 -0
  19. data/spec/mrc_test_app/app/views/articles/edit.html.erb +13 -0
  20. data/spec/mrc_test_app/app/views/articles/index.html.erb +25 -0
  21. data/spec/mrc_test_app/app/views/articles/new.html.erb +12 -0
  22. data/spec/mrc_test_app/app/views/articles/show.html.erb +8 -0
  23. data/spec/mrc_test_app/app/views/community/comments/edit.html.erb +12 -0
  24. data/spec/mrc_test_app/app/views/community/comments/index.html.erb +25 -0
  25. data/spec/mrc_test_app/app/views/community/comments/new.html.erb +3 -0
  26. data/spec/mrc_test_app/app/views/community/comments/show.html.erb +3 -0
  27. data/spec/mrc_test_app/app/views/community/ratings/edit.html.erb +11 -0
  28. data/spec/mrc_test_app/app/views/community/ratings/index.html.erb +25 -0
  29. data/spec/mrc_test_app/app/views/community/ratings/show.html.erb +3 -0
  30. data/spec/mrc_test_app/app/views/editors/edit.html.erb +12 -0
  31. data/spec/mrc_test_app/app/views/editors/new.html.erb +12 -0
  32. data/spec/mrc_test_app/app/views/editors/show.html.erb +7 -0
  33. data/spec/mrc_test_app/config/database.yml +33 -0
  34. data/spec/mrc_test_app/config/environments/development.rb +15 -0
  35. data/spec/mrc_test_app/config/environments/rake.rb +11 -0
  36. data/spec/mrc_test_app/config/environments/test.rb +12 -0
  37. data/spec/mrc_test_app/config/init.rb +36 -0
  38. data/spec/mrc_test_app/config/rack.rb +11 -0
  39. data/spec/mrc_test_app/config/router.rb +50 -0
  40. data/spec/mrc_test_app/spec/lib/resource_proxy_spec.rb +292 -0
  41. data/spec/mrc_test_app/spec/request/article_comments_spec.rb +208 -0
  42. data/spec/mrc_test_app/spec/request/article_editor_spec.rb +202 -0
  43. data/spec/mrc_test_app/spec/request/articles_spec.rb +208 -0
  44. data/spec/mrc_test_app/spec/request/comments_spec.rb +221 -0
  45. data/spec/mrc_test_app/spec/spec.opts +2 -0
  46. data/spec/mrc_test_app/spec/spec_helper.rb +206 -0
  47. metadata +166 -0
@@ -0,0 +1,208 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "GET" do
4
+
5
+ describe "resource(:article_comments)", :given => "2 articles and 3 comments exist" do
6
+
7
+ it "should respond successfully" do
8
+ request(resource(Article.first, :comments)).should be_successful
9
+ end
10
+
11
+ it "should render the :index template" do
12
+ @response = request(resource(Article.get(1), :comments))
13
+ @response.should have_selector("tr:eq(2) td:nth-child(1):contains('1')")
14
+ @response.should have_selector("tr:eq(2) td:nth-child(2):contains('article title')")
15
+ @response.should have_selector("tr:eq(2) td:nth-child(3):contains('comment body')")
16
+ @response.should have_selector("tr:eq(3) td:nth-child(1):contains('2')")
17
+ @response.should have_selector("tr:eq(3) td:nth-child(2):contains('article title')")
18
+ @response.should have_selector("tr:eq(3) td:nth-child(3):contains('comment body')")
19
+ @response.should_not have_selector("tr:eq(4)")
20
+
21
+ @response = request(resource(Article.get(2), :comments))
22
+ @response.should have_selector("tr:eq(2) td:nth-child(1):contains('3')")
23
+ @response.should have_selector("tr:eq(2) td:nth-child(2):contains('article title')")
24
+ @response.should have_selector("tr:eq(2) td:nth-child(3):contains('comment body')")
25
+ @response.should_not have_selector("tr:eq(3)")
26
+ end
27
+
28
+ end
29
+
30
+ describe "resource(@article, :comments, :new)", :given => "an Article exists" do
31
+
32
+ before(:each) do
33
+ @response = request(resource(Article.first, :comments, :new))
34
+ end
35
+
36
+ it "should respond successfully" do
37
+ @response.should be_successful
38
+ end
39
+
40
+ it "should render the :new template" do
41
+ @response.should have_selector("h2:contains('New Comment')")
42
+ end
43
+
44
+ end
45
+
46
+ describe "resource(@article, @comment)", :given => "2 articles and 3 comments exist" do
47
+
48
+ before(:each) do
49
+ @response = request(resource(Article.first, Community::Comment.first))
50
+ end
51
+
52
+ it "should respond successfully" do
53
+ @response.should be_successful
54
+ end
55
+
56
+ it "should render the :show template" do
57
+ @response.should have_selector("h2:contains('Show Comment')")
58
+ end
59
+
60
+ end
61
+
62
+ describe "resource(@article, @comment, :edit)", :given => "a Comment exists" do
63
+
64
+ before(:each) do
65
+ @response = request(resource(Article.first, Community::Comment.first, :edit))
66
+ end
67
+
68
+ it "should respond successfully" do
69
+ @response.should be_successful
70
+ end
71
+
72
+ it "should render the :edit template" do
73
+ @response.should have_selector("h2:contains('Edit Comment')")
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+ describe "POST" do
81
+
82
+ describe "Success", :given => "an Article exists" do
83
+
84
+ before(:each) do
85
+ @response = request(
86
+ resource(Article.first, :comments),
87
+ :method => "POST",
88
+ :params => {
89
+ :comment => {
90
+ :id => nil,
91
+ :article_id => Article.first.id,
92
+ :body => "comment body"
93
+ }
94
+ }
95
+ )
96
+ end
97
+
98
+ it "should redirect to resource(@article, @comment)" do
99
+ @response.should redirect_to(
100
+ resource(Article.first, Community::Comment.first),
101
+ :message => {
102
+ :notice => "Comment was successfully created"
103
+ }
104
+ )
105
+ end
106
+
107
+ end
108
+
109
+ describe "Failure", :given => "an Article exists" do
110
+
111
+ before(:each) do
112
+ @response = request(
113
+ resource(Article.first, :comments),
114
+ :method => "POST",
115
+ :params => {
116
+ :comment => {
117
+ :id => nil,
118
+ :article_id => nil,
119
+ :body => "comment body"
120
+ }
121
+ }
122
+ )
123
+ end
124
+
125
+ it "should not be successful" do
126
+ @response.should_not be_successful
127
+ @response.status.should == 406
128
+ end
129
+
130
+ it "should render the :new action" do
131
+ @response.should have_selector("h2:contains('New Comment')")
132
+ end
133
+
134
+ end
135
+
136
+ end
137
+
138
+ describe "PUT resource(@article, @comment)", :given => "2 articles and 3 comments exist" do
139
+
140
+ describe "Success" do
141
+
142
+ before(:each) do
143
+ @article = Article.first
144
+ @comment = Community::Comment.first
145
+ @response = request(resource(@article, @comment), :method => "PUT",
146
+ :params => { :comment => {:id => @comment.id} })
147
+ end
148
+
149
+ it "should redirect to resource(@article, @comment)" do
150
+ @response.should redirect_to(resource(@article, @comment))
151
+ end
152
+
153
+ end
154
+
155
+ describe "Failure" do
156
+
157
+ before(:each) do
158
+ @article = Article.first
159
+ @comment = Community::Comment.first
160
+ @response = request(resource(@article, @comment), :method => "PUT",
161
+ :params => { :comment => {:id => @comment.id, :body => nil } })
162
+ end
163
+
164
+ it "should not be successful" do
165
+ @response.should_not be_successful
166
+ @response.status.should == 406
167
+ end
168
+
169
+ it "should render the :edit template" do
170
+ @response.should have_selector("h2:contains('Edit Comment')")
171
+ end
172
+
173
+ end
174
+
175
+ end
176
+
177
+ describe "DELETE resource(@article, @comment)" do
178
+
179
+ describe "Success", :given => "a Comment exists" do
180
+
181
+ before(:each) do
182
+ @response = request(
183
+ resource(Article.first, Community::Comment.first),
184
+ :method => "DELETE"
185
+ )
186
+ end
187
+
188
+ it "should redirect to resource(@article, :comments)" do
189
+ @response.should redirect_to(resource(Article.first, :comments))
190
+ end
191
+
192
+ end
193
+
194
+ describe "Failure", :given => "an Article exists" do
195
+
196
+ before(:each) do
197
+ Community::Comment.all.destroy!
198
+ @response = request("/articles/#{Article.first.id}/comments/1", :method => "DELETE")
199
+ end
200
+
201
+ it "should not be successful" do
202
+ @response.should_not be_successful
203
+ @response.status.should == 404
204
+ end
205
+
206
+ end
207
+
208
+ end
@@ -0,0 +1,202 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "GET" do
4
+
5
+ describe "resource(@article, :editors)", :given => "an Article exists" do
6
+
7
+ before(:each) do
8
+ @response = request(resource(Article.first, :editor, :new))
9
+ end
10
+
11
+ it "should not respond to the :index action" do
12
+ lambda {
13
+ request(resource(Article.first, :editors))
14
+ }.should raise_error(Merb::Router::GenerationError)
15
+ end
16
+
17
+ end
18
+
19
+ describe "resource(@article, :editor, :new)", :given => "an Article exists" do
20
+
21
+ before(:each) do
22
+ @response = request(resource(Article.first, :editor, :new))
23
+ end
24
+
25
+ it "should respond successfully" do
26
+ @response.should be_successful
27
+ end
28
+
29
+ it "should render the :new template" do
30
+ @response.should have_selector("h2:contains('New Editor')")
31
+ end
32
+
33
+ end
34
+
35
+ describe "resource(@article, :editor)", :given => "an Editor exists" do
36
+
37
+ before(:each) do
38
+ @response = request(resource(Article.first, :editor))
39
+ end
40
+
41
+ it "should respond successfully" do
42
+ @response.should be_successful
43
+ end
44
+
45
+ end
46
+
47
+ describe "resource(@article, :editor, :edit)", :given => "an Editor exists" do
48
+
49
+ before(:each) do
50
+ @response = request(resource(Article.first, :editor, :edit))
51
+ end
52
+
53
+ it "should respond successfully" do
54
+ @response.should be_successful
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ describe "POST resource(@article, :editor)" do
62
+
63
+ describe "Success", :given => "an Article exists" do
64
+
65
+ before(:each) do
66
+ Editor.all.destroy!
67
+ @response = request(
68
+ resource(Article.first, :editor),
69
+ :method => "POST",
70
+ :params => {
71
+ :editor => {
72
+ :id => nil,
73
+ :name => "snusnu"
74
+ }
75
+ }
76
+ )
77
+ end
78
+
79
+ it "should redirect to resource(@article, :editor)" do
80
+ @response.should redirect_to(
81
+ resource(Article.first, :editor),
82
+ :message => {
83
+ :notice => "Editor was successfully created"
84
+ }
85
+ )
86
+ end
87
+
88
+ end
89
+
90
+ describe "Failure", :given => "an Article exists" do
91
+
92
+ before(:each) do
93
+ Editor.all.destroy!
94
+ @response = request(resource(Article.first, :editor),
95
+ :method => "POST",
96
+ :params => {
97
+ :editor => {
98
+ :id => nil,
99
+ :name => nil
100
+ }
101
+ }
102
+ )
103
+ end
104
+
105
+ it "should not be successful" do
106
+ @response.should_not be_successful
107
+ @response.status.should == 406
108
+ end
109
+
110
+ it "should render the :new action" do
111
+ @response.should have_selector("h2:contains('New Editor')")
112
+ end
113
+
114
+ end
115
+
116
+ end
117
+
118
+ describe "PUT resource(@article, :editor)", :given => "an Editor exists" do
119
+
120
+ describe "Success" do
121
+
122
+ before(:each) do
123
+ @response = request(
124
+ resource(Article.first, :editor),
125
+ :method => "PUT",
126
+ :params => {
127
+ :editor => {
128
+ :id => Editor.first.id,
129
+ :name => "bender"
130
+ }
131
+ }
132
+ )
133
+ end
134
+
135
+ it "should redirect to resource(@article, :editor)" do
136
+ @response.should redirect_to(resource(Article.first, :editor))
137
+ end
138
+
139
+ end
140
+
141
+ describe "Failure" do
142
+
143
+ before(:each) do
144
+ @response = request(
145
+ resource(Article.first, :editor),
146
+ :method => "PUT",
147
+ :params => {
148
+ :editor => {
149
+ :id => Editor.first.id,
150
+ :name => nil
151
+ }
152
+ }
153
+ )
154
+ end
155
+
156
+ it "should not be successful" do
157
+ @response.should_not be_successful
158
+ @response.status.should == 406
159
+ end
160
+
161
+ it "should render the :edit template" do
162
+ @response.should have_selector("h2:contains('Edit Editor')")
163
+ end
164
+
165
+ end
166
+
167
+ end
168
+
169
+ describe "DELETE resource(@article, @editor)" do
170
+
171
+ describe "Success", :given => "an Editor exists" do
172
+
173
+ before(:each) do
174
+ @response = request(
175
+ resource(Article.first, :editor),
176
+ :method => "DELETE"
177
+ )
178
+ end
179
+
180
+ it "should redirect to resource(@article)" do
181
+ @response.should redirect_to(resource(Article.first))
182
+ end
183
+
184
+ end
185
+
186
+ describe "Failure", :given => "an Article exists" do
187
+
188
+ before(:each) do
189
+ @response = request(
190
+ resource(Article.first, :editor),
191
+ :method => "DELETE"
192
+ )
193
+ end
194
+
195
+ it "should not be successful" do
196
+ @response.should_not be_successful
197
+ @response.status.should == 404
198
+ end
199
+
200
+ end
201
+
202
+ end
@@ -0,0 +1,208 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "GET" do
4
+
5
+ describe "resource(:articles)", :given => "an Article exists" do
6
+
7
+ before(:each) do
8
+ @response = request(resource(:articles))
9
+ end
10
+
11
+ it "should respond successfully" do
12
+ @response.should be_successful
13
+ end
14
+
15
+ it "should render the :index template" do
16
+ @response.should have_selector("tr:nth-child(2)")
17
+ @response.should have_selector("td:nth-child(1):contains('article title')")
18
+ @response.should have_selector("td:nth-child(2):contains('Anonymous')")
19
+ @response.should have_selector("td:nth-child(3):contains('article body')")
20
+ end
21
+
22
+ end
23
+
24
+ describe "resource(:articles, :new)" do
25
+
26
+ before(:each) do
27
+ @response = request(resource(:articles, :new))
28
+ end
29
+
30
+ it "should respond successfully" do
31
+ @response.should be_successful
32
+ end
33
+
34
+ it "should render the :new template" do
35
+ @response.should have_selector("h2:contains('New Article')")
36
+ end
37
+
38
+ end
39
+
40
+ describe "resource(@article)", :given => "an Article exists" do
41
+
42
+ before(:each) do
43
+ @response = request(resource(Article.first))
44
+ end
45
+
46
+ it "should respond successfully" do
47
+ @response.should be_successful
48
+ end
49
+
50
+ it "should render the :show template" do
51
+ @response.should have_selector("h2:contains('Show Article')")
52
+ @response.should have_selector("h3:contains('article title')")
53
+ @response.should have_selector("p:contains('article body')")
54
+ end
55
+
56
+ end
57
+
58
+ describe "resource(@article, :edit)", :given => "an Article exists" do
59
+
60
+ before(:each) do
61
+ @response = request(resource(Article.first, :edit))
62
+ end
63
+
64
+ it "should respond successfully" do
65
+ @response.should be_successful
66
+ end
67
+
68
+ it "should render the :edit template" do
69
+ @response.should have_selector("h2:contains('Edit Article')")
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+
77
+ describe "POST resource(:articles)" do
78
+
79
+ describe "Success" do
80
+
81
+ before(:each) do
82
+ Article.all.destroy!
83
+ @response = request(
84
+ resource(:articles),
85
+ :method => "POST",
86
+ :params => {
87
+ :article => {
88
+ :id => nil,
89
+ :title => "article title",
90
+ :body => "article body"
91
+ }
92
+ }
93
+ )
94
+ end
95
+
96
+ it "should redirect to resource(@article)" do
97
+ @response.should redirect_to(
98
+ resource(Article.first),
99
+ :message => {
100
+ :notice => "Article was successfully created"
101
+ }
102
+ )
103
+ end
104
+
105
+ end
106
+
107
+ describe "Failure" do
108
+
109
+ before(:each) do
110
+ Article.all.destroy!
111
+ @response = request(
112
+ resource(:articles),
113
+ :method => "POST",
114
+ :params => {
115
+ :article => {
116
+ :id => nil,
117
+ :title => nil,
118
+ :body => "article body"
119
+ }
120
+ }
121
+ )
122
+ end
123
+
124
+ it "should not be successful" do
125
+ @response.should_not be_successful
126
+ @response.status.should == 406
127
+ end
128
+
129
+ it "should render the :new action" do
130
+ @response.should have_selector("h2:contains('New Article')")
131
+ end
132
+
133
+ end
134
+
135
+ end
136
+
137
+ describe "PUT resource(@article)", :given => "an Article exists" do
138
+
139
+ describe "Success" do
140
+
141
+ before(:each) do
142
+ @article = Article.first
143
+ @response = request(
144
+ resource(@article),
145
+ :method => "PUT",
146
+ :params => { :article => { :id => @article.id, :title => "updated title", :body => "updated body" } }
147
+ )
148
+ end
149
+
150
+ it "should redirect to resource(@article)" do
151
+ @response.should redirect_to(resource(@article))
152
+ end
153
+
154
+ end
155
+
156
+ describe "Failure" do
157
+
158
+ before(:each) do
159
+ @article = Article.first
160
+ @response = request(
161
+ resource(@article),
162
+ :method => "PUT",
163
+ :params => { :article => { :id => @article.id, :title => nil, :body => "updated body" } }
164
+ )
165
+ end
166
+
167
+ it "should not be successful" do
168
+ @response.should_not be_successful
169
+ @response.status.should == 406
170
+ end
171
+
172
+ it "should render the :edit template" do
173
+ @response.should have_selector("h2:contains('Edit Article')")
174
+ end
175
+
176
+ end
177
+
178
+ end
179
+
180
+ describe "DELETE resource(@article)" do
181
+
182
+ describe "Success", :given => "an Article exists" do
183
+
184
+ before(:each) do
185
+ @response = request(resource(Article.first), :method => "DELETE")
186
+ end
187
+
188
+ it "should redirect to resource(:articles)" do
189
+ @response.should redirect_to(resource(:articles))
190
+ end
191
+
192
+ end
193
+
194
+ describe "Failure" do
195
+
196
+ before(:each) do
197
+ Article.all.destroy!
198
+ @response = request('/articles/1', :method => "DELETE")
199
+ end
200
+
201
+ it "should not be successful" do
202
+ @response.should_not be_successful
203
+ @response.status.should == 404
204
+ end
205
+
206
+ end
207
+
208
+ end