Sha256: 4861a5b6915d0c8d124b0923d1816075580ee3c8763bb19b4cece4fed7f02c5b

Contents?: true

Size: 986 Bytes

Versions: 2

Compression:

Stored size: 986 Bytes

Contents

# Class that can subscribe to and log any view events
module Footnotes
  class ViewSubscriber < ActiveSupport::LogSubscriber
    attr_accessor :events
    def initialize
      @events = Array.new
      super
    end

    def render_template(event)
      @events << event.dup
    end
    alias :render_partial :render_template
    alias :render_collection :render_template

    def partials
      events.select{ |e| e.name =~ /render_partial/ }
    end

    def partial_time
      partials.map{|p| p.duration }.sum
    end

    def page
      events.find{ |e| e.name =~ /render_template/ }
    end

    def view
      page && page.payload[:identifier]
    end

    def view_name
      view && view.gsub(Rails.root.join("app/views/"),'')
    end

    def layout
      page && Rails.root.join("app/views", page.payload[:layout])
    end

    def layout_name
      page && page.payload[:layout]
    end
  end

  def self.view_subscriber
    @view_subscriber ||= ViewSubscriber.new
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails3-footnotes-4.0.0.pre.5 lib/rails-footnotes/view_subscriber.rb
rails3-footnotes-4.0.0.pre.4 lib/rails-footnotes/view_subscriber.rb