-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose Config object to Rails templates via #config method for templa…
…te-specific configuration
- Loading branch information
Showing
5 changed files
with
78 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |