<commit_before>defmodule ExAdmin.ResourceController do
  @moduledoc false

  defmacro __using__(_opts) do
    quote do
      alias ExAdmin.Authorization
      import ExAdmin.Utils
      import ExAdmin.ParamsToAtoms

      def action(%{private: %{phoenix_action: action}} = conn, _options) do
        conn = conn |> assign(:xhr, get_req_header(conn, "x-requested-with") == ["XMLHttpRequest"])
        resource = @resource || conn.params["resource"]
        conn = scrub_params(conn, resource, action)
        defn = get_registered_by_controller_route!(conn, resource)
        params = filter_params(conn.params, defn.resource_model)

        if !restricted_action?(action, defn) do
          conn
          |> assign(:defn, defn)
          |> load_resource(action, defn, params[:id])
          |> authorize_action(action)
          |> handle_plugs(action, defn)
          |> handle_before_filter(action, defn, params)
          |> handle_custom_actions(action, defn, params)
        else
          render_403 conn
        end
      end

      defp do_form_view(conn, resource, params) do
        model = conn.assigns.defn.__struct__
        if function_exported? model, :form_view, 3 do
          apply(model, :form_view, [conn, resource, params])
        else
          ExAdmin.Form.default_form_view conn, resource, params
        end
      end

      defp handle_changeset_error(conn, defn, changeset, params) do
        conn = put_flash(conn, :inline_error, changeset.errors)
        |> Plug.Conn.assign(:changeset, changeset)
        |> Plug.Conn.assign(:ea_required,
           defn.resource_model.changeset(conn.assigns.resource).required)
        contents = do_form_view(conn, ExAdmin.Changeset.get_data(changeset), params)
        render(conn, "admin.html", html: contents, filters: nil)
      end

      defp render_403(conn) do
        conn
        |> put_layout(false)
        |> put_status(403)
        |> render(ExAdmin.ErrorView, "403.html")
        |> halt
      end

      defp restricted_action?(:destroy, defn), do: restricted_action?(:delete, defn)
      defp restricted_action?(:create, defn), do: restricted_action?(:new, defn)<commit_msg>Fetch required fields from the changeset itself<commit_after>defmodule ExAdmin.ResourceController do
  @moduledoc false

  defmacro __using__(_opts) do
    quote do
      alias ExAdmin.Authorization
      import ExAdmin.Utils
      import ExAdmin.ParamsToAtoms

      def action(%{private: %{phoenix_action: action}} = conn, _options) do
        conn = conn |> assign(:xhr, get_req_header(conn, "x-requested-with") == ["XMLHttpRequest"])
        resource = @resource || conn.params["resource"]
        conn = scrub_params(conn, resource, action)
        defn = get_registered_by_controller_route!(conn, resource)
        params = filter_params(conn.params, defn.resource_model)

        if !restricted_action?(action, defn) do
          conn
          |> assign(:defn, defn)
          |> load_resource(action, defn, params[:id])
          |> authorize_action(action)
          |> handle_plugs(action, defn)
          |> handle_before_filter(action, defn, params)
          |> handle_custom_actions(action, defn, params)
        else
          render_403 conn
        end
      end

      defp do_form_view(conn, resource, params) do
        model = conn.assigns.defn.__struct__
        if function_exported? model, :form_view, 3 do
          apply(model, :form_view, [conn, resource, params])
        else
          ExAdmin.Form.default_form_view conn, resource, params
        end
      end

      defp handle_changeset_error(conn, defn, changeset, params) do
        conn = put_flash(conn, :inline_error, changeset.errors)
        |> Plug.Conn.assign(:changeset, changeset)
        |> Plug.Conn.assign(:ea_required, changeset.required)
        contents = do_form_view(conn, ExAdmin.Changeset.get_data(changeset), params)
        render(conn, "admin.html", html: contents, filters: nil)
      end

      defp render_403(conn) do
        conn
        |> put_layout(false)
        |> put_status(403)
        |> render(ExAdmin.ErrorView, "403.html")
        |> halt
      end

      defp restricted_action?(:destroy, defn), do: restricted_action?(:delete, defn)
      defp restricted_action?(:create, defn), do: restricted_action?(:new, defn)
{'fraction_non_alphanumeric': 0.10651162790697674, 'fraction_numerical': 0.0060465116279069765, 'mean_word_length': 2.799469964664311, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6595473', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1268, 'n_words': 281}