diff --git a/CHANGELOG.md b/CHANGELOG.md index 14f45af..f1e29e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # CHANGELOG +## Version 0.3.1 + +### Improvements + +- Adds `build_slug/2` to accept the original `changeset` as the second argument, it still receives list of `sources` as the first argument +- Updates `build_slug/1` inner logic + +### Documentation + +- Updates `README.md` with the new example +- Updates docs to handle new changes +- Updates `CONTRIBUTING.md` with 'Development' section + +### Testing + +- Adds new test cases to cover `build_slug/2` + ## Version 0.3.0 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bae8354..00fedc0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,11 +1,19 @@ # Contributing to EctoAutoslugField ## Pull Requests Welcome -1. Fork ecto_autoslug_field +1. Fork `ecto_autoslug_field` 2. Create a topic branch 3. Make logically-grouped commits with clear commit messages 4. Push commits to your fork -5. Open a pull request against ecto_autoslug_field/master +5. Open a pull request against `ecto_autoslug_field/master` + +## Development + +Please, make sure that all these commands succeed before pushing anything: + +1. `mix test` +2. `mix credo --strict` +3. `mix dialyzer` (it may take long on the first run) ## Issues diff --git a/README.md b/README.md index 34d5ca9..d8c8ade 100644 --- a/README.md +++ b/README.md @@ -49,79 +49,39 @@ Optional: The simplest example: ```elixir -defmodule NameSlug do - use EctoAutoslugField.Slug, from: :name, to: :slug +defmodule EctoSlugs.Blog.Article.TitleSlug do + use EctoAutoslugField.Slug, from: :title, to: :slug end -defmodule User do +defmodule EctoSlugs.Blog.Article do use Ecto.Schema import Ecto.Changeset + alias EctoSlugs.Blog.Article + alias EctoSlugs.Blog.Article.TitleSlug - schema "users" do - field :name, :string - field :slug, NameSlug.Type - end - - @required_fields ~w(name) - @optional_fields ~w(slug) - - def changeset(model, params \\ :empty) do - model - |> cast(params, @required_fields, @optional_fields) - |> NameSlug.maybe_generate_slug - |> NameSlug.unique_constraint - end -end -``` - -More complex example: - -```elixir -defmodule ComplexSlug do - use EctoAutoslugField.Slug, to: :slug - - def get_sourses(changeset, _opts) do - # This function is used to get sources to build slug from: - base_fields = [:title] - - if get_change(changeset, :breaking, false) do - base_fields ++ ["breaking"] - else - base_fields - end - end + schema "blog_articles" do + field :breaking, :boolean, default: false + field :content, :string + field :title, :string - def build_slug(sources, _changeset) do - # Custom slug building rule: - sources - |> Enum.join("-") - |> Slugger.slugify_downcase - |> String.replace("-", "+") - end -end + field :slug, TitleSlug.Type -defmodule Article do - use Ecto.Schema - import Ecto.Changeset - - schema "articles" do - field :title, :string - field :breaking, :boolean - field :slug, ComplexSlug.Type + timestamps() end - @required_fields ~w(title breaking) - @optional_fields ~w(slug) - - def changeset(model, params \\ :empty) do - model - |> cast(params, @required_fields, @optional_fields) - |> ComplexSlug.maybe_generate_slug - |> ComplexSlug.unique_constraint + def changeset(%Article{} = article, attrs) do + article + |> cast(attrs, [:title, :content, :breaking]) + |> validate_required([:title, :content]) + |> unique_constraint(:title) + |> TitleSlug.maybe_generate_slug + |> TitleSlug.unique_constraint end end ``` +More complex examples are cover in [this tutorial](https://medium.com/wemake-services/creating-slugs-for-ecto-schemas-7349513410f0). + ## Changelog See [CHANGELOG.md](https://github.com/sobolevn/ecto_autoslug_field/blob/master/CHANGELOG.md). diff --git a/mix.exs b/mix.exs index 91dcf7e..4bf9bd0 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule EctoAutoslugField.Mixfile do use Mix.Project - @version "0.3.0" + @version "0.3.1" @url "https://github.com/sobolevn/ecto_autoslug_field" def project do