Sha256: 6088432d6996ef69ebd9267b2d3c8f02fbb77414752d2ec7c24061f37cc07d04

Contents?: true

Size: 1.28 KB

Versions: 9

Compression:

Stored size: 1.28 KB

Contents

module Htmltoword
  module HtmltowordHelper

    def template_file template_file_name=nil
      default_path = File.join(::Htmltoword.templates_path, "default.docx")
      template_path = template_file_name.present? ? File.join(::Htmltoword.templates_path, template_file_name) : ""
      File.exist?(template_path) ? template_path : default_path
    end

    def replace_values content, set
      doc = Nokogiri::HTML(content)
      set.each_pair do |key, value|
        fields = (doc/"//span[@data-id='#{key}']")
        fields.each do |f|
          date_format = f.attr("date-format") || "long"
          data_transform = f.attr("data-transform")
          if value.is_a? Hash
            view = ActionView::Base.new(ActionController::Base.view_paths, {})
            final_value = view.render "partials/answer_table", answer: value
            fragment = doc.root.parse(final_value).first
            new_node = doc.root.add_child(fragment)
            f.parent.replace new_node
          elsif value.is_a? Time
            f.content = I18n.l(value.to_date, format: date_format.to_sym)
          elsif data_transform == "capitalized"
            f.content = value.mb_chars.capitalize rescue value
          else
            f.content = value
          end
        end
      end
      doc.to_s
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
htmltoword-0.1.8 lib/htmltoword/htmltoword_helper.rb
htmltoword-0.1.5 lib/htmltoword/htmltoword_helper.rb
htmltoword-0.1.4 lib/htmltoword/htmltoword_helper.rb
htmltoword-0.1.3 lib/htmltoword/htmltoword_helper.rb
htmltoword-0.1.2 lib/htmltoword/htmltoword_helper.rb
htmltoword-0.1.1 lib/htmltoword/htmltoword_helper.rb
htmltoword-0.1.0 lib/htmltoword/htmltoword_helper.rb
htmltoword-0.0.2 lib/htmltoword/htmltoword_helper.rb
htmltoword-0.0.1 lib/htmltoword/htmltoword_helper.rb