     render conn, "toggle_attr.js", attr_name: attr_name, attr_value: Map.get(resource, attr_name_atom), id: ExAdmin.Schema.get_id(resource)
   end
 
-  def destroy(conn, params) do
-    registered = get_registered_by_controller_route!(params[:resource], conn)
-
-    resource_model =
-      case registered do
-        defn ->
-          model = defn.__struct__
-          resource_model = model.__struct__.resource_model
-          |> base_name |> String.downcase |> String.to_atom
-
-          resource = model.run_query(repo, defn, :edit, params[:id])
-
-          if resource == nil do
-            raise Phoenix.Router.NoRouteError, conn: conn, router: __MODULE__
-          end
-
-          ExAdmin.Repo.delete(resource, params[resource_model])
-          base_name model
-      end
-
-    case get_req_header(conn, "x-requested-with") do
-      ["XMLHttpRequest"] ->
-        render conn, "destroy.js", tr_id: String.downcase("#{resource_model}_#{params[:id]}")
-      _ ->
-        put_flash(conn, :notice, "#{resource_model} was successfully destroyed.")
-        |> redirect(to: get_route_path(conn, :index))
-    end
-  end
-
-  def batch_action(conn, %{batch_action: "destroy"} = params) do
-    defn = get_registered_by_controller_route!(params[:resource], conn)
-
+  def destroy(conn, defn, params) do
+    model = defn.__struct__
+    resource_model = model.__struct__.resource_model
+    |> base_name |> String.downcase |> String.to_atom
+
+    resource = model.run_query(repo, defn, :edit, params[:id])
+
+    if resource == nil do
+      raise Phoenix.Router.NoRouteError, conn: conn, router: __MODULE__
+    end
+
+    ExAdmin.Repo.delete(resource, params[resource_model])
+    resource_model = base_name model
+
+    if conn.assigns.xhr do
+      render conn, "destroy.js", tr_id: String.downcase("#{resource_model}_#{params[:id]}")
+    else
+      put_flash(conn, :notice, "#{resource_model} was successfully destroyed.")
+      |> redirect(to: get_route_path(conn, :index))
+    end
+  end
+
+  def batch_action(conn, defn, %{batch_action: "destroy"} = params) do
     model = defn.__struct__
     resource_model = model.__struct__.resource_model
 

    handle_action(conn, action, conn.params["resource"])
  end

  defp handle_action(conn, action, nil) do
    ExAdmin.get_all_registered
    |> Enum.sort(&(elem(&1,1).menu[:priority] < elem(&2,1).menu[:priority]))
    |> hd
    |> case do
      {_, %{controller_route: resource}} ->
        conn = scrub_params(conn, resource, action)
        params = filter_params(conn.params)
        conn
        |> struct(path_info: conn.path_info ++ [resource])
        |> struct(params: Map.put(conn.params, "resource", resource))
        |> handle_action(action, resource)
      _other ->
        throw :invalid_route
    end
  end
  defp handle_action(conn, action, resource) do
    conn = scrub_params(conn, resource, action)
    params = filter_params(conn.params)

  defp authorized?(conn), do: conn


  def index(conn, defn, params) do
    model = defn.__struct__

    page = case conn.assigns[:page] do
      nil ->
        model.run_query(repo, defn, :index, params |> Map.to_list)
      page ->
        page
    end
    scope_counts = model.run_query_counts repo, defn, :index, params |> Map.to_list

    contents = if function_exported? model, :index_view, 3 do
      apply(model, :index_view, [conn, page, scope_counts])
    else
      ExAdmin.Index.default_index_view(conn, page, scope_counts)
    end

    assign(conn, :scope_counts, scope_counts)
    |> render("admin.html", html: contents, defn: defn, resource: page,
      filters: (if false in defn.index_filters, do: false, else: defn.index_filters))
  end

  def show(conn, defn, params) do
    model = defn.__struct__

    resource = if Application.get_all_env(:auth_ex) != [] do
      resource_name = AuthEx.Utils.resource_name conn, model: defn.resource_model
      case conn.assigns[resource_name] do
        nil ->
          model.run_query(repo, defn, :show, params[:id])
        res ->
          res
      end
    else
      model.run_query(repo, defn, :show, params[:id])
    end

    if resource == nil do
      raise Phoenix.Router.NoRouteError, conn: conn, router: __MODULE__
    end

    contents = if function_exported? model, :show_view, 2 do
      apply(model, :show_view, [conn, resource])
    else
      ExAdmin.Show.default_show_view(conn, resource)
    end

    render conn, "admin.html", html: contents, resource: resource, filters: nil, defn: defn
  end

  def edit(conn, defn, params) do
    model = defn.__struct__
    resource = model.run_query(repo, defn, :edit, params[:id])

    if resource == nil do
      raise Phoenix.Router.NoRouteError, conn: conn, router: __MODULE__
    end

    contents = do_form_view(model, conn, resource, params)

    render conn, "admin.html", html: contents, resource: resource, filters: nil, defn: defn
  end

  def new(conn, defn, params) do
    model = defn.__struct__
    resource = model.__struct__.resource_model.__struct__
    contents = do_form_view(model, conn, resource, params)

    render conn, "admin.html", html: contents, resource: resource, filters: nil, defn: defn
  end

