Last active
November 14, 2022 20:00
-
-
Save Sleepful/79dfbcddcd8a15e8e98cf878433a9682 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule WordBase.Classifier.Value do | |
@kinds [ | |
%{string: "filter", atom: :Filter}, | |
%{string: "word", atom: :Word} | |
] | |
@types [ | |
%{string: "bool", atom: :Bool, db_type: :boolean}, | |
%{string: "text", atom: :Text, db_type: :string}, | |
%{string: "datetime", atom: :Datetime, db_type: :naive_datetime} | |
] | |
@all_types for type <- @types, kind <- @kinds, do: {type, kind} | |
for {type, kind} <- @all_types do | |
%{string: kind_string, atom: kind_atom} = kind | |
%{string: type_string, atom: type_atom, db_type: db_type} = type | |
module = "#{__MODULE__}.#{type_atom}.#{kind_atom}" | |
module = module |> String.to_atom() | |
table = "#{kind_string}s_categories_#{type_string}" | |
belongs_to_name = "#{kind_string}_category" |> String.to_atom() | |
belongs_to_module = | |
"#{WordBase.Classifier.Join}.#{type_atom}.#{kind_atom}" |> String.to_atom() | |
# WordBase.Classifier.Value.Bool.Filter | |
# WordBase.Classifier.Value.Bool.Word | |
# WordBase.Classifier.Value.Text.Filter | |
# WordBase.Classifier.Value.Text.Word | |
# WordBase.Classifier.Value.Datetime.Filter | |
# WordBase.Classifier.Value.Datetime.Word | |
defmodule module do | |
use Ecto.Schema | |
import Ecto.Changeset | |
schema table do | |
field :value, db_type | |
belongs_to belongs_to_name, belongs_to_module | |
end | |
def changeset(schema, attrs) do | |
schema | |
|> cast(attrs, [:value]) | |
|> validate_required(:value) | |
end | |
end | |
end | |
# multiselect is a little different: | |
for kind <- @kinds do | |
module = "#{__MODULE__.Multi}.#{kind.atom}" |> String.to_atom() | |
belongs_to_name = "#{kind.string}_category" |> String.to_atom() | |
belongs_to_module = "#{WordBase.Classifier.Join.Multi}.#{kind.atom}" |> String.to_atom() | |
# WordBase.Classifier.Value.Multi.Word | |
# WordBase.Classifier.Value.Multi.Filter | |
defmodule module do | |
use Ecto.Schema | |
import Ecto.Changeset | |
schema "#{kind.string}s_categories_multiselect" do | |
belongs_to belongs_to_name, belongs_to_module | |
belongs_to :multiselect, WordBase.Classifier.Multiselect | |
end | |
def changeset(_schema, _attrs) do | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment