Module: Ballast::Concerns::Ajax

Extended by:
ActiveSupport::Concern
Defined in:
lib/ballast/concerns/ajax.rb

Overview

A concern to handle AJAX and HTTP requests.

Instance Method Summary (collapse)

Instance Method Details

- (Object) allow_cors

Allows HTTP Cross-Origin Resource Sharing.



75
76
77
78
79
80
81
82
# File 'lib/ballast/concerns/ajax.rb', line 75

def allow_cors
  headers.merge!({
    "Access-Control-Allow-Origin" => "*",
    "Access-Control-Allow-Methods" => "POST, GET, OPTIONS",
    "Access-Control-Allow-Headers" => "*",
    "Access-Control-Max-Age" => 1.year.to_i.to_s
  })
end

- (Object) disallow_robots

Disallows web robots.



85
86
87
# File 'lib/ballast/concerns/ajax.rb', line 85

def disallow_robots
  render(text: "User-agent: *\nDisallow: /", content_type: "text/plain")
end

- (Boolean) is_ajax?

Checks if the current request is AJAX.

Returns:

  • (Boolean)

    true if the request is AJAX, false otherwise.



16
17
18
# File 'lib/ballast/concerns/ajax.rb', line 16

def is_ajax?
  ((request.respond_to?(:xhr?) && request.xhr?) || params[:xhr].to_boolean) ? true : false
end

- (Object) prepare_ajax(status = :ok, data = nil, error = nil)

Prepares an AJAX response.

Parameters:

  • status (Symbol|Fixnum) (defaults to: :ok)

    The HTTP status of the response.

  • data (Object) (defaults to: nil)

    Additional data to append to the response.

  • error (Object) (defaults to: nil)

    A error to append to the response.



25
26
27
28
29
30
# File 'lib/ballast/concerns/ajax.rb', line 25

def prepare_ajax(status = :ok, data = nil, error = nil)
  rv = {status: status}.ensure_access(:indifferent)
  rv[:error] = error if error.present?
  rv[:data] = data if data.present?
  rv
end

- (Object) prevent_caching

Prevents HTTP caching.



66
67
68
69
70
71
72
# File 'lib/ballast/concerns/ajax.rb', line 66

def prevent_caching
  response.headers.merge!({
    "Cache-Control" => "no-cache, no-store, max-age=0, must-revalidate",
    "Pragma" => "no-cache",
    "Expires" => "Fri, 01 Jan 1990 00:00:00 GMT"
  })
end

- (Object) send_ajax(data, status: :ok, format: :json)

Sends an AJAX response to the client.

Parameters:

  • data (Hash)

    The response to send.

  • status (Symbol|Fixnum)

    The HTTP status of the response, ignored if already set in data.

  • format (Symbol)

    The content type of the response.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ballast/concerns/ajax.rb', line 37

def send_ajax(data, status: :ok, format: :json)
  if !performed? then
    # Prepare data
    data = prepare_ajax_send(data, status)

    # Setup callback and format
    format, callback, content_type = format_ajax_send(format)
    status = data[:status]

    # Prepare data for formatting
    data = ActiveSupport::JSON.encode(data) if [:json, :jsonp, :pretty_json, :pretty_jsonp, :text].include?(format)

    # Render
    render(format => data, status: status, callback: callback, content_type: content_type)
  end
end

- (Hash) update_ajax(data, operation = nil)

Updates an AJAX response from a operation, taking either the response data or the first error.

Parameters:

  • data (Hash)

    The current data.

  • operation (Operation) (defaults to: nil)

    The operation to gather data from.

Returns:

  • (Hash)

    The updated data.



59
60
61
62
63
# File 'lib/ballast/concerns/ajax.rb', line 59

def update_ajax(data, operation = nil)
  operation ||= @operation
  data.merge!(operation.success? ? {data: operation.response[:data]} : {error: operation.errors.first})
  data
end