Class: Ajax::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/ajax/application.rb

Overview

A class with framework/application related methods like discovering which version of Rails we are running under.

Instance Method Summary collapse

Instance Method Details

#initObject

Include framework hooks for Rails

This method is called by init.rb, which is run by Rails on startup.

Customize rendering. Include custom headers and don’t render the layout for AJAX. Insert the Rack::Ajax middleware to rewrite and handle requests. Add custom attributes to outgoing links.

Hooks for Rails 3 are installed using Railties.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ajax/application.rb', line 41

def init
  return unless rails?
  if rails?(3)
    require 'ajax/railtie'
  else
    require 'ajax/action_controller'
    require 'ajax/action_view'

    Ajax.logger = ::Rails.logger
    
    # Customize rendering.  Include custom headers and don't render the layout for AJAX.
    ::ActionController::Base.send(:include, Ajax::ActionController)

    # Insert the Rack::Ajax middleware to rewrite and handle requests
    ::ActionController::Dispatcher.middleware.insert_before(Rack::Head, Rack::Ajax)

    # Add custom attributes to outgoing links
    ::ActionView::Base.send(:include, Ajax::ActionView)
  end
end

#rails?(version = nil) ⇒ Boolean

Return a boolean indicating whether the Rails constant is defined. Pass a version integer to determine whether the major version of Rails matches version.

Example: rails?(3) returns true when Rails.version.to_i == 3.

Returns false if Rails is not defined or the major version does not match.

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ajax/application.rb', line 14

def rails?(version=nil)
  !!(if version.nil?
    defined?(::Rails)
  elsif defined?(::Rails)
    if ::Rails.respond_to?(:version)
      ::Rails.version.to_i == version
    else
      version <= 2 # Rails.version defined in 2.1.0
    end
  else
    false
  end)
end

#rootObject



28
29
30
# File 'lib/ajax/application.rb', line 28

def root
  Pathname.new(rails? && Rails.root || Dir.getwd)
end