Class Footnotes::Filter
In: lib/rails-footnotes/footnotes.rb
Parent: Object

Methods

Public Class methods

Method that calls Footnotes to attach its contents

[Source]

# File lib/rails-footnotes/footnotes.rb, line 31
      def after(controller)
        filter = Footnotes::Filter.new(controller)
        filter.add_footnotes!
        filter.close!(controller)
      end

Method called to start the notes It‘s a before filter prepend in the controller

[Source]

# File lib/rails-footnotes/footnotes.rb, line 25
      def before(controller)
        Footnotes::Filter.start!(controller)
      end

Process notes, discarding only the note if any problem occurs

[Source]

# File lib/rails-footnotes/footnotes.rb, line 53
      def each_with_rescue(notes)
        delete_me = []

        notes.each do |note|
          begin
            yield note
          rescue Exception => e
            # Discard note if it has a problem
            log_error("Footnotes #{note.to_s.camelize}Note Exception", e)
            delete_me << note
            next
          end
        end

        delete_me.each{ |note| notes.delete(note) }
        return notes
      end

Logs the error using specified title and format

[Source]

# File lib/rails-footnotes/footnotes.rb, line 73
      def log_error(title, exception)
        RAILS_DEFAULT_LOGGER.error "#{title}: #{exception}\n#{exception.backtrace.join("\n")}"
      end

[Source]

# File lib/rails-footnotes/footnotes.rb, line 90
    def initialize(controller)
      @controller = controller
      @template = controller.instance_variable_get(:@template)
      @body = controller.response.body
      @notes = []
    end

If none argument is sent, simply return the prefix. Otherwise, replace the args in the prefix.

[Source]

# File lib/rails-footnotes/footnotes.rb, line 80
      def prefix(*args)
        if args.empty?
          @@prefix
        else
          format(@@prefix, *args)
        end
      end

Calls the class method start! in each note Sometimes notes need to set variables or clean the environment to work properly This method allows this kind of setup

[Source]

# File lib/rails-footnotes/footnotes.rb, line 41
      def start!(controller)
        @@klasses = []

        each_with_rescue(@@notes.flatten) do |note|
          klass = "Footnotes::Notes::#{note.to_s.camelize}Note".constantize
          klass.start!(controller) if klass.respond_to?(:start!)
          @@klasses << klass
        end
      end

Public Instance methods

[Source]

# File lib/rails-footnotes/footnotes.rb, line 97
    def add_footnotes!
      add_footnotes_without_validation! if valid?
    rescue Exception => e
      # Discard footnotes if there are any problems
      self.class.log_error("Footnotes Exception", e)
    end

Calls the class method close! in each note Sometimes notes need to finish their work even after being read This method allows this kind of work

[Source]

# File lib/rails-footnotes/footnotes.rb, line 108
    def close!(controller)
      each_with_rescue(@@klasses) do |klass|
        klass.close!(controller)
      end
    end

Protected Instance methods

[Source]

# File lib/rails-footnotes/footnotes.rb, line 119
      def add_footnotes_without_validation!
        initialize_notes!
        insert_styles unless @@no_style
        insert_footnotes
      end

Process notes to get javascript code to close them. This method is only used when multiple_notes is false.

[Source]

# File lib/rails-footnotes/footnotes.rb, line 243
      def close
        javascript = ''
        each_with_rescue(@notes) do |note|
          next unless note.has_fieldset?
          javascript << close_helper(note)
        end
        javascript
      end

Helper that creates the javascript code to close the note

[Source]

# File lib/rails-footnotes/footnotes.rb, line 258
      def close_helper(note)
        "Footnotes.hide(document.getElementById('#{note.to_sym}_debug_info'));\n"
      end

[Source]

# File lib/rails-footnotes/footnotes.rb, line 145
      def component_request?
        @controller.instance_variable_get(:@parent_controller)
      end

Instance each_with_rescue method

[Source]

# File lib/rails-footnotes/footnotes.rb, line 295
      def each_with_rescue(*args, &block)
        self.class.each_with_rescue(*args, &block)
      end

Process notes to get their content

[Source]

# File lib/rails-footnotes/footnotes.rb, line 225
      def fieldsets
        content = ''
        each_with_rescue(@notes) do |note|
          next unless note.has_fieldset?
          content << "<fieldset id=\"\#{note.to_sym}_debug_info\" style=\"display: none\">\n<legend>\#{note.legend}</legend>\n<div>\#{note.content}</div>\n</fieldset>\n"
        end
        content
      end

