Sha256: f5b3ca3927c98b3ba4db187b931aa13dcab1c1670224ba202a1dccd8cd9617ab

Contents?: true

Size: 1.88 KB

Versions: 10

Compression:

Stored size: 1.88 KB

Contents

require 'rails-footnotes/notes/log_note'

module Footnotes
  module Notes
    class PartialsNote < LogNote
      def initialize(controller)
        super
        @controller = controller
      end
      def row
        :edit
      end
      def title
        "Partials (#{partials.size})"
      end
      def content
        rows = partials.map do |filename|
          href = Footnotes::Filter.prefix(filename,1,1)
          shortened_name=filename.gsub(File.join(Rails.root,"app/views/"),"")
          [%{<a href="#{href}">#{shortened_name}</a>},"#{@partial_times[filename].sum}ms",@partial_counts[filename]]
        end
        mount_table(rows.unshift(%w(Partial Time Count)), :summary => "Partials for #{title}")
      end

      protected
        #Generate a list of partials that were rendered, also build up render times and counts.
        #This is memoized so we can use its information in the title easily.
        def partials
          @partials ||= begin
            partials = []
            @partial_counts = {}
            @partial_times = {}
            log_lines = log
            log_lines.split("\n").each do |line|
              if line =~ /Rendered (\S*) \(([\d\.]+)\S*?\)/
                partial = $1
                @controller.view_paths.each do |view_path|
                  path = File.join(view_path.to_s, "#{partial}*")
                  files = Dir.glob(path)
                  for file in files
                    #TODO figure out what format got rendered if theres multiple
                    @partial_times[file] ||= []
                    @partial_times[file] << $2.to_f
                    @partial_counts[file] ||= 0
                    @partial_counts[file] += 1
                    partials << file unless partials.include?(file)
                  end
                end
              end
            end
            partials.reverse
          end
        end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rails-footnotes-3.7.9 lib/rails-footnotes/notes/partials_note.rb
rails-footnotes-3.7.8 lib/rails-footnotes/notes/partials_note.rb
rails-footnotes-3.7.7 lib/rails-footnotes/notes/partials_note.rb
rails-footnotes-3.7.6 lib/rails-footnotes/notes/partials_note.rb
rails-footnotes-3.7.5 lib/rails-footnotes/notes/partials_note.rb
rails-footnotes-3.7.5.rc4 lib/rails-footnotes/notes/partials_note.rb
rails-footnotes-3.7.5.rc3 lib/rails-footnotes/notes/partials_note.rb
rails-footnotes-3.7.5.rc2 lib/rails-footnotes/notes/partials_note.rb
rails-footnotes-3.7.5.rc1 lib/rails-footnotes/notes/partials_note.rb
rails-footnotes-3.7.4 lib/rails-footnotes/notes/partials_note.rb