public
Description: response for lets you decorate your actions respond_to blocks
Homepage: http://blog.ardes.com/response_for
Clone URL: git://github.com/ianwhite/response_for.git
Click here to lend your support to: response_for and make a donation at www.pledgie.com !
response_for / README.rdoc
f0b98bf1 » ian 2007-10-18 Initial import of response_for 1 = response_for
2
570f798d » ian 2007-10-23 response_for: initial release 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 Final notes and docfixes fo... 5 == Current Version 0.2-stable
9eb9e429 » ianwhite 2008-04-30 Added note about 2.0-stable... 6
9dc94651 » ianwhite 2008-09-13 Final notes and docfixes fo... 7 As of version 0.2.0, response_for's functionality can be summed up in one sentence:
8
6a8d4ccf » ianwhite 2008-09-17 Updated README with more no... 9 "response_for allows you to specify default responses for any action (or before filter) that doesn't render or redirect"
37e2d2ed » ianwhite 2008-09-13 Added notes about 0.1 vs 0.... 10
7700d081 » ianwhite 2008-09-17 Updated README with more no... 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 Updated README 12 two functions (where appropriate), which means abstraction of common patterns becomes possible.
9eb9e429 » ianwhite 2008-04-30 Added note about 2.0-stable... 13
7700d081 » ianwhite 2008-09-17 Updated README with more no... 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
db80cbb0 » ianwhite 2008-09-22 Updated README 19 == Tested for Rails 2.1 and edge
20
21 response_for works with 2.1.x and edge. For previous rails versions checkout the 0.1-stable-rails2.0 branch
22
570f798d » ian 2007-10-23 response_for: initial release 23 === Example
24
25 class FooController < ApplicationController
26 def index
27 @foos = Foo.find(:all)
7ce71c62 » ianwhite 2008-09-13 Updated README 28 # default response - render html
570f798d » ian 2007-10-23 response_for: initial release 29 end
30 end
31
7ce71c62 » ianwhite 2008-09-13 Updated README 32 # this controller needs to respond_to fbml on index.
33 # Using response_for, we don't need to repeat '@foos = Foo.find(1)'
570f798d » ian 2007-10-23 response_for: initial release 34 class SpecialFooController < FooController
35 response_for :index do |format|
36 format.fbml { render :inline => turn_into_facebook(@foos) }
37 end
38 end
41b2cc18 » ianwhite 2008-04-30 Updated README, and various... 39
f0b98bf1 » ian 2007-10-18 Initial import of response_for 40 === Specs and Coverage
41
41b2cc18 » ianwhite 2008-04-30 Updated README, and various... 42 * The SPECDOC lists the specifications
0aeceda2 » ianwhite 2008-06-05 Fixed typo in README 43 * Coverage is 100% (C0), and the spec suite is quite comprehensive
41b2cc18 » ianwhite 2008-04-30 Updated README, and various... 44
f0b98bf1 » ian 2007-10-18 Initial import of response_for 45 RSpec is used for testing, so the tests are in spec/ rather than
46 test/ Do rake --tasks for more details.
47
41b2cc18 » ianwhite 2008-04-30 Updated README, and various... 48 === Continuous Integration
49
50 garlic (at http://github.com/ianwhite/garlic) is used for CI. To run the CI suite have a look at
51 garlic_example.rb
7ce71c62 » ianwhite 2008-09-13 Updated README 52
7700d081 » ianwhite 2008-09-17 Updated README with more no... 53 === Why change the API in 0.2?
54
55 repsonse_for <= v0.1 intercepted respond_to calls to allow overriding of these by class level declarations. This turns out to have some
56 headaches, such as:
57
58 * 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
59 to write response_for to only kick in once before_filters had run. This made for some funky smelling code.
60 * 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
61 another method). The above hack doesn't work for this case (the before_filters have run). The solution in this case was to use
62 respond_to_without_response_for in any bail out code.
63 * Conceptually, overriding code declared in methods, with code declared at the class level, is weird. Here's an example
64
6a8d4ccf » ianwhite 2008-09-17 Updated README with more no... 65 class FooController < SuperclassController
66 response_for :index # override Superclass's index respond_to
7700d081 » ianwhite 2008-09-17 Updated README with more no... 67
6a8d4ccf » ianwhite 2008-09-17 Updated README with more no... 68 def index
69 respond_to # one might expect this to override the above, as its declared later - but it wont!
70 end
71 end
72
73 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
74 if that that action has not already performed a render or redirect. This means that all of your bail out code written with
75 respond_to will do what it's supposed to.
76
77 ==== Rewriting for 0.2
78
79 If you're upgrading, you just need to convert any actions you want to override from this:
80
81 def index
82 @things = Thing.all
83 respond_to do |format|
84 format.html
85 format.xml { render :xml => @things }
7700d081 » ianwhite 2008-09-17 Updated README with more no... 86 end
87 end
6a8d4ccf » ianwhite 2008-09-17 Updated README with more no... 88
89 to this:
90
91 def index
92 @things = Thing.all
93 end
94
95 response_for :index fo |format|
96 format.html
97 format.xml { render :xml => @things }
98 end
7700d081 » ianwhite 2008-09-17 Updated README with more no... 99
7ce71c62 » ianwhite 2008-09-13 Updated README 100 == Previous Versions: 0.1
101
102 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
103 using rails >= 2.1 then use the 0.1-stable branch
104