     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
 

    end
  end

  def csv(conn, params) do
    registered = get_registered_by_controller_route!(params[:resource], conn)
    case registered do
      defn ->
        model = defn.__struct__

        csv = case model.run_query(repo, defn, :csv) do
          [] -> []
          [resource | resources] ->
            ExAdmin.View.Adapter.build_csv(resource, resources)
        end

        conn
        |> put_resp_content_type("text/csv")
        |> put_resp_header("Content-Disposition", "inline; filename=\"#{params[:resource]}.csv\"")
        |> send_resp(conn.status || 200, csv)
    end
  end

  @nested_key_list for i <- 1..5, do: {String.to_atom("nested#{i}"), String.to_atom("id#{i}")}

  def nested(conn, params) do
    registered = get_registered_by_controller_route!(params[:resource], conn)
    contents =
      case registered do
        defn ->
          model = defn.__struct__

          items = apply(model, :get_blocks, [conn, defn.resource_model.__struct__, params])
          block = deep_find(items, String.to_atom(params[:field_name]))

          resources = block[:opts][:collection].(conn, defn.resource_model.__struct__)

          contents = apply(model, :ajax_view, [conn, params, resources, block])
          contents
      end
    send_resp(conn, conn.status || 200, "text/javascript", contents)
  end


      collection_action = Keyword.get(collection_actions, action) ->
        collection_action.(conn, params)
      true ->
        apply(__MODULE__, action, [conn, defn, params])
    end
  end

