Skip to content

Commit

Permalink
Expose Config object to Rails templates via #config method for templa…
Browse files Browse the repository at this point in the history
…te-specific configuration
  • Loading branch information
jimryan committed May 23, 2016
1 parent 4ce7fdd commit fe9a5f8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 25 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ CsvShaper.encode(col_sep: "\t") do |csv|
end
```

To configure Rails template-specific CSV output, use the `config` method on the `csv` object:

```ruby
csv.config.col_sep = "\t"
```

### Contributing

1. Fork it
Expand Down
2 changes: 1 addition & 1 deletion lib/csv_shaper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def self.configure(&block)
end
end

require "csv_shaper_template" if defined?(ActionView::Template)
require "csv_shaper_handler" if defined?(ActionView::Template)
27 changes: 27 additions & 0 deletions lib/csv_shaper_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'csv_shaper_template'

# CsvShaperHandler
# Template handler for Rails
class CsvShaperHandler
cattr_accessor :default_format
self.default_format = Mime[:csv]

# Expected `call` class method
# Set response headers with filename
# Primarily calls CsvShaperTemplate.encode, passing through the context (self)
def self.call(template)
%{
if ( controller.present? ) && !( defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) )
@filename ||= "\#{controller.action_name}.csv"
controller.response.headers["Content-Type"] ||= 'text/csv'
controller.response.headers['Content-Disposition'] = "attachment; filename=\\\"\#{@filename}\\\""
end
CsvShaperTemplate.encode(self) do |csv|
#{template.source}
end
}
end
end

ActionView::Template.register_template_handler :shaper, CsvShaperHandler
26 changes: 2 additions & 24 deletions lib/csv_shaper_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,8 @@ def initialize(context)
@context = context
super()
end
end

# CsvShaperHandler
# Template handler for Rails
class CsvShaperHandler
cattr_accessor :default_format
self.default_format = Mime[:csv]

# Expected `call` class method
# Set response headers with filename
# Primarily calls CsvShaperTemplate.encode, passing through the context (self)
def self.call(template)
%{
if ( controller.present? ) && !( defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) )
@filename ||= "\#{controller.action_name}.csv"
controller.response.headers["Content-Type"] ||= 'text/csv'
controller.response.headers['Content-Disposition'] = "attachment; filename=\\\"\#{@filename}\\\""
end
CsvShaperTemplate.encode(self) do |csv|
#{template.source}
end
}
def config
@local_config
end
end

ActionView::Template.register_template_handler :shaper, CsvShaperHandler
42 changes: 42 additions & 0 deletions spec/csv_shaper_template_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'
require "csv_shaper_template"

describe CsvShaperTemplate do
it "should allow configuration via #config method" do
csv_string = CsvShaperTemplate.encode(double('context')) do |csv|
csv.config.write_headers = false
csv.config.col_sep = ';'

csv.headers :name, :age, :gender

csv.row do |csv|
csv.cell :name, 'Paul'
csv.cell :age, '27'
csv.cell :gender, 'Male'
end
end

expect(csv_string).to eq "Paul;27;Male\n"
end

it "should override global configuration with local configuration" do
CsvShaper::Shaper.config = CsvShaper::Config.new do |c|
c.write_headers = false
c.col_sep = "\t"
end

csv_string = CsvShaperTemplate.encode(double('context')) do |csv|
csv.config.col_sep = ','

csv.headers :name, :age, :gender

csv.row do |csv|
csv.cell :name, 'Paul'
csv.cell :age, '27'
csv.cell :gender, 'Male'
end
end

expect(csv_string).to eq "Paul,27,Male\n"
end
end

0 comments on commit fe9a5f8

Please sign in to comment.