Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle PdfHelper include vs prepend with separate modules #818

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 59 additions & 37 deletions lib/wicked_pdf/pdf_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,64 @@
class WickedPdf
module PdfHelper
module Includable
def self.included(base)
base.class_eval do
alias_method_chain :render, :wicked_pdf
alias_method_chain :render_to_string, :wicked_pdf
end
end

def render_with_wicked_pdf(options = nil, *args, &block)
if options.is_a?(Hash) && options.key?(:pdf)
options[:basic_auth] = set_basic_auth(options)
make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options))
else
# support alias_method_chain (module included)
render_without_wicked_pdf(options, *args, &block)
end
end

def render_to_string_with_wicked_pdf(options = nil, *args, &block)
if options.is_a?(Hash) && options.key?(:pdf)
options[:basic_auth] = set_basic_auth(options)
options.delete :pdf
make_pdf((WickedPdf.config || {}).merge(options))
else
# support alias_method_chain (module included)
render_to_string_without_wicked_pdf(options, *args, &block)
end
end
end

module Prependable
def render(options = nil, *args, &block)
if options.is_a?(Hash) && options.key?(:pdf)
options[:basic_auth] = set_basic_auth(options)
make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options))
else
super
end
end

def render_to_string(options = nil, *args, &block)
if options.is_a?(Hash) && options.key?(:pdf)
options[:basic_auth] = set_basic_auth(options)
options.delete :pdf
make_pdf((WickedPdf.config || {}).merge(options))
else
super
end
end
end

def self.included(base)
# Protect from trying to augment modules that appear
# as the result of adding other gems.
return if base != ActionController::Base

base.class_eval do
alias_method_chain :render, :wicked_pdf
alias_method_chain :render_to_string, :wicked_pdf
include Includable

if respond_to?(:after_action)
after_action :clean_temp_files
else
Expand All @@ -22,42 +73,13 @@ def self.prepended(base)
return if base != ActionController::Base

base.class_eval do
after_action :clean_temp_files
end
end
prepend Prependable

def render(options = nil, *args, &block)
render_with_wicked_pdf(options, *args, &block)
end

def render_to_string(options = nil, *args, &block)
render_to_string_with_wicked_pdf(options, *args, &block)
end

def render_with_wicked_pdf(options = nil, *args, &block)
if options.is_a?(Hash) && options.key?(:pdf)
options[:basic_auth] = set_basic_auth(options)
make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options))
elsif respond_to?(:render_without_wicked_pdf)
# support alias_method_chain (module included)
render_without_wicked_pdf(options, *args, &block)
else
# support inheritance (module prepended)
method(:render).super_method.call(options, *args, &block)
end
end

def render_to_string_with_wicked_pdf(options = nil, *args, &block)
if options.is_a?(Hash) && options.key?(:pdf)
options[:basic_auth] = set_basic_auth(options)
options.delete :pdf
make_pdf((WickedPdf.config || {}).merge(options))
elsif respond_to?(:render_to_string_without_wicked_pdf)
# support alias_method_chain (module included)
render_to_string_without_wicked_pdf(options, *args, &block)
else
# support inheritance (module prepended)
method(:render_to_string).super_method.call(options, *args, &block)
if respond_to?(:after_action)
after_action :clean_temp_files
else
after_filter :clean_temp_files
end
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/wicked_pdf/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class WickedPdf

class WickedRailtie < Rails::Railtie
initializer 'wicked_pdf.register' do |_app|
if ActionController::Base.respond_to?(:prepend) &&
Object.method(:new).respond_to?(:super_method)
if ActionController::Base.respond_to?(:prepend)
ActionController::Base.send :prepend, PdfHelper
else
ActionController::Base.send :include, PdfHelper
Expand Down