Sha256: 554efbe9cadec575168d4cf86f104720adb1ae2ba4fa2f19a67cb1399cfe889a

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

require 'action_controller'
unless defined? Mime::DOCX
  Mime::Type.register "application/vnd.openxmlformats-officedocument.wordprocessingml.document", :docx
end

ActionController::Renderers.add :docx do |filename, options|
  unless formats.include?(:docx) || Rails.version < '3.2'
    formats[0] = :docx
  end

  if options[:template] == action_name
    if filename =~ /^([^\/]+)\/(.+)$/
      options[:prefixes] ||= []
      options[:prefixes].unshift $1
      options[:template] = $2
    else
      options[:template] = filename
    end
  end

  # disposition / filename
  disposition   = options.delete(:disposition) || 'attachment'
  if file_name = options.delete(:filename)
    file_name += ".docx" unless file_name =~ /\.docx$/
  else
    file_name = "#{filename.gsub(/^.*\//,'')}.docx"
  end

  # other properties
  word_template = options.delete(:word_template) || nil
  # content will come from property content unless not specified
  # then it will look for a template.
  content = options.delete(:content) || render_to_string(options)

  doc = Htmltoword::Document.create content, file_name, word_template
  send_data File.read(doc.path), :filename => file_name, :type => Mime::DOCX, :disposition => disposition
end

# For respond_with default
begin
  ActionController::Responder
rescue LoadError
else
  class ActionController::Responder
    def to_docx
      if @default_response
        @default_response.call(options)
      else
        controller.render({:docx => controller.action_name}.merge(options))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
htmltoword-0.2.1 lib/htmltoword/action_controller.rb
htmltoword-0.2.0 lib/htmltoword/action_controller.rb