Skip to content

Commit 3e211a5

Browse files
committed
added section about CSV Responder in README
1 parent 9a0cdcb commit 3e211a5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,39 @@ Which will generate the following SQL command:
116116
COPY users (id, name) FROM '/tmp/users.dat' WITH BINARY
117117

118118

119+
### Using the CSV Responder
120+
If you want to make the result of a COPY command available to download this gem provides a CSV responder that, in conjunction with [inherited_resources](https://github.com/josevalim/inherited_resources), is a very powerfull tool. BTW, do not try to use the responder without inherited_resources.
121+
122+
The advantage of using this CSV responder over generating the CSV file, is that it will iterate over the resulting lines and send them to the client as they became available, so you will not have any problem with memory consumption or timeouts executing the action.
123+
124+
Here's an example of controller using inherited resources and the CSV Responder:
125+
126+
```ruby
127+
class Clients < ApplicationController
128+
inherit_resources
129+
responders :csv
130+
respond_to :csv
131+
actions :index
132+
end
133+
```
134+
135+
The example above should be enough to generate a CSV file with clients (given that database and routes are set up accordingly) if you access the index action passing csv as the format.
136+
The [has_scope](https://github.com/plataformatec/has_scope) gem (included by default in inherited_resources) is very useful to filter the generated CSV file using model scopes.
137+
138+
You could use something such as:
139+
```ruby
140+
class Clients < ApplicationController
141+
inherit_resources
142+
responders :csv
143+
respond_to :csv
144+
actions :index
145+
has_scope :by_name
146+
end
147+
```
148+
149+
To filter clients by name using a corresponding model scope.
150+
One could argue that this responder should be in a separate gem. But it's a very small piece of code and make the CSV available for download is a very common use case of postgres-copy.
151+
119152
## Note on Patches/Pull Requests
120153

121154
* Fork the project.

0 commit comments

Comments
 (0)