Class: Rack::Ajax

Inherits:
Object
  • Object
show all
Extended by:
DecisionTree
Defined in:
lib/rack-ajax.rb,
lib/rack-ajax/parser.rb,
lib/rack-ajax/decision_tree.rb

Defined Under Namespace

Modules: DecisionTree Classes: Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DecisionTree

default_decision_tree

Constructor Details

#initialize(app) ⇒ Ajax

If called with a block, executes that block as the “decision tree”. This is useful when testing.

To integrate Rack::Ajax into your app you should store the decision tree in a class-attribute decision_tree.

The default_decision_tree is used if no other is provided.



21
22
23
24
# File 'lib/rack-ajax.rb', line 21

def initialize(app)
  @app = app
  @decision_tree = block_given? ? Proc.new : (self.class.decision_tree || self.class.default_decision_tree)
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



12
13
14
# File 'lib/rack-ajax.rb', line 12

def params
  @params
end

#requestObject

Returns the value of attribute request.



12
13
14
# File 'lib/rack-ajax.rb', line 12

def request
  @request
end

#userObject

Returns the value of attribute user.



12
13
14
# File 'lib/rack-ajax.rb', line 12

def user
  @user
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rack-ajax.rb', line 26

def call(env)
  return @app.call(env) unless ::Ajax.is_enabled?

  # Parse the Ajax-Info header
  env["Ajax-Info"] = ::Ajax.unserialize_hash(env['HTTP_AJAX_INFO'])
  @parser = Parser.new(env)
  rack_response = @parser.instance_eval(&@decision_tree)

  # Clear the value of session[:redirected_to]
  unless env['rack.session'].nil?
    env['rack.session']['redirected_to'] = env['rack.session'][:redirected_to] = nil
  end

  # If we are testing our Rack::Ajax middleware, return
  # a Rack response now rather than falling through
  # to the application.
  #
  # To test rewrites, return a 200 response with
  # the modified request environment encoded as Yaml.
  #
  # The Ajax::RSpec::Helpers module includes a helper
  # method to test the result of a rewrite.
  if ::Ajax.is_mocked?
    rack_response.nil? ? Rack::Ajax::Parser.rack_response(encode_env(env)) : rack_response
  elsif !rack_response.nil?
    rack_response
  else
    # Fallthrough to the app.
    @app.call(env)
  end
end