Sha256: e08699091940195eafb61cee78efe0869d081c1d71f3b563b0df9fb75556f626

Contents?: true

Size: 1019 Bytes

Versions: 12

Compression:

Stored size: 1019 Bytes

Contents

module RemoteRequestManager
  extend ActiveSupport::Concern

  PATH_TO_JS_FILE = "shopapp/modal_errors"

  included do
  end

  class_methods do
  end

  def redirect_remote_request(notice, args = {})
    respond_to do |format|
      format.html { redirect_to redirect_path(args), success: notice }
      format.js do
        flash[:success] = notice
        render js: "window.location='#{redirect_path(args)}'"
      end
    end
  end

  def rescue_remote_request(error)
    Rails.logger.error error.message
    Rails.logger.error error.backtrace.join("\n")
    if error.instance_of?(ActiveRecord::RecordInvalid)
      respond_to_js(error.record.errors.messages)
    else
      respond_to_js(error)
    end
  end

  private

  def respond_to_js(error_message)
    flash.now[:danger] = error_message
    respond_to do |format|
      format.js do
        render PATH_TO_JS_FILE, locals: { flash: flash }
      end
    end
  end

  def redirect_path(args)
    args[:to].presence || request.referer || root_path
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
shopapp-0.76.06 app/controllers/concerns/remote_request_manager.rb
shopapp-0.76.05 app/controllers/concerns/remote_request_manager.rb
shopapp-0.76.04 app/controllers/concerns/remote_request_manager.rb
shopapp-0.75.03 app/controllers/concerns/remote_request_manager.rb
shopapp-0.75.02 app/controllers/concerns/remote_request_manager.rb
shopapp-0.73.02 app/controllers/concerns/remote_request_manager.rb
shopapp-0.3.76 app/controllers/concerns/remote_request_manager.rb
shopapp-0.2.75 app/controllers/concerns/remote_request_manager.rb
shopapp-0.2.74 app/controllers/concerns/remote_request_manager.rb
shopapp-0.2.73 app/controllers/concerns/remote_request_manager.rb
shopapp-0.2.72 app/controllers/concerns/remote_request_manager.rb
shopapp-0.2.71 app/controllers/concerns/remote_request_manager.rb