lib/htmltoword/action_controller.rb in htmltoword-0.2.1 vs lib/htmltoword/action_controller.rb in htmltoword-0.4.0

- old
+ new

@@ -1,53 +1,54 @@ +require 'rack/test' require 'action_controller' + unless defined? Mime::DOCX - Mime::Type.register "application/vnd.openxmlformats-officedocument.wordprocessingml.document", :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 + formats[0] = :docx unless formats.include?(:docx) || Rails.version < '3.2' + # This is ugly and should be solved with regular file utils if options[:template] == action_name - if filename =~ /^([^\/]+)\/(.+)$/ + if filename =~ %r{^([^\/]+)/(.+)$} options[:prefixes] ||= [] options[:prefixes].unshift $1 options[:template] = $2 else options[:template] = filename end end # disposition / filename - disposition = options.delete(:disposition) || 'attachment' + disposition = options.delete(:disposition) || 'attachment' if file_name = options.delete(:filename) - file_name += ".docx" unless file_name =~ /\.docx$/ + file_name += '.docx' unless file_name =~ /\.docx$/ else - file_name = "#{filename.gsub(/^.*\//,'')}.docx" + file_name = "#{filename.gsub(/^.*\//, '')}.docx" end # other properties word_template = options.delete(:word_template) || nil + extras = options.delete(:extras) || false # 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 + document = Htmltoword::Document.create(content, word_template, extras) + send_data document, 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)) +if defined? ActionController::Responder + module ActionController + # For respond_with default + class Responder + def to_docx + if @default_response + @default_response.call(options) + else + controller.render({ docx: controller.action_name }.merge(options)) + end end end end end