Sha256: 50a8081d0422f371e67def70c6e7b26a3c8f6a9d749c9bbeba2fe2ceeb838e8c

Contents?: true

Size: 1.69 KB

Versions: 5

Compression:

Stored size: 1.69 KB

Contents

module Paloma
  module ActionControllerFilters
    
    def self.included base
      base.module_eval do
        prepend_view_path "#{Paloma.root}/app/views/"
        before_filter :js
        after_filter :update_callback, :if => :html_response_from_render?
      end
    end

    
  protected
    
    def html_response_from_render?
      [nil, 'text/html'].include?(response.content_type) && self.status != 302 
    end

        
    def update_callback
      return clear_callbacks if @__paloma_callback__.nil?

      add_to_callbacks @__paloma_callback__

      paloma_txt = view_context.render(
        :partial => "paloma/callback_hook",
        :locals => {:callbacks => session[:callbacks]})
      
      before_body_end_index = response_body[0].rindex('</body>')
      
      if before_body_end_index.present?
        before_body_end_content = response_body[0][0, before_body_end_index].html_safe
        after_body_end_content = response_body[0][before_body_end_index..-1].html_safe
        
        response_body[0] = before_body_end_content + paloma_txt + after_body_end_content
       
        response.body = response_body[0]
      else
        # If body tag is not present, append paloma_txt in the response body
        response_body[0] += paloma_txt
        response.body = response_body[0]
      end
      
      clear_callbacks
    end
    
    
    def add_to_callbacks callback
      session[:callbacks] ||= []
      session[:callbacks].push(callback) if callback.present?
    end
    
    
    def get_callbacks
      session[:callbacks]
    end
    
    
    def clear_callbacks
      session[:callbacks] = []
    end
  
  end
  
  ::ActionController::Base.send :include, ActionControllerFilters
end


Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
paloma-2.0.6 lib/paloma/action_controller_filters.rb
paloma-2.0.5 lib/paloma/action_controller_filters.rb
paloma-2.0.4 lib/paloma/action_controller_filters.rb
paloma-2.0.3 lib/paloma/action_controller_filters.rb
paloma-2.0.2 lib/paloma/action_controller_filters.rb