Module: HolyGrail::Extensions

Defined in:
lib/holygrail.rb

Constant Summary collapse

XHR_MOCK_SCRIPT =

JS script to reroute ajax requests through XhrProxy

"<script>\n  XMLHttpRequest.prototype.open = function(method, url, async, username, password) {\n    this.info = { method: method, url: url }\n  }\n  XMLHttpRequest.prototype.send = function(data) {\n    this.responseText = Ruby.HolyGrail.XhrProxy.request(this.info, data)\n    this.readyState = 4\n    this.onreadystatechange()\n  }\n</script>\n"

Instance Method Summary collapse

Instance Method Details

#js(code) ⇒ Object Also known as: execute_javascript

Execute javascript within the context of a view.

Examples:


class PeopleControllerTest < ActionController::TestCase
  get :index
  assert_equal 'People: index', js('document.title')
end

Raises:

  • (Johnson::Error)

    javascript code exception



77
78
79
80
81
82
# File 'lib/holygrail.rb', line 77

def js(code)
  XhrProxy.context = self
  @__page ||= Harmony::Page.new(XHR_MOCK_SCRIPT + rewrite_script_paths(@response.body.to_s))
  Harmony::Page::Window::BASE_RUNTIME.wait
  @__page.execute_js(code)
end

#process(*args) ⇒ Object

Clear harmony page on every request. Prevents changes to context from bleeding into the next one.

Examples:


get :index
js("foo = 'bar'")
js('foo') #=> "bar"

get :index
js('foo') #=> "Error: foo is not defined"


54
55
56
57
# File 'lib/holygrail.rb', line 54

def process(*args) #:nodoc:
  @__page = nil
  super
end