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(allow_origin: "*", allow_methods: [:post, :get, :options], allow_headers: "*", max_age: 1.year, allow_credentials: false)
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, pretty_json: false)
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(allow_origin: "*", allow_methods: [:post, :get, :options], allow_headers: "*", max_age: 1.year, allow_credentials: false)
Allows HTTP Cross-Origin Resource Sharing.
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ballast/concerns/ajax.rb', line 82 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.collect {|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 |
- (Object) disallow_robots
Disallows web robots.
94 95 96 |
# File 'lib/ballast/concerns/ajax.rb', line 94 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) ? true : false 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.
67 68 69 70 71 72 73 |
# File 'lib/ballast/concerns/ajax.rb', line 67 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, pretty_json: false)
Sends an AJAX response to the client.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ballast/concerns/ajax.rb', line 38 def send_ajax(data, status: :ok, format: :json, pretty_json: false) 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] # Adjust data data = (pretty_json ? Oj.dump(data) : ActiveSupport::JSON.encode(data)) if [:json, :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.
60 61 62 63 64 |
# File 'lib/ballast/concerns/ajax.rb', line 60 def update_ajax(data, operation = nil) operation ||= @operation data.merge!(operation.success? ? {data: operation.response[:data]} : {error: operation.errors.first}) data end |