rendered_csv 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.sublime*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rendered_csv.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David van Geest
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # RenderedCsv
2
+
3
+ RenderedCsv provides a `render_csv` method to your Rails 3.0+ controllers.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rendered_csv'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rendered_csv
18
+
19
+ ## Usage
20
+
21
+ In your controllers, simply call `render_csv` instead of `render`. This will setup the appropriate headers and render your CSV file.
22
+
23
+ `render_csv` has two optional parameters, `filename` and `template`. If not specified, `filename` defaults to the name of the current action.
24
+
25
+ This example would respond with a file named `widgets.csv`, rendering the `index` template in the process:
26
+
27
+ class WidgetsController < ApplicationController
28
+ def index
29
+ @widgets = Widget.all
30
+ respond_to do |format|
31
+ format.csv do
32
+ render_csv('widgets')
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ You could render a different template: `render_csv('widgets', 'other_index')`
39
+
40
+ Or you could just use the defaults to get `index.csv` from the `index` template: `render_csv`
41
+
42
+ ## Acknowledgements
43
+
44
+ The meat of this gem was taken from [Clinton R. Nixon's](http://stackoverflow.com/users/6262/clinton-r-nixon) answer to this [StackOverflow question](http://stackoverflow.com/questions/94502/in-rails-how-to-return-records-as-a-csv-file). I just wanted to stop copy-pasting the code from project to project.
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:spec) do |t|
6
+ t.libs << 'lib'
7
+ t.libs << 'spec'
8
+ t.pattern = 'spec/**/*_spec.rb'
9
+ t.verbose = false
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ require "rendered_csv/version"
2
+ require "rendered_csv/controller"
3
+ require 'rendered_csv/railtie' if defined?(Rails)
@@ -0,0 +1,30 @@
1
+ require 'active_support'
2
+
3
+ module RenderedCsv
4
+ module Controller
5
+ extend ActiveSupport::Concern
6
+
7
+ def render_csv(filename = nil, template = nil)
8
+ filename ||= params[:action]
9
+ filename += '.csv'
10
+
11
+ if request.env['HTTP_USER_AGENT'] =~ /msie/i
12
+ headers['Pragma'] = 'public'
13
+ headers["Content-Type"] = "text/plain"
14
+ headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
15
+ headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
16
+ headers['Expires'] = "0"
17
+ else
18
+ headers["Content-Type"] ||= 'text/csv'
19
+ headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
20
+ end
21
+
22
+ if template.nil?
23
+ render :layout => false
24
+ else
25
+ render template, :layout => false
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ module RenderedCsv
2
+ class Railtie < Rails::Railtie
3
+ initializer "rendered_csv.action_controller" do
4
+ ActiveSupport.on_load(:action_controller) do
5
+ include RenderedCsv::Controller # ActiveSupport::Concern
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module RenderedCsv
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rendered_csv/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rendered_csv"
8
+ gem.version = RenderedCsv::VERSION
9
+ gem.authors = ["SpinDance, Inc."]
10
+ gem.email = ["rubygems@spindance.com"]
11
+ gem.description = %q{Adds a render_csv to your Rails 3.0+ controllers}
12
+ gem.summary = %q{Takes care of setting headers, naming your CSV file, and rendering a desired template}
13
+ gem.homepage = "https://github.com/spindance/rendered_csv"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'rails', '>= 3.0.0'
21
+ gem.add_development_dependency 'mocha', '~> 0.13.1'
22
+ end
@@ -0,0 +1,138 @@
1
+ require 'spec_helper'
2
+ require 'rendered_csv/controller'
3
+
4
+ describe RenderedCsv::Controller do
5
+ before do
6
+ @controller = Object.new
7
+ @controller.extend(RenderedCsv::Controller)
8
+ end
9
+
10
+ describe 'when asked to render a CSV file' do
11
+ before do
12
+ @request = mock()
13
+ @env = mock()
14
+ @request.stubs(:env).returns(@env)
15
+ @env.stubs('[]')
16
+ @controller.stubs(:request).returns(@request)
17
+
18
+ @headers = mock()
19
+ @headers.stubs('[]')
20
+ @headers.stubs('[]=')
21
+ @controller.stubs(:headers).returns(@headers)
22
+ end
23
+ describe 'with a template' do
24
+ it 'should render the correct template' do
25
+ @controller.expects(:render).with('index', {:layout => false})
26
+ @controller.render_csv('widgets', 'index')
27
+ end
28
+ end
29
+ describe 'without a template' do
30
+ it 'should render' do
31
+ @controller.expects(:render).with({:layout => false})
32
+ @controller.render_csv('widgets')
33
+ end
34
+ end
35
+ end
36
+
37
+ describe 'when asked to render a CSV file' do
38
+ before do
39
+ @request = mock()
40
+ @env = mock()
41
+ @request.stubs(:env).returns(@env)
42
+ @env.stubs('[]')
43
+ @controller.stubs(:request).returns(@request)
44
+
45
+ @headers = mock()
46
+ @headers.stubs('[]')
47
+ @headers.stubs('[]=')
48
+ @controller.stubs(:headers).returns(@headers)
49
+
50
+ @controller.stubs(:render)
51
+ end
52
+ describe 'with a filename' do
53
+ it 'should set the filename in the header' do
54
+ @headers.expects('[]=').with("Content-Disposition", "attachment; filename=\"widgets.csv\"")
55
+ @controller.render_csv('widgets')
56
+ end
57
+ end
58
+ describe 'without a filename' do
59
+ it 'should set the filename in the header' do
60
+ @controller.expects(:params).returns({:action => 'index'})
61
+ @headers.expects('[]=').with("Content-Disposition", "attachment; filename=\"index.csv\"")
62
+ @controller.render_csv
63
+ end
64
+ end
65
+ end
66
+
67
+ describe 'when asked to render a CSV file' do
68
+ before do
69
+ @controller.stubs(:params).returns({:action => 'index'})
70
+
71
+ @request = mock()
72
+ @env = mock()
73
+ @request.stubs(:env).returns(@env)
74
+ @env.stubs('[]')
75
+ @controller.stubs(:request).returns(@request)
76
+
77
+ @headers = mock()
78
+ @headers.stubs('[]')
79
+ @headers.stubs('[]=').with("Content-Disposition", "attachment; filename=\"index.csv\"")
80
+ @controller.stubs(:headers).returns(@headers)
81
+
82
+ @controller.stubs(:render)
83
+ end
84
+ describe 'with Content-Type already set' do
85
+ it 'should not set the Content-Type in the header' do
86
+ @headers.expects('[]').with("Content-Type").returns('application/csv')
87
+ @controller.render_csv
88
+ end
89
+ end
90
+ describe 'with Content-Type not set' do
91
+ it 'should set the Content-Type in the header' do
92
+ @headers.expects('[]=').with("Content-Type", 'text/csv')
93
+ @controller.render_csv
94
+ end
95
+ end
96
+ end
97
+
98
+ describe 'when asked to render a CSV file' do
99
+ before do
100
+ @controller.stubs(:params).returns({:action => 'index'})
101
+
102
+ @request = mock()
103
+ @env = mock()
104
+ @request.stubs(:env).returns(@env)
105
+ @controller.stubs(:request).returns(@request)
106
+
107
+ @headers = mock()
108
+ @controller.stubs(:headers).returns(@headers)
109
+
110
+ @controller.stubs(:render)
111
+ end
112
+ describe 'for IE browser' do
113
+ before do
114
+ @env.stubs('[]').with('HTTP_USER_AGENT').returns('msie')
115
+ end
116
+ it 'should set the correct headers' do
117
+ @headers.expects('[]=').with("Pragma", 'public')
118
+ @headers.expects('[]=').with("Content-Type", "text/plain")
119
+ @headers.expects('[]=').with("Cache-Control", 'no-cache, must-revalidate, post-check=0, pre-check=0')
120
+ @headers.expects('[]=').with("Content-Disposition", "attachment; filename=\"index.csv\"")
121
+ @headers.expects('[]=').with("Expires", '0')
122
+ @controller.render_csv
123
+ end
124
+ end
125
+ describe 'for non-IE browser' do
126
+ before do
127
+ @env.stubs('[]').with('HTTP_USER_AGENT').returns('mozilla')
128
+ @headers.stubs('[]').returns(nil)
129
+ end
130
+ it 'should set the correct headers' do
131
+ @headers.expects('[]=').with("Content-Type", "text/csv")
132
+ @headers.expects('[]=').with("Content-Disposition", "attachment; filename=\"index.csv\"")
133
+ @controller.render_csv
134
+ end
135
+ end
136
+ end
137
+
138
+ end
@@ -0,0 +1,2 @@
1
+ require 'minitest/autorun'
2
+ require "mocha/setup"
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rendered_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - SpinDance, Inc.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.13.1
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.13.1
46
+ description: Adds a render_csv to your Rails 3.0+ controllers
47
+ email:
48
+ - rubygems@spindance.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/rendered_csv.rb
59
+ - lib/rendered_csv/controller.rb
60
+ - lib/rendered_csv/railtie.rb
61
+ - lib/rendered_csv/version.rb
62
+ - rendered_csv.gemspec
63
+ - spec/lib/rendered_csv/controller_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: https://github.com/spindance/rendered_csv
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.24
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Takes care of setting headers, naming your CSV file, and rendering a desired
89
+ template
90
+ test_files:
91
+ - spec/lib/rendered_csv/controller_spec.rb
92
+ - spec/spec_helper.rb