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)
-
- (Object) allow_cors
Allows HTTP Cross-Origin Resource Sharing.
-
- (Object) disallow_robots
Disallows web robots.
-
- (Boolean) is_ajax?
Checks if the current request is AJAX.
-
- (Object) prepare_ajax(status = :ok, data = nil, error = nil)
Prepares an AJAX response.
-
- (Object) prevent_caching
Prevents HTTP caching.
-
- (Object) send_ajax(data, status: :ok, format: :json)
Sends an AJAX response to the client.
-
- (Hash) update_ajax(data, operation = nil)
Updates an AJAX response from a operation, taking either the response data or the first error.
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.
16 17 18 |
# File 'lib/ballast/concerns/ajax.rb', line 16 def is_ajax? (request.respond_to?(:xhr?) && request.xhr?) || params[:xhr].to_boolean end |
- (Object) prepare_ajax(status = :ok, data = nil, error = nil)
Prepares an AJAX 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.
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.
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 |