Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplified DSL context handling Version bump to 1.0.1
- Loading branch information
Showing
13 changed files
with
115 additions
and
104 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
This file was deleted.
Oops, something went wrong.
56 changes: 24 additions & 32 deletions
56
lib/to_spreadsheet/action_pack_renderers.rb → ...preadsheet/rails/action_pack_renderers.rb
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 |
---|---|---|
@@ -1,32 +1,24 @@ | ||
require 'active_support' | ||
require 'action_controller/metal/renderers' | ||
require 'action_controller/metal/responder' | ||
|
||
require 'to_spreadsheet/renderer' | ||
|
||
# This will let us do thing like `render :xlsx => 'index'` | ||
# This is similar to how Rails internally implements its :json and :xml renderers | ||
ActionController::Renderers.add :xlsx do |template, options| | ||
filename = options[:filename] || options[:template] || 'data' | ||
|
||
html = with_context ToSpreadsheet::Context.global.merge(ToSpreadsheet::Context.new) do | ||
# local context | ||
@local_formats.each do |selector, &block| | ||
context.process_dsl selector, &block | ||
end if @local_formats | ||
render_to_string(options[:template], options) | ||
end | ||
|
||
data = ToSpreadsheet::Axlsx::Renderer.to_data(html) | ||
send_data data, type: :xlsx, disposition: %(attachment; filename="#{filename}.xlsx") | ||
end | ||
|
||
class ActionController::Responder | ||
# This sets up a default render call for when you do | ||
# respond_to do |format| | ||
# format.xlsx | ||
# end | ||
def to_xlsx | ||
controller.render xlsx: controller.action_name | ||
end | ||
end | ||
require 'active_support' | ||
require 'action_controller/metal/renderers' | ||
require 'action_controller/metal/responder' | ||
|
||
# This will let us do thing like `render :xlsx => 'index'` | ||
# This is similar to how Rails internally implements its :json and :xml renderers | ||
ActionController::Renderers.add :xlsx do |template, options| | ||
filename = options[:filename] || options[:template] || 'data' | ||
data = ToSpreadsheet::Context.with_context ToSpreadsheet::Context.global.merge(ToSpreadsheet::Context.new) do |context| | ||
html = render_to_string(template, options.merge(template: template, formats: ['html'])) | ||
ToSpreadsheet::Renderer.to_data(html, context) | ||
end | ||
send_data data, type: :xlsx, disposition: %(attachment; filename="#{filename}.xlsx") | ||
end | ||
|
||
class ActionController::Responder | ||
# This sets up a default render call for when you do | ||
# respond_to do |format| | ||
# format.xlsx | ||
# end | ||
def to_xlsx | ||
controller.render xlsx: controller.action_name | ||
end | ||
end |
4 changes: 2 additions & 2 deletions
4
lib/to_spreadsheet/mime_types.rb → lib/to_spreadsheet/rails/mime_types.rb
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
require 'action_dispatch/http/mime_type' | ||
Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx unless Mime::Type.lookup_by_extension(:xlsx) | ||
require 'action_dispatch/http/mime_type' | ||
Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx unless Mime::Type.lookup_by_extension(:xlsx) |
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,11 @@ | ||
module ToSpreadsheet | ||
module Rails | ||
module ViewHelpers | ||
def format_xls(selector = nil, &block) | ||
ctx = ToSpreadsheet::Context.current | ||
return unless ctx | ||
ctx.format_xls selector, &block | ||
end | ||
end | ||
end | ||
end |
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,11 @@ | ||
require 'rails/railtie' | ||
require 'to_spreadsheet/rails/mime_types' | ||
require 'to_spreadsheet/rails/action_pack_renderers' | ||
require 'to_spreadsheet/rails/view_helpers' | ||
module ToSpreadsheet | ||
class Railtie < ::Rails::Railtie | ||
initializer "to_spreadsheet.view_helpers" do | ||
ActionView::Base.send :include, ToSpreadsheet::Rails::ViewHelpers | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module ToSpreadsheet | ||
VERSION = '1.0.0' | ||
end | ||
module ToSpreadsheet | ||
VERSION = '1.0.1' | ||
end |
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