ianwhite / response_for
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (7)
- Wiki (1)
- Graphs
-
Tree:
6a8d4cc
response_for / README.rdoc
| f0b98bf1 » | ian | 2007-10-18 | 1 | = response_for | |
| 2 | |||||
| 570f798d » | ian | 2007-10-23 | 3 | response_for (see Ardes::ResponseFor::ClassMethods) allows you to decorate the respond_to block of actions on sublcassed controllers. This works nicely with http://plugins.ardes.com/doc/resources_controller | |
| 4 | |||||
| 9dc94651 » | ianwhite | 2008-09-13 | 5 | == Current Version 0.2-stable | |
| 9eb9e429 » | ianwhite | 2008-04-30 | 6 | ||
| 9dc94651 » | ianwhite | 2008-09-13 | 7 | As of version 0.2.0, response_for's functionality can be summed up in one sentence: | |
| 8 | |||||
| 6a8d4ccf » | ianwhite | 2008-09-17 | 9 | <b>"response_for allows you to specify default responses for any action (or before filter) that doesn't render or redirect"</b> | |
| 37e2d2ed » | ianwhite | 2008-09-13 | 10 | ||
| 7700d081 » | ianwhite | 2008-09-17 | 11 | Actions typically do two things - interact with models, and render a response. The above simple idea allows you to decouple these | |
| 7ce71c62 » | ianwhite | 2008-09-13 | 12 | two functions (where appropriate), which means abstraction of common patterns becomes possible. | |
| 9eb9e429 » | ianwhite | 2008-04-30 | 13 | ||
| 7700d081 » | ianwhite | 2008-09-17 | 14 | NOTE: 0.2-stable has BC-breaking API changes, and is supported only for Rails >= 2.1.x. Version 0.2.0 was released on Sept 14th 2008. | |
| 15 | You should use 0.1-stable in your existing projects until you have runs your specs and whatnot. | ||||
| 16 | |||||
| 17 | If you want to know more about why I changed the API in 0.2 look at the bottom of this README | ||||
| 18 | |||||
| 570f798d » | ian | 2007-10-23 | 19 | === Example | |
| 20 | |||||
| 21 | class FooController < ApplicationController | ||||
| 22 | def index | ||||
| 23 | @foos = Foo.find(:all) | ||||
| 7ce71c62 » | ianwhite | 2008-09-13 | 24 | # default response - render html | |
| 570f798d » | ian | 2007-10-23 | 25 | end | |
| 26 | end | ||||
| 27 | |||||
| 7ce71c62 » | ianwhite | 2008-09-13 | 28 | # this controller needs to respond_to fbml on index. | |
| 29 | # Using response_for, we don't need to repeat '@foos = Foo.find(1)' | ||||
| 570f798d » | ian | 2007-10-23 | 30 | class SpecialFooController < FooController | |
| 31 | response_for :index do |format| | ||||
| 32 | format.fbml { render :inline => turn_into_facebook(@foos) } | ||||
| 33 | end | ||||
| 34 | end | ||||
| 41b2cc18 » | ianwhite | 2008-04-30 | 35 | ||
| f0b98bf1 » | ian | 2007-10-18 | 36 | === Specs and Coverage | |
| 37 | |||||
| 41b2cc18 » | ianwhite | 2008-04-30 | 38 | * The SPECDOC lists the specifications | |
| 0aeceda2 » | ianwhite | 2008-06-05 | 39 | * Coverage is 100% (C0), and the spec suite is quite comprehensive | |
| 41b2cc18 » | ianwhite | 2008-04-30 | 40 | ||
| f0b98bf1 » | ian | 2007-10-18 | 41 | RSpec is used for testing, so the tests are in <tt>spec/</tt> rather than | |
| 42 | <tt>test/</tt> Do rake --tasks for more details. | ||||
| 43 | |||||
| 41b2cc18 » | ianwhite | 2008-04-30 | 44 | === Continuous Integration | |
| 45 | |||||
| 46 | garlic (at http://github.com/ianwhite/garlic) is used for CI. To run the CI suite have a look at | ||||
| 47 | garlic_example.rb | ||||
| 7ce71c62 » | ianwhite | 2008-09-13 | 48 | ||
| 7700d081 » | ianwhite | 2008-09-17 | 49 | === Why change the API in 0.2? | |
| 50 | |||||
| 51 | repsonse_for <= v0.1 intercepted respond_to calls to allow overriding of these by class level declarations. This turns out to have some | ||||
| 52 | headaches, such as: | ||||
| 53 | |||||
| 54 | * If you have some bail-out code in before_filters which uses respond_to, then response_for tries to overwrite this. This meant that I had | ||||
| 55 | to write response_for to only kick in once before_filters had run. This made for some funky smelling code. | ||||
| 56 | * Sometimes your bail out code runs after the before_filters, in a superclass action for example, or just as part of your action (perhaps in | ||||
| 57 | another method). The above hack doesn't work for this case (the before_filters have run). The solution in this case was to use | ||||
| 58 | respond_to_without_response_for in any bail out code. | ||||
| 59 | * Conceptually, overriding code declared in methods, with code declared at the class level, is weird. Here's an example | ||||
| 60 | |||||
| 6a8d4ccf » | ianwhite | 2008-09-17 | 61 | class FooController < SuperclassController | |
| 62 | response_for :index # override Superclass's index respond_to | ||||
| 7700d081 » | ianwhite | 2008-09-17 | 63 | ||
| 6a8d4ccf » | ianwhite | 2008-09-17 | 64 | def index | |
| 65 | respond_to # one might expect this to override the above, as its declared later - but it wont! | ||||
| 66 | end | ||||
| 67 | end | ||||
| 68 | |||||
| 69 | So, in 0.2 a much simpler idea is behind response_for - you can declare a default response for an action which will be performed | ||||
| 70 | if <b>that that action has not already performed a render or redirect</b>. This means that all of your bail out code written with | ||||
| 71 | respond_to will do what it's supposed to. | ||||
| 72 | |||||
| 73 | ==== Rewriting for 0.2 | ||||
| 74 | |||||
| 75 | If you're upgrading, you just need to convert any actions you want to override from this: | ||||
| 76 | |||||
| 77 | def index | ||||
| 78 | @things = Thing.all | ||||
| 79 | respond_to do |format| | ||||
| 80 | format.html | ||||
| 81 | format.xml { render :xml => @things } | ||||
| 7700d081 » | ianwhite | 2008-09-17 | 82 | end | |
| 83 | end | ||||
| 6a8d4ccf » | ianwhite | 2008-09-17 | 84 | ||
| 85 | to this: | ||||
| 86 | |||||
| 87 | def index | ||||
| 88 | @things = Thing.all | ||||
| 89 | end | ||||
| 90 | |||||
| 91 | response_for :index fo |format| | ||||
| 92 | format.html | ||||
| 93 | format.xml { render :xml => @things } | ||||
| 94 | end | ||||
| 7700d081 » | ianwhite | 2008-09-17 | 95 | ||
| 7ce71c62 » | ianwhite | 2008-09-13 | 96 | == Previous Versions: 0.1 | |
| 97 | |||||
| 98 | There is a branch for rails 2.0 users on this release. If you are using rails 2.0, then you want the 0.1-stable-rails2.0 branch. If you are | ||||
| 99 | using rails >= 2.1 then use the 0.1-stable branch | ||||
| 100 | |||||