[Source]

# File lib/rails-footnotes/footnotes.rb, line 125
      def initialize_notes!
        each_with_rescue(@@klasses) do |klass|
          note = klass.new(@controller)
          @notes << note if note.respond_to?(:valid?) && note.valid?
        end
      end

[Source]

# File lib/rails-footnotes/footnotes.rb, line 174
      def insert_footnotes
        # Fieldsets method should be called first
        content = fieldsets
        hideAll = @@multiple_notes ? nil : %[Footnotes.hideAll = function() { #{close} }]

        footnotes_html = "<!-- Footnotes -->\n<div style=\"clear:both\"></div>\n<div id=\"footnotes_debug\">\n\#{links}\n\#{content}\n<script type=\"text/javascript\">\n\#{read_asset('footnotes.js')}\n\#{hideAll}\n/* Additional Javascript */\n\#{@notes.map(&:javascript).compact.join(\"\\n\")}\n</script>\n</div>\n<!-- End Footnotes -->\n"

        if @body =~ %r{<div[^>]+id=['"]footnotes_holder['"][^>]*>}
          # Insert inside the "footnotes_holder" div if it exists
          insert_text :after, %r{<div[^>]+id=['"]footnotes_holder['"][^>]*>}, footnotes_html
        else
          # Otherwise, try to insert as the last part of the html body
          insert_text :before, /<\/body>/i, footnotes_html
        end
      end

Insertion methods

[Source]

# File lib/rails-footnotes/footnotes.rb, line 161
      def insert_styles
        insert_text :before, /<\/head>/i, "<!-- Footnotes Style -->\n<style type=\"text/css\">\n\#{read_asset('footnotes.css')}\n/* Aditional Stylesheets */\n\#{@notes.map(&:stylesheet).compact.join(\"\\n\")}\n</style>\n<!-- End Footnotes Style -->\n"
      end

Inserts text in to the body of the document pattern is a Regular expression which, when matched, will cause new_text to be inserted before or after the match. If no match is found, new_text is appended to the body instead. position may be either :before or :after

[Source]

# File lib/rails-footnotes/footnotes.rb, line 279
      def insert_text(position, pattern, new_text)
        index = case pattern
          when Regexp
            if match = @body.match(pattern)
              match.offset(0)[position == :before ? 0 : 1]
            else
              @body.size
            end
          else
            pattern
          end
        @body.insert index, new_text
      end

Helper that creates the link and javascript code when note is clicked

[Source]

# File lib/rails-footnotes/footnotes.rb, line 264
      def link_helper(note)
        onclick = note.onclick
        unless href = note.link
          href = '#'
          onclick ||= "Footnotes.hideAllAndToggle('#{note.to_sym}_debug_info');return false;" if note.has_fieldset?
        end

        "<a href=\"#{href}\" onclick=\"#{onclick}\">#{note.title}</a>"
      end

Process notes to gets their links in their equivalent row

[Source]

# File lib/rails-footnotes/footnotes.rb, line 207
      def links
        links = Hash.new([])
        order = []
        each_with_rescue(@notes) do |note|
          order << note.row
          links[note.row] += [link_helper(note)]
        end

        html = ''
        order.uniq!
        order.each do |row|
          html << "#{row.is_a?(String) ? row : row.to_s.camelize}: #{links[row].join(" | \n")}<br />"
        end
        html
      end

[Source]

# File lib/rails-footnotes/footnotes.rb, line 132
      def performed_render?
        @controller.instance_variable_get(:@performed_render)
      end

[Source]

# File lib/rails-footnotes/footnotes.rb, line 153
      def read_asset(filename)
        File.read(File.join(File.dirname(__FILE__), 'assets', filename))
      end

[Source]

# File lib/rails-footnotes/footnotes.rb, line 115
      def valid?
        performed_render? && valid_format? && valid_content_type? && @body.is_a?(String) && !component_request? && !xhr?
      end

[Source]

# File lib/rails-footnotes/footnotes.rb, line 140
      def valid_content_type?
        c = @controller.response.headers['Content-Type'].to_s
        (c.empty? || c =~ /html/)
      end

[Source]

# File lib/rails-footnotes/footnotes.rb, line 136
      def valid_format?
        [:html,:rhtml,:xhtml,:rxhtml].include?(@template.template_format.to_sym)
      end

[Source]

# File lib/rails-footnotes/footnotes.rb, line 149
      def xhr?
        @controller.request.xhr?
      end

[Validate]