Permalink
Browse files

Add pseudo code for FormResource

  • Loading branch information...
1 parent 468eb27 commit f442581de14c5ee89bbac1e8c2e4216543d1e4bb @lgierth committed Mar 13, 2012
Showing with 52 additions and 0 deletions.
  1. +52 −0 wrocloverb2012-forms-are-resources/3-form-resource/form-resource.rb
@@ -0,0 +1,52 @@
+class FormResource < Webmachine::Resource
+ def initialize
+ @resource = resolve("/" + request.path_tokens.join("/"))
+
+ if @resource && input
+ @form = @resource.to_form
+ @form.apply(input)
+ end
+ end
+
+ def resource_exists?
+ @resource &&
+ @resource.respond_to?(:to_form) &&
+ @resource.resource_exists?
+ end
+
+ def allowed_methods
+ ["GET", "HEAD", "POST"]
+ end
+
+ def malformed_request?
+ input == false
+ end
+
+ def process_post
+ if form.valid?
+ form.process
+ 200
+ else
+ 422
+ end
+ end
+
+ def to_html
+ FormRepresenter.new(form).to_html
+ end
+
+ def content_types_provided
+ [ ["text/html", :to_html] ]
+ end
+
+ def finish_request
+ if response.code == 200
+ form.succeed
+ else
+ response.headers["Content-Type"] = "text/html"
+ response.body = to_html
+ form.fail
+ end
+ end
+end
+

0 comments on commit f442581

Please sign in to comment.