Sha256: 6a0e672808cd46355a6a26cb8ee4f510d8d5a4df29726184410d38118b3c74e2

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

module Footnotes
  module Notes
    class FilesNote < AbstractNote
      def initialize(controller)
        @files = scan_text(controller.response.body)
        parse_files!
      end

      def row
        :edit
      end

      def content
        if @files.empty?
          ""
        else
          "<ul><li>%s</li></ul>" % @files.join("</li><li>")
        end
      end

      def valid?
        prefix?
      end

      protected
        def scan_text(text)
          raise NotImplementedError, "implement this in your subclass"
        end

        def parse_files!
          asset_paths = Rails.application.config.try(:assets).try(:paths) || []
          linked_files = []

          @files.collect do |file|
            file.gsub!(/-[a-f0-9]{64}\./, '.')
            base_name = File.basename(file)
            asset_paths.each do |asset_path|
              results = Dir[File.expand_path(base_name, asset_path) + '*']
              results.each do |r|
                linked_files << %[<a href="#{Footnotes::Filter.prefix(r, 1, 1)}">#{File.basename(r)}</a>]
              end
              break if results.present?
            end
          end
          @files = linked_files
        end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
rails-footnotes-7.1.0 lib/rails-footnotes/notes/files_note.rb
rails-footnotes-7.0.1 lib/rails-footnotes/notes/files_note.rb
rails-footnotes-7.0.0 lib/rails-footnotes/notes/files_note.rb
rails-footnotes-5.0.0 lib/rails-footnotes/notes/files_note.rb
rails6-footnotes-5.0.2 lib/rails-footnotes/notes/files_note.rb
rails6-footnotes-5.0.1 lib/rails-footnotes/notes/files_note.rb
rails6-footnotes-5.0.0 lib/rails-footnotes/notes/files_note.rb