module Ballast::Concerns::AjaxHandling

A concern to handle AJAX and HTTP requests.

Public Instance Methods

ajax_request?() click to toggle source

Checks if the current request is AJAX.

@return [Boolean] `true` if the request is AJAX, `false` otherwise.

# File lib/ballast/concerns/ajax_handling.rb, line 16
def ajax_request?
  request.safe_send(:xhr?).to_boolean || params[:xhr].to_boolean
end
allow_cors(allow_origin: "*", allow_methods: [:post, :get, :options], allow_headers: "*", max_age: 1.year, allow_credentials: false) click to toggle source

Allows HTTP Cross-Origin Resource Sharing.

@param allow_origin [String] The value for the `Access-Control-Allow-Origin` header. @param allow_methods [Array] A list of methods for the `Access-Control-Allow-Methods` header. @param allow_headers [String] The value for the `Access-Control-Allow-Headers` header. @param max_age [Float|Fixnum] The value for the `Access-Control-Max-Age` header. @param allow_credentials [Boolean] The value for the `Access-Control-Allow-Credentials` header.

# File lib/ballast/concerns/ajax_handling.rb, line 45
def allow_cors(allow_origin: "*", allow_methods: [:post, :get, :options], allow_headers: "*", max_age: 1.year, allow_credentials: false)
  headers.merge!({
    "Access-Control-Allow-Origin" => allow_origin,
    "Access-Control-Allow-Methods" => allow_methods.map { |m| m.to_s.upcase }.join(", "),
    "Access-Control-Allow-Headers" => allow_headers,
    "Access-Control-Max-Age" => max_age.to_i.to_s
  })

  headers["Access-Control-Allow-Credentials"] = "true" if allow_credentials
end
disallow_robots(configuration = nil)
Alias for: generate_robots_txt
generate_robots_txt(configuration = nil) click to toggle source

Generates a `robots.txt file.

@param configuration [Hash|NilClass] An hash of agent and list of paths to include.

# File lib/ballast/concerns/ajax_handling.rb, line 59
def generate_robots_txt(configuration = nil)
  configuration ||= {"*" => "/"}
  rv = configuration.reduce([]) do |accu, (agent, paths)|
    paths = paths.ensure_array.map { |e| "Disallow: #{e}" }

    accu << "User-agent: #{agent}\n#{paths.join("\n")}"
    accu
  end

  render(text: rv.join("\n\n"), content_type: "text/plain")
end
Also aliased as: disallow_robots
prepare_ajax_response(status: :ok, data: {}, error: nil) click to toggle source

Prepares an AJAX response.

@param status [Symbol|Fixnum] The HTTP status of the response. @param data [Object] The data of the response. @param error [Object|NilClass] The error of the response.

# File lib/ballast/concerns/ajax_handling.rb, line 25
def prepare_ajax_response(status: :ok, data: {}, error: nil)
  Ballast::AjaxResponse.new(status: status, data: data, error: error, transport: self)
end
prevent_caching() click to toggle source

Prevents HTTP caching.

# File lib/ballast/concerns/ajax_handling.rb, line 30
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