tpitale-shoulda 2.11.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 (170) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +10 -0
  2. data/MIT-LICENSE +22 -0
  3. data/README.rdoc +171 -0
  4. data/Rakefile +71 -0
  5. data/bin/convert_to_should_syntax +42 -0
  6. data/lib/shoulda.rb +11 -0
  7. data/lib/shoulda/action_controller.rb +26 -0
  8. data/lib/shoulda/action_controller/macros.rb +240 -0
  9. data/lib/shoulda/action_controller/matchers.rb +37 -0
  10. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +109 -0
  11. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +57 -0
  12. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +81 -0
  13. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +74 -0
  14. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +81 -0
  15. data/lib/shoulda/action_controller/matchers/route_matcher.rb +93 -0
  16. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +87 -0
  17. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +85 -0
  18. data/lib/shoulda/action_mailer.rb +10 -0
  19. data/lib/shoulda/action_mailer/assertions.rb +38 -0
  20. data/lib/shoulda/action_view.rb +10 -0
  21. data/lib/shoulda/action_view/macros.rb +61 -0
  22. data/lib/shoulda/active_record.rb +16 -0
  23. data/lib/shoulda/active_record/assertions.rb +69 -0
  24. data/lib/shoulda/active_record/helpers.rb +27 -0
  25. data/lib/shoulda/active_record/macros.rb +512 -0
  26. data/lib/shoulda/active_record/matchers.rb +43 -0
  27. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  28. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
  29. data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
  30. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  31. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  32. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  33. data/lib/shoulda/active_record/matchers/have_db_index_matcher.rb +112 -0
  34. data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +128 -0
  35. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
  36. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
  37. data/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb +67 -0
  38. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
  39. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  40. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  41. data/lib/shoulda/active_record/matchers/validation_matcher.rb +57 -0
  42. data/lib/shoulda/assertions.rb +71 -0
  43. data/lib/shoulda/autoload_macros.rb +46 -0
  44. data/lib/shoulda/context.rb +413 -0
  45. data/lib/shoulda/helpers.rb +8 -0
  46. data/lib/shoulda/macros.rb +133 -0
  47. data/lib/shoulda/minitest.rb +24 -0
  48. data/lib/shoulda/private_helpers.rb +13 -0
  49. data/lib/shoulda/proc_extensions.rb +14 -0
  50. data/lib/shoulda/rails.rb +13 -0
  51. data/lib/shoulda/rspec.rb +11 -0
  52. data/lib/shoulda/tasks.rb +3 -0
  53. data/lib/shoulda/tasks/list_tests.rake +29 -0
  54. data/lib/shoulda/tasks/yaml_to_shoulda.rake +28 -0
  55. data/lib/shoulda/test_unit.rb +22 -0
  56. data/rails/init.rb +7 -0
  57. data/test/README +36 -0
  58. data/test/fail_macros.rb +39 -0
  59. data/test/fixtures/addresses.yml +3 -0
  60. data/test/fixtures/friendships.yml +0 -0
  61. data/test/fixtures/posts.yml +5 -0
  62. data/test/fixtures/products.yml +0 -0
  63. data/test/fixtures/taggings.yml +0 -0
  64. data/test/fixtures/tags.yml +9 -0
  65. data/test/fixtures/users.yml +6 -0
  66. data/test/functional/posts_controller_test.rb +121 -0
  67. data/test/functional/users_controller_test.rb +19 -0
  68. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +68 -0
  69. data/test/matchers/active_record/allow_value_matcher_test.rb +64 -0
  70. data/test/matchers/active_record/association_matcher_test.rb +263 -0
  71. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +80 -0
  72. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +158 -0
  73. data/test/matchers/active_record/have_db_column_matcher_test.rb +169 -0
  74. data/test/matchers/active_record/have_db_index_matcher_test.rb +91 -0
  75. data/test/matchers/active_record/have_named_scope_matcher_test.rb +65 -0
  76. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +29 -0
  77. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +44 -0
  78. data/test/matchers/active_record/validate_format_of_matcher_test.rb +39 -0
  79. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +52 -0
  80. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +86 -0
  81. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +147 -0
  82. data/test/matchers/controller/assign_to_matcher_test.rb +35 -0
  83. data/test/matchers/controller/filter_param_matcher_test.rb +32 -0
  84. data/test/matchers/controller/render_with_layout_matcher_test.rb +33 -0
  85. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +32 -0
  86. data/test/matchers/controller/respond_with_matcher_test.rb +106 -0
  87. data/test/matchers/controller/route_matcher_test.rb +58 -0
  88. data/test/matchers/controller/set_session_matcher_test.rb +38 -0
  89. data/test/matchers/controller/set_the_flash_matcher.rb +41 -0
  90. data/test/model_builder.rb +106 -0
  91. data/test/other/autoload_macro_test.rb +18 -0
  92. data/test/other/context_test.rb +203 -0
  93. data/test/other/convert_to_should_syntax_test.rb +63 -0
  94. data/test/other/helpers_test.rb +340 -0
  95. data/test/other/private_helpers_test.rb +32 -0
  96. data/test/other/should_test.rb +271 -0
  97. data/test/rails_root/app/controllers/application_controller.rb +25 -0
  98. data/test/rails_root/app/controllers/posts_controller.rb +87 -0
  99. data/test/rails_root/app/controllers/users_controller.rb +84 -0
  100. data/test/rails_root/app/helpers/application_helper.rb +3 -0
  101. data/test/rails_root/app/helpers/posts_helper.rb +2 -0
  102. data/test/rails_root/app/helpers/users_helper.rb +2 -0
  103. data/test/rails_root/app/models/address.rb +7 -0
  104. data/test/rails_root/app/models/flea.rb +3 -0
  105. data/test/rails_root/app/models/friendship.rb +4 -0
  106. data/test/rails_root/app/models/pets/cat.rb +7 -0
  107. data/test/rails_root/app/models/pets/dog.rb +10 -0
  108. data/test/rails_root/app/models/post.rb +12 -0
  109. data/test/rails_root/app/models/product.rb +12 -0
  110. data/test/rails_root/app/models/profile.rb +2 -0
  111. data/test/rails_root/app/models/registration.rb +2 -0
  112. data/test/rails_root/app/models/tag.rb +8 -0
  113. data/test/rails_root/app/models/tagging.rb +4 -0
  114. data/test/rails_root/app/models/treat.rb +3 -0
  115. data/test/rails_root/app/models/user.rb +32 -0
  116. data/test/rails_root/app/views/layouts/posts.rhtml +19 -0
  117. data/test/rails_root/app/views/layouts/users.rhtml +17 -0
  118. data/test/rails_root/app/views/layouts/wide.html.erb +1 -0
  119. data/test/rails_root/app/views/posts/edit.rhtml +27 -0
  120. data/test/rails_root/app/views/posts/index.rhtml +25 -0
  121. data/test/rails_root/app/views/posts/new.rhtml +26 -0
  122. data/test/rails_root/app/views/posts/show.rhtml +18 -0
  123. data/test/rails_root/app/views/users/edit.rhtml +22 -0
  124. data/test/rails_root/app/views/users/index.rhtml +22 -0
  125. data/test/rails_root/app/views/users/new.rhtml +21 -0
  126. data/test/rails_root/app/views/users/show.rhtml +13 -0
  127. data/test/rails_root/config/boot.rb +110 -0
  128. data/test/rails_root/config/database.yml +4 -0
  129. data/test/rails_root/config/environment.rb +18 -0
  130. data/test/rails_root/config/environments/test.rb +0 -0
  131. data/test/rails_root/config/initializers/new_rails_defaults.rb +15 -0
  132. data/test/rails_root/config/initializers/shoulda.rb +8 -0
  133. data/test/rails_root/config/routes.rb +6 -0
  134. data/test/rails_root/db/migrate/001_create_users.rb +19 -0
  135. data/test/rails_root/db/migrate/002_create_posts.rb +13 -0
  136. data/test/rails_root/db/migrate/003_create_taggings.rb +12 -0
  137. data/test/rails_root/db/migrate/004_create_tags.rb +11 -0
  138. data/test/rails_root/db/migrate/005_create_dogs.rb +12 -0
  139. data/test/rails_root/db/migrate/006_create_addresses.rb +14 -0
  140. data/test/rails_root/db/migrate/007_create_fleas.rb +11 -0
  141. data/test/rails_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  142. data/test/rails_root/db/migrate/009_create_products.rb +17 -0
  143. data/test/rails_root/db/migrate/010_create_friendships.rb +14 -0
  144. data/test/rails_root/db/migrate/011_create_treats.rb +12 -0
  145. data/test/rails_root/db/migrate/20090506203502_create_profiles.rb +12 -0
  146. data/test/rails_root/db/migrate/20090506203536_create_registrations.rb +14 -0
  147. data/test/rails_root/db/migrate/20090513104502_create_cats.rb +12 -0
  148. data/test/rails_root/db/schema.rb +0 -0
  149. data/test/rails_root/log/test.log +1 -0
  150. data/test/rails_root/public/404.html +30 -0
  151. data/test/rails_root/public/422.html +30 -0
  152. data/test/rails_root/public/500.html +30 -0
  153. data/test/rails_root/script/console +3 -0
  154. data/test/rails_root/script/generate +3 -0
  155. data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
  156. data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  157. data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  158. data/test/rspec_test.rb +207 -0
  159. data/test/test_helper.rb +28 -0
  160. data/test/unit/address_test.rb +10 -0
  161. data/test/unit/cat_test.rb +7 -0
  162. data/test/unit/dog_test.rb +9 -0
  163. data/test/unit/flea_test.rb +6 -0
  164. data/test/unit/friendship_test.rb +6 -0
  165. data/test/unit/post_test.rb +19 -0
  166. data/test/unit/product_test.rb +23 -0
  167. data/test/unit/tag_test.rb +15 -0
  168. data/test/unit/tagging_test.rb +6 -0
  169. data/test/unit/user_test.rb +80 -0
  170. metadata +226 -0
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class PrivateHelpersTest < ActiveSupport::TestCase # :nodoc:
4
+ include Shoulda::Private
5
+ context "get_options!" do
6
+ should "remove opts from args" do
7
+ args = [:a, :b, {}]
8
+ get_options!(args)
9
+ assert_equal [:a, :b], args
10
+ end
11
+
12
+ should "return wanted opts in order" do
13
+ args = [{:one => 1, :two => 2}]
14
+ one, two = get_options!(args, :one, :two)
15
+ assert_equal 1, one
16
+ assert_equal 2, two
17
+ end
18
+
19
+ should "raise ArgumentError if given unwanted option" do
20
+ args = [{:one => 1, :two => 2}]
21
+ assert_raises ArgumentError do
22
+ get_options!(args, :one)
23
+ end
24
+ end
25
+
26
+ should "return single wanted option" do
27
+ args = [:a, :b, {:class => Object}]
28
+ klass = get_options!(args,:class)
29
+ assert_equal Object, klass
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,271 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class ShouldTest < ActiveSupport::TestCase # :nodoc:
4
+ should "be able to define a should statement outside of a context" do
5
+ assert true
6
+ end
7
+
8
+ should "see the name of my class as ShouldTest" do
9
+ assert_equal "ShouldTest", self.class.name
10
+ end
11
+
12
+ def self.should_see_class_methods
13
+ should "be able to see class methods" do
14
+ assert true
15
+ end
16
+ end
17
+
18
+ def self.should_be_able_to_setup_a_should_eventually_in_a_class_method
19
+ should "be able to setup a should eventually in a class method"
20
+ end
21
+
22
+ def self.should_see_a_context_block_like_a_Test_Unit_class
23
+ should "see a context block as a Test::Unit class" do
24
+ assert_equal "ShouldTest", self.class.name
25
+ end
26
+ end
27
+
28
+ def self.should_see_blah
29
+ should "see @blah through a macro" do
30
+ assert @blah
31
+ end
32
+ end
33
+
34
+ def self.should_not_see_blah
35
+ should "not see @blah through a macro" do
36
+ assert_nil @blah
37
+ end
38
+ end
39
+
40
+ def self.should_be_able_to_make_context_macros(prefix = nil)
41
+ context "a macro" do
42
+ should "have the tests named correctly" do
43
+ assert_match(/^test: #{prefix}a macro should have the tests named correctly/, self.to_s)
44
+ end
45
+ end
46
+ end
47
+
48
+ context "Context" do
49
+
50
+ should_see_class_methods
51
+ should_see_a_context_block_like_a_Test_Unit_class
52
+ should_be_able_to_make_context_macros("Context ")
53
+ should_be_able_to_setup_a_should_eventually_in_a_class_method
54
+
55
+ should "not define @blah" do
56
+ assert ! self.instance_variables.include?("@blah")
57
+ end
58
+
59
+ should_not_see_blah
60
+
61
+ should "be able to define a should statement" do
62
+ assert true
63
+ end
64
+
65
+ should "see the name of my class as ShouldTest" do
66
+ assert_equal "ShouldTest", self.class.name
67
+ end
68
+
69
+ context "with a subcontext" do
70
+ should_be_able_to_make_context_macros("Context with a subcontext ")
71
+ end
72
+ end
73
+
74
+ context "Context with setup block" do
75
+ setup do
76
+ @blah = "blah"
77
+ end
78
+
79
+ should "have @blah == 'blah'" do
80
+ assert_equal "blah", @blah
81
+ end
82
+ should_see_blah
83
+
84
+ should "have name set right" do
85
+ assert_match(/^test: Context with setup block/, self.to_s)
86
+ end
87
+
88
+ context "and a subcontext" do
89
+ setup do
90
+ @blah = "#{@blah} twice"
91
+ end
92
+
93
+ should "be named correctly" do
94
+ assert_match(/^test: Context with setup block and a subcontext should be named correctly/, self.to_s)
95
+ end
96
+
97
+ should "run the setup methods in order" do
98
+ assert_equal @blah, "blah twice"
99
+ end
100
+ should_see_blah
101
+ end
102
+ end
103
+
104
+ context "Another context with setup block" do
105
+ setup do
106
+ @blah = "foo"
107
+ end
108
+
109
+ should "have @blah == 'foo'" do
110
+ assert_equal "foo", @blah
111
+ end
112
+
113
+ should "have name set right" do
114
+ assert_match(/^test: Another context with setup block/, self.to_s)
115
+ end
116
+ should_see_blah
117
+ end
118
+
119
+ should_eventually "pass, since it's a should_eventually" do
120
+ flunk "what?"
121
+ end
122
+
123
+ # Context creation and naming
124
+
125
+ def test_should_create_a_new_context
126
+ assert_nothing_raised do
127
+ Shoulda::Context.new("context name", self) do; end
128
+ end
129
+ end
130
+
131
+ def test_should_create_a_nested_context
132
+ assert_nothing_raised do
133
+ parent = Shoulda::Context.new("Parent", self) do; end
134
+ child = Shoulda::Context.new("Child", parent) do; end
135
+ end
136
+ end
137
+
138
+ def test_should_name_a_contexts_correctly
139
+ parent = Shoulda::Context.new("Parent", self) do; end
140
+ child = Shoulda::Context.new("Child", parent) do; end
141
+ grandchild = Shoulda::Context.new("GrandChild", child) do; end
142
+
143
+ assert_equal "Parent", parent.full_name
144
+ assert_equal "Parent Child", child.full_name
145
+ assert_equal "Parent Child GrandChild", grandchild.full_name
146
+ end
147
+
148
+ # Should statements
149
+
150
+ def test_should_have_should_hashes_when_given_should_statements
151
+ context = Shoulda::Context.new("name", self) do
152
+ should "be good" do; end
153
+ should "another" do; end
154
+ end
155
+
156
+ names = context.shoulds.map {|s| s[:name]}
157
+ assert_equal ["another", "be good"], names.sort
158
+ end
159
+
160
+ # setup and teardown
161
+
162
+ def test_should_capture_setup_and_teardown_blocks
163
+ context = Shoulda::Context.new("name", self) do
164
+ setup do; "setup"; end
165
+ teardown do; "teardown"; end
166
+ end
167
+
168
+ assert_equal "setup", context.setup_blocks.first.call
169
+ assert_equal "teardown", context.teardown_blocks.first.call
170
+ end
171
+
172
+ # building
173
+
174
+ def test_should_create_shoulda_test_for_each_should_on_build
175
+ context = Shoulda::Context.new("name", self) do
176
+ should "one" do; end
177
+ should "two" do; end
178
+ end
179
+ context.expects(:create_test_from_should_hash).with(has_entry(:name => "one"))
180
+ context.expects(:create_test_from_should_hash).with(has_entry(:name => "two"))
181
+ context.build
182
+ end
183
+
184
+ def test_should_create_test_methods_on_build
185
+ tu_class = ActiveSupport::TestCase
186
+ context = Shoulda::Context.new("A Context", tu_class) do
187
+ should "define the test" do; end
188
+ end
189
+
190
+ tu_class.expects(:define_method).with(:"test: A Context should define the test. ")
191
+ context.build
192
+ end
193
+
194
+ def test_should_create_test_methods_on_build_when_subcontext
195
+ tu_class = ActiveSupport::TestCase
196
+ context = Shoulda::Context.new("A Context", tu_class) do
197
+ context "with a child" do
198
+ should "define the test" do; end
199
+ end
200
+ end
201
+
202
+ tu_class.expects(:define_method).with(:"test: A Context with a child should define the test. ")
203
+ context.build
204
+ end
205
+
206
+ # Test::Unit integration
207
+
208
+ def test_should_create_a_new_context_and_build_it_on_Test_Unit_context
209
+ c = mock("context")
210
+ c.expects(:build)
211
+ Shoulda::Context.expects(:new).with("foo", kind_of(Class)).returns(c)
212
+ self.class.context "foo" do; end
213
+ end
214
+
215
+ def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should
216
+ s = mock("test")
217
+ Shoulda::Context.any_instance.expects(:should).with("rock", {}).returns(s)
218
+ Shoulda::Context.any_instance.expects(:build)
219
+ self.class.should "rock" do; end
220
+ end
221
+
222
+ def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should_eventually
223
+ s = mock("test")
224
+ Shoulda::Context.any_instance.expects(:should_eventually).with("rock").returns(s)
225
+ Shoulda::Context.any_instance.expects(:build)
226
+ self.class.should_eventually "rock" do; end
227
+ end
228
+
229
+ should "run a :before proc", :before => lambda { @value = "before" } do
230
+ assert_equal "before", @value
231
+ end
232
+
233
+ context "A :before proc" do
234
+ setup do
235
+ assert_equal "before", @value
236
+ @value = "setup"
237
+ end
238
+
239
+ should "run before the current setup", :before => lambda { @value = "before" } do
240
+ assert_equal "setup", @value
241
+ end
242
+ end
243
+
244
+ context "a before statement" do
245
+ setup do
246
+ assert_equal "before", @value
247
+ @value = "setup"
248
+ end
249
+
250
+ before_should "run before the current setup" do
251
+ @value = "before"
252
+ end
253
+ end
254
+
255
+ context "A context" do
256
+ setup do
257
+ @value = "outer"
258
+ end
259
+
260
+ context "with a subcontext and a :before proc" do
261
+ before = lambda do
262
+ assert "outer", @value
263
+ @value = "before"
264
+ end
265
+ should "run after the parent setup", :before => before do
266
+ assert_equal "before", @value
267
+ end
268
+ end
269
+ end
270
+
271
+ end
@@ -0,0 +1,25 @@
1
+ # Filters added to this controller apply to all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+
4
+ class ApplicationController < ActionController::Base
5
+ # Pick a unique cookie name to distinguish our session data from others'
6
+ session :session_key => '_rails_root_session_id'
7
+
8
+ def ensure_logged_in
9
+ unless session[:logged_in]
10
+ respond_to do |accepts|
11
+ accepts.html do
12
+ flash[:error] = 'What do you think you\'re doing?'
13
+ redirect_to '/'
14
+ end
15
+ accepts.xml do
16
+ headers["Status"] = "Unauthorized"
17
+ headers["WWW-Authenticate"] = %(Basic realm="Web Password")
18
+ render :text => "Couldn't authenticate you", :status => '401 Unauthorized'
19
+ end
20
+ end
21
+ return false
22
+ end
23
+ return true
24
+ end
25
+ end
@@ -0,0 +1,87 @@
1
+ class PostsController < ApplicationController
2
+ before_filter :ensure_logged_in
3
+ before_filter :load_user
4
+
5
+ def index
6
+ @posts = @user.posts
7
+
8
+ respond_to do |format|
9
+ format.html # index.rhtml
10
+ format.xml { render :xml => @posts.to_xml }
11
+ format.rss do
12
+ headers['Content-Type'] = 'application/rss+xml'
13
+ session[:special] = '$2 off your next purchase'
14
+ session[:special_user_id] = @user.id
15
+ session[:false_var] = false
16
+ head :ok
17
+ end
18
+ end
19
+ end
20
+
21
+ def show
22
+ @post = @user.posts.find(params[:id])
23
+ @false_flag = false
24
+
25
+ respond_to do |format|
26
+ format.html { render :layout => 'wide' }
27
+ format.xml { render :xml => @post.to_xml }
28
+ end
29
+ end
30
+
31
+ def new
32
+ @post = @user.posts.build
33
+ render :layout => false
34
+ end
35
+
36
+ def edit
37
+ @post = @user.posts.find(params[:id])
38
+ end
39
+
40
+ def create
41
+ @post = @user.posts.build(params[:post])
42
+
43
+ respond_to do |format|
44
+ if @post.save
45
+ flash[:notice] = 'Post was successfully created.'
46
+ format.html { redirect_to user_post_url(@post.user, @post) }
47
+ format.xml { head :created, :location => user_post_url(@post.user, @post) }
48
+ else
49
+ format.html { render :action => "new" }
50
+ format.xml { render :xml => @post.errors.to_xml }
51
+ end
52
+ end
53
+ end
54
+
55
+ def update
56
+ @post = @user.posts.find(params[:id])
57
+
58
+ respond_to do |format|
59
+ if @post.update_attributes(params[:post])
60
+ flash[:notice] = 'Post was successfully updated.'
61
+ format.html { redirect_to user_post_url(@post.user, @post) }
62
+ format.xml { head :ok }
63
+ else
64
+ format.html { render :action => "edit" }
65
+ format.xml { render :xml => @post.errors.to_xml }
66
+ end
67
+ end
68
+ end
69
+
70
+ def destroy
71
+ @post = @user.posts.find(params[:id])
72
+ @post.destroy
73
+
74
+ flash[:notice] = "Post was removed"
75
+
76
+ respond_to do |format|
77
+ format.html { redirect_to user_posts_url(@post.user) }
78
+ format.xml { head :ok }
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ def load_user
85
+ @user = User.find(params[:user_id])
86
+ end
87
+ end
@@ -0,0 +1,84 @@
1
+ class UsersController < ApplicationController
2
+
3
+ filter_parameter_logging :ssn
4
+
5
+ # GET /users
6
+ # GET /users.xml
7
+ def index
8
+ @users = User.find(:all)
9
+
10
+ respond_to do |format|
11
+ format.html # index.rhtml
12
+ format.xml { render :xml => @users.to_xml }
13
+ end
14
+ end
15
+
16
+ # GET /users/1
17
+ # GET /users/1.xml
18
+ def show
19
+ @user = User.find(params[:id])
20
+
21
+ respond_to do |format|
22
+ format.html # show.rhtml
23
+ format.xml { render :xml => @user.to_xml }
24
+ end
25
+ end
26
+
27
+ # GET /users/new
28
+ def new
29
+ @user = User.new
30
+ end
31
+
32
+ # GET /users/1;edit
33
+ def edit
34
+ @user = User.find(params[:id])
35
+ end
36
+
37
+ # POST /users
38
+ # POST /users.xml
39
+ def create
40
+ @user = User.new(params[:user])
41
+
42
+ respond_to do |format|
43
+ if @user.save
44
+ flash[:notice] = 'User was successfully created.'
45
+ format.html { redirect_to user_url(@user) }
46
+ format.xml { head :created, :location => user_url(@user) }
47
+ else
48
+ format.html { render :action => "new" }
49
+ format.xml { render :xml => @user.errors.to_xml }
50
+ end
51
+ end
52
+ end
53
+
54
+ # PUT /users/1
55
+ # PUT /users/1.xml
56
+ def update
57
+ @user = User.find(params[:id])
58
+
59
+ respond_to do |format|
60
+ if @user.update_attributes(params[:user])
61
+ flash[:notice] = 'User was successfully updated.'
62
+ format.html { redirect_to user_url(@user) }
63
+ format.xml { head :ok }
64
+ else
65
+ format.html { render :action => "edit" }
66
+ format.xml { render :xml => @user.errors.to_xml }
67
+ end
68
+ end
69
+ end
70
+
71
+ # DELETE /users/1
72
+ # DELETE /users/1.xml
73
+ def destroy
74
+ @user = User.find(params[:id])
75
+ @user.destroy
76
+
77
+ flash[:notice] = "User was removed"
78
+
79
+ respond_to do |format|
80
+ format.html { redirect_to users_url }
81
+ format.xml { head :ok }
82
+ end
83
+ end
84
+ end