Sha256: 0af07bd4f97b12e2f82cf6eea48bd944fa4079d9df220f7b1e7aa9be704f3e1e

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

module Htmltoword
  class Renderer
    class << self
      def send_file(context, filename, options = {})
        new(context, filename, options).send_file
      end
    end

    def initialize(context, filename, options)
      @word_template = options[:word_template].presence
      @disposition = options.fetch(:disposition, 'attachment')
      @use_extras = options.fetch(:extras, false)
      @file_name = file_name(filename, options)
      @context = context
      define_template(filename, options)
      @content = options[:content] || @context.render_to_string(options)
    end

    def send_file
      document = Htmltoword::Document.create(@content, @word_template, @use_extras)
      @context.send_data(document, filename: @file_name, type: Mime[:docx], disposition: @disposition)
    end

    private

    def define_template(filename, options)
      if options[:template] == @context.action_name
        if filename =~ %r{^([^\/]+)/(.+)$}
          options[:prefixes] ||= []
          options[:prefixes].unshift $1
          options[:template] = $2
        else
          options[:template] = filename
        end
      end
    end

    def file_name(filename, options)
      name = options[:filename].presence || filename
      name =~ /\.docx$/ ? name : "#{name}.docx"
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
htmltoword-1.1.1 lib/htmltoword/renderer.rb
janie-htmltoword-1.1.2 lib/htmltoword/renderer.rb
htmltoword-1.1.0 lib/htmltoword/renderer.rb
htmltoword-1.0.0 lib/htmltoword/renderer.rb
htmltoword-0.7.0 lib/htmltoword/renderer.rb