<commit_before>defmodule CoursePlanner.Attendance do
  @moduledoc """
  This module holds the model for the attendance table
  """
  use CoursePlanner.Web, :model
  alias CoursePlanner.Settings
  alias Ecto.Changeset

  schema "attendances" do
    field :attendance_type, :string
    field :comment, :string
    belongs_to :student, CoursePlanner.User
    belongs_to :class, CoursePlanner.Class

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:attendance_type, :class_id, :student_id, :comment])
    |> validate_required([:attendance_type, :class_id, :student_id])
    |> cast_assoc(:class)
    |> cast_assoc(:student)
    |> validate_comment()
  end

  def validate_comment(%{valid?: true} = changeset) do
    comment = Changeset.get_field(changeset, :comment)

    comment_valid? =
      case comment do
        nil -> true
        _   -> "ATTENDANCE_DESCRIPTORS"
               |> Settings.get_value()
               |> Enum.member?(comment)
      end

    if comment_valid? do
      changeset
    else
      Changeset.add_error(changeset, :comment,
                  "The Provided comment is not among the valid options")
    end
  end
  def validate_comment(changeset), do: changeset
end
<commit_msg>refactors checks of membership<commit_after>defmodule CoursePlanner.Attendance do
  @moduledoc """
  This module holds the model for the attendance table
  """
  use CoursePlanner.Web, :model
  alias CoursePlanner.Settings
  alias Ecto.Changeset

  schema "attendances" do
    field :attendance_type, :string
    field :comment, :string
    belongs_to :student, CoursePlanner.User
    belongs_to :class, CoursePlanner.Class

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:attendance_type, :class_id, :student_id, :comment])
    |> validate_required([:attendance_type, :class_id, :student_id])
    |> cast_assoc(:class)
    |> cast_assoc(:student)
    |> validate_comment()
  end

  def validate_comment(%{valid?: true} = changeset) do
    comment = Changeset.get_field(changeset, :comment)

    if comment_valid?(comment) do
      changeset
    else
      Changeset.add_error(changeset, :comment,
                  "The Provided comment is not among the valid options")
    end
  end
  def validate_comment(changeset), do: changeset

  defp comment_valid?(comment) do
      case comment do
        nil -> true
        _   -> "ATTENDANCE_DESCRIPTORS"
               |> Settings.get_value()
               |> Enum.member?(comment)
      end
  end
end

{'fraction_non_alphanumeric': 0.09894407508799374, 'fraction_numerical': 0.0, 'mean_word_length': 3.06031746031746, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '5432409', 'n_tokens_mistral': 801, 'n_tokens_neox': 785, 'n_words': 196}