Skip to content

Commit 656cc94

Browse files
committed
Fix bug where markdown doesn't clear when updating with nil values
1 parent 3707679 commit 656cc94

File tree

7 files changed

+37
-3
lines changed

7 files changed

+37
-3
lines changed

lib/code_corps/analytics/segment_data_extractor.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule CodeCorps.Analytics.SegmentDataExtractor do
55

66
@spec get_action(Plug.Conn.t) :: atom
77
def get_action(%Plug.Conn{private: %{phoenix_action: action}}), do: action
8+
def get_action(_), do: nil
89

910
@spec get_resource(Plug.Conn.t) :: struct
1011
def get_resource(%Plug.Conn{assigns: %{data: data}}), do: data

lib/code_corps/services/markdown_renderer.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule CodeCorps.Services.MarkdownRendererService do
99
def render_markdown_to_html(%Changeset{valid?: false} = changeset, _, _), do: changeset
1010
def render_markdown_to_html(changeset, source_field, destination_field) do
1111
case Changeset.get_change(changeset, source_field) do
12+
"" -> Changeset.put_change(changeset, destination_field, nil)
1213
nil -> changeset
1314
markdown -> markdown |> convert_into_html() |> put_into(changeset, destination_field)
1415
end
@@ -23,6 +24,6 @@ defmodule CodeCorps.Services.MarkdownRendererService do
2324

2425
@spec put_into(String.t, Changeset.t, atom) :: Changeset.t
2526
defp put_into(html, changeset, destination_field) do
26-
Ecto.Changeset.put_change(changeset, destination_field, html)
27+
Changeset.put_change(changeset, destination_field, html)
2728
end
2829
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule CodeCorps.Repo.Migrations.AddCheckConstraintToProjectDescriptionWhenProjectIsApproved do
2+
use Ecto.Migration
3+
4+
def change do
5+
create constraint(:projects, "set_long_description_markdown_if_approved", check: "long_description_markdown IS NOT NULL OR approved = false")
6+
end
7+
end

priv/repo/structure.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ CREATE TABLE projects (
402402
cloudinary_public_id character varying(255),
403403
default_color character varying(255),
404404
website character varying(255),
405-
should_link_externally boolean DEFAULT false
405+
should_link_externally boolean DEFAULT false,
406+
CONSTRAINT set_long_description_markdown_if_approved CHECK (((long_description_markdown IS NOT NULL) OR (approved = false)))
406407
);
407408

408409

@@ -2663,5 +2664,5 @@ ALTER TABLE ONLY user_tasks
26632664
-- PostgreSQL database dump complete
26642665
--
26652666

2666-
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611);
2667+
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449);
26672668

test/lib/code_corps/services/markdown_renderer_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ defmodule CodeCorps.Services.MarkdownRendererServiceTest do
1212
status: "open"
1313
}
1414

15+
test "renders empty strings to nil" do
16+
attrs = @valid_attrs |> Map.merge(%{markdown: ""})
17+
changeset =
18+
%Task{}
19+
|> Task.changeset(attrs)
20+
|> render_markdown_to_html(:markdown, :body)
21+
22+
assert changeset |> Ecto.Changeset.get_change(:body) == nil
23+
end
24+
25+
test "returns unchanged changeset when nil" do
26+
attrs = @valid_attrs |> Map.merge(%{markdown: nil})
27+
changeset =
28+
%Task{}
29+
|> Task.changeset(attrs)
30+
|> render_markdown_to_html(:markdown, :body)
31+
32+
assert changeset == %Task{} |> Task.changeset(attrs)
33+
end
34+
1535
test "renders markdown to html" do
1636
changeset =
1737
%Task{}

test/support/factories.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ defmodule CodeCorps.Factories do
7676
def project_factory do
7777
%CodeCorps.Project{
7878
approved: true,
79+
long_description_markdown: sequence(:long_description_markdown, &"Description #{&1}"), # once approved, this MUST be set
7980
slug: sequence(:slug, &"project-#{&1}"),
8081
title: sequence(:title, &"Project #{&1}"),
8182
website: sequence(:website, &"http://test-#{&1}.com"),

web/models/project.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ defmodule CodeCorps.Project do
5353
|> generate_slug(:title, :slug)
5454
|> validate_slug(:slug)
5555
|> unique_constraint(:slug, name: :index_projects_on_slug)
56+
|> check_constraint(:long_description_markdown,
57+
message: "cannot be deleted once your project is approved",
58+
name: "set_long_description_markdown_if_approved")
5659
|> MarkdownRendererService.render_markdown_to_html(:long_description_markdown, :long_description_body)
5760
end
5861

0 commit comments

Comments
 (0)