-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
131 additions
and
16 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule CodeCorps.Helpers.URL do | ||
@moduledoc """ | ||
Provides some helpers for assembling and validating URLs. | ||
""" | ||
|
||
alias Ecto.Changeset | ||
|
||
@doc """ | ||
Prefixes the URL with `http://` in the event that `http://` and `https://` are | ||
not already the starting format. If `nil`, simply returns `nil`. | ||
""" | ||
def prefix_url(changeset, key) do | ||
changeset | ||
|> Changeset.update_change(key, &do_prefix_url/1) | ||
end | ||
|
||
defp do_prefix_url(nil), do: nil | ||
defp do_prefix_url("http://" <> rest), do: "http://" <> rest | ||
defp do_prefix_url("https://" <> rest), do: "https://" <> rest | ||
defp do_prefix_url(value), do: "http://" <> value | ||
|
||
def valid_format do | ||
~r/\A((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,}(([0-9]{1,5})?\/.*)?#=\z/ix | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
priv/repo/migrations/20170308214128_add_website_to_project.exs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule CodeCorps.Repo.Migrations.AddWebsiteToProject do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:projects) do | ||
add :website, :string | ||
end | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
priv/repo/migrations/20170308220713_add_should_link_externally_to_project.exs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule CodeCorps.Repo.Migrations.AddShouldLinkExternallyToProject do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:projects) do | ||
add :should_link_externally, :boolean, default: false | ||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule CodeCorps.Helpers.URLTest do | ||
use ExUnit.Case, async: true | ||
import CodeCorps.Helpers.URL | ||
alias Ecto.Changeset | ||
|
||
test "returns nil when nil" do | ||
changeset = create_prefixed_changeset(nil) | ||
assert Changeset.get_change(changeset, :url) == nil | ||
end | ||
|
||
test "returns the original when starts with http://" do | ||
original = "http://www.google.com" | ||
changeset = create_prefixed_changeset(original) | ||
assert Changeset.get_change(changeset, :url) == original | ||
end | ||
|
||
test "returns the original when starts with https://" do | ||
original = "https://www.google.com" | ||
changeset = create_prefixed_changeset(original) | ||
assert Changeset.get_change(changeset, :url) == original | ||
end | ||
|
||
test "returns prefixed with http:// in every other case" do | ||
changeset = create_prefixed_changeset("www.google.com") | ||
assert Changeset.get_change(changeset, :url) == "http://www.google.com" | ||
end | ||
|
||
defp create_prefixed_changeset(value) do | ||
%Changeset{changes: %{url: value}} |> prefix_url(:url) | ||
end | ||
end |
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
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
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
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
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
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