How to Reload the wicked pdf.rb which is present in app-> config-> initializer-> wicked_pdf.rb file, without restarting the server in rails 2

StackOverflow https://stackoverflow.com/questions/20163893

Question

I have made changes in one of my plugin to customize the header and footer for pdf reports, so I am changing the contents of file wicked_pdf.rb to

if HeaderFooterDesign.first.config_key == true
  WickedPdf.config = {
    :layout => "pdf.html",
    :margin => {    :top=> 40,
      :bottom => 20,
      :left=> 30,
      :right => 30},
    :header => {:html => { :template=> "#{Rails.root}/vendor/plugins/globoschool_header_footer_designer/app/views/header_footer_designs/header.html.erb"}},
    :footer => {:html => { :template=> "#{Rails.root}/vendor/plugins/globoschool_header_footer_designer/app/views/header_footer_designs/footer.html.erb"}},
  }
else
  WickedPdf.config = {
    :layout => "pdf.html",
    :margin => {    :top=> 40,
      :bottom => 20,
      :left=> 30,
      :right => 30},
    :header => {:html => { :template=> 'layouts/pdf_header.html'}},
    :footer => {:html => { :template=> 'layouts/pdf_footer.html'}},
  }
end

Every time when the cofig_key value is changed I need to restart the server to get the effect, so how to go about this? I don't want to restart it every time, any help?

Était-ce utile?

La solution

I'm assuming you placed the above code in an initializer or environment file.

To solve your problem, place the the code you pasted above into a module method and call it to load the config. Whenever the config_key changes call that method again and the config will reload:

module WickedPdfConfig
  def self.load
    if HeaderFooterDesign.first.config_key == true
      WickedPdf.config = {
        :layout => "pdf.html",

    # ... rest of config code...
  end
end

# load the config the first time (on boot)
WickedPdfConfig.load

Now you have a method you can call whenever config_key changes. For example if you have an admin controller where you change your settings:

class AdminController < ApplicationController
  def update
    if params[:header_footer_design][:config_key] != HeaderFooterDesign.first.config_key
      HeaderFooterDesign.first.config_key = params[:header_footer_design][:config_key]
      WickedPdfConfig.load
    end
    # ... more code maybe ...
  end
end

Alternatively you can use Ruby's load method. With this approach you don't need to wrap your config code in a module method but rather you'd load the file again. After updating your HeaderFooterDesign config you'd call:

load "path/to/config/file.rb"

And the code within that file would execute again and those settings would take effect.

Hope that helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top