Skip to content

Commit

Permalink
Version 0.3.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Jul 19, 2017
1 parent 5d32c80 commit efa20b4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 63 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 10 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
80 changes: 20 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit efa20b4

Please sign in to comment.