Skip to content

Commit a502f4d

Browse files
authored
Merge pull request #677 from code-corps/add-dialyzer
Added dialyzer locally (not on CI)
2 parents 46941c2 + 32e38c5 commit a502f4d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+245
-75
lines changed

circle.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ dependencies:
1414
test:
1515
override:
1616
- |
17-
if [ ${CIRCLE_PR_USERNAME} ]; then MIX_ENV=test mix test; else MIX_ENV=test mix coveralls.circle --include requires_env; fi
18-
17+
if [ ${CIRCLE_PR_USERNAME} ]; then
18+
MIX_ENV=test mix test;
19+
else
20+
MIX_ENV=test mix coveralls.circle --include requires_env;
21+
fi
1922
post:
2023
- mix inch.report
2124

lib/code_corps/emails/receipt_email.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule CodeCorps.Emails.ReceiptEmail do
1313
do
1414
BaseEmail.create
1515
|> to(charge.user.email)
16-
|> template(template_id, template_model)
16+
|> template(template_id(), template_model)
1717
else
1818
nil -> {:error, :project_not_found}
1919
other -> other
@@ -49,7 +49,7 @@ defmodule CodeCorps.Emails.ReceiptEmail do
4949
%{
5050
charge_amount: charge.amount |> format_amount(),
5151
charge_statement_descriptor: charge.statement_descriptor,
52-
high_five_image_url: high_five_image_url,
52+
high_five_image_url: high_five_image_url(),
5353
project_current_donation_goal_description: current_donation_goal.description,
5454
project_title: project.title,
5555
project_url: project |> url(),
@@ -62,7 +62,7 @@ defmodule CodeCorps.Emails.ReceiptEmail do
6262
"Your monthly donation to " <> project.title
6363
end
6464

65-
defp high_five_image_url, do: Enum.random(high_five_image_urls)
65+
defp high_five_image_url, do: Enum.random(high_five_image_urls())
6666

6767
defp high_five_image_urls, do: [
6868
"https://d3pgew4wbk2vb1.cloudfront.net/emails/images/[email protected]",

lib/code_corps/helpers/policy.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ defmodule CodeCorps.Helpers.Policy do
4646
4747
Returns `:string`
4848
"""
49-
@spec get_role(nil | OrganizationMembership.t | Changeset.t) :: String.t
49+
@spec get_role(nil | OrganizationMembership.t | Changeset.t) :: String.t | nil
5050
def get_role(nil), do: nil
5151
def get_role(%OrganizationMembership{role: role}), do: role
5252
def get_role(%Changeset{} = changeset), do: changeset |> Changeset.get_field(:role)

lib/code_corps/repo.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule CodeCorps.Repo do
22
use Ecto.Repo, otp_app: :code_corps
33
use Scrivener, page_size: 10
4+
5+
@dialyzer {:nowarn_function, rollback: 1}
46
end

lib/code_corps/services/donation_goals.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@ defmodule CodeCorps.Services.DonationGoalsService do
1515
alias CodeCorps.{DonationGoal, Project, Repo}
1616
alias Ecto.Multi
1717

18+
# Prevents warning for calling `Repo.transaction(multi)`.
19+
# The warning was caused with how the function is internally
20+
# implemented, so there's no way around it
21+
# As we update Ecto, we should check if this is still necessary.
22+
# Last check was Ecto 2.1.3
23+
@dialyzer :no_opaque
24+
1825
@doc """
1926
Creates the `CodeCorps.DonationGoal` by wrapping the following in a
2027
transaction:
2128
2229
- Inserting the donation goal
2330
- Updating the sibling goals with `update_related_goals/1`
2431
"""
32+
@spec create(map) :: tuple
2533
def create(attributes) do
2634
changeset =
2735
%DonationGoal{}
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
defmodule CodeCorps.Services.MarkdownRendererService do
2+
@moduledoc """
3+
Used to render provided markdown into html using an external renderer package.
4+
"""
5+
6+
alias Ecto.Changeset
7+
8+
@spec render_markdown_to_html(Changeset.t, atom, atom) :: Changeset.t
9+
def render_markdown_to_html(%Changeset{valid?: false} = changeset, _, _), do: changeset
210
def render_markdown_to_html(changeset, source_field, destination_field) do
3-
case changeset do
4-
%Ecto.Changeset{valid?: false} ->
5-
changeset
6-
%Ecto.Changeset{changes: %{^source_field => _}} ->
7-
changeset
8-
|> do_render_markdown_to_html(source_field, destination_field)
9-
_ ->
10-
changeset
11+
case Changeset.get_change(changeset, source_field) do
12+
nil -> changeset
13+
markdown -> markdown |> convert_into_html() |> put_into(changeset, destination_field)
1114
end
1215
end
13-
defp do_render_markdown_to_html(changeset, source_field, destination_field) do
14-
markdown =
15-
changeset
16-
|> Ecto.Changeset.get_change(source_field)
1716

18-
{html, _errors} =
19-
markdown
20-
|> Earmark.as_html()
17+
@spec convert_into_html(String.t) :: String.t
18+
defp convert_into_html(markdown) do
19+
# `Earmark.as_html` will return `{html, errors}`
20+
markdown |> Earmark.as_html() |> Tuple.to_list() |> List.first()
21+
end
2122

22-
changeset
23-
|> Ecto.Changeset.put_change(destination_field, html)
23+
@spec put_into(String.t, Changeset.t, atom) :: Changeset.t
24+
defp put_into(html, changeset, destination_field) do
25+
Ecto.Changeset.put_change(changeset, destination_field, html)
2426
end
2527
end

lib/code_corps/services/user_service.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ defmodule CodeCorps.Services.UserService do
1010

1111
alias CodeCorps.{Repo, StripeConnectCustomer, StripePlatformCustomer, User}
1212
alias CodeCorps.StripeService.{StripeConnectCustomerService, StripePlatformCustomerService}
13-
alias Ecto.Changeset
14-
alias Ecto.Multi
13+
alias Ecto.{Changeset, Multi}
14+
15+
# Prevents warning for calling `Repo.transaction(multi)`.
16+
# The warning was caused with how the function is internally
17+
# implemented, so there's no way around it
18+
# As we update Ecto, we should check if this is still necessary.
19+
# Last check was Ecto 2.1.3
20+
@dialyzer :no_opaque
1521

1622
@doc """
1723
Updates a `CodeCorps.User` record and, if necessary, associated

lib/code_corps/stripe_service/stripe_connect_account_service.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ defmodule CodeCorps.StripeService.StripeConnectAccountService do
88

99
@api Application.get_env(:code_corps, :stripe)
1010

11+
# Prevents warning for calling `Repo.transaction(multi)`.
12+
# The warning was caused with how the function is internally
13+
# implemented, so there's no way around it
14+
# As we update Ecto, we should check if this is still necessary.
15+
# Last check was Ecto 2.1.3
16+
@dialyzer :no_opaque
17+
1118
@doc """
1219
Used to create a remote `Stripe.Account` record as well as an associated local
1320
`StripeConnectAccount` record.

lib/code_corps/stripe_service/stripe_connect_external_account_service.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
defmodule CodeCorps.StripeService.StripeConnectExternalAccountService do
2+
@moduledoc """
3+
Used to perform actions on a `StripeConnectExternalAccount` record while
4+
propagating to and from the associated `Stripe.ExternalAccount` record.
5+
"""
26
alias CodeCorps.{Repo, StripeConnectAccount, StripeExternalAccount}
37
alias CodeCorps.StripeService.Adapters.StripeExternalAccountAdapter
48

5-
@api Application.get_env(:code_corps, :stripe)
6-
9+
@spec create(Stripe.ExternalAccount.t, StripeConnectAccount.t) :: {:ok, StripeExternalAccount.t}
710
def create(%Stripe.ExternalAccount{} = external_account, %StripeConnectAccount{} = connect_account) do
811
with {:ok, params} <- StripeExternalAccountAdapter.to_params(external_account, connect_account) do
912
%StripeExternalAccount{} |> StripeExternalAccount.changeset(params) |> Repo.insert
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
defmodule CodeCorps.StripeService.StripeConnectPlanService do
2+
@moduledoc """
3+
Used to perform actions on `StripeConnectPlan` records
4+
while at the same time propagating to and from associated `Stripe.Plan`
5+
records.
6+
"""
7+
28
alias CodeCorps.{Project, Repo, StripeConnectPlan}
39
alias CodeCorps.StripeService.Adapters.StripeConnectPlanAdapter
410
alias CodeCorps.StripeService.Validators.ProjectCanEnableDonations
@@ -8,17 +14,15 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do
814
@doc """
915
Creates a new `Stripe.Plan` record on Stripe API, as well as an associated local
1016
`StripeConnectPlan` record
11-
12-
# Possible return values
13-
14-
- `{:ok, %StripeConnectPlan{}}` - the created record.
15-
- `{:error, %Ecto.Changeset{}}` - the record was not created due to validation issues.
16-
- `{:error, :project_not_ready}` - the associated project does not meed the prerequisites for creating a plan.
17-
- `{:error, %Stripe.APIErrorResponse{}}` - there was a problem with the stripe request
18-
- `{:error, :not_found}` - one of the associated records was not found
1917
"""
18+
@spec create(map) :: {:ok, StripeConnectPlan.t} |
19+
{:error, Ecto.Changeset.t} |
20+
{:error, Stripe.APIErrorResponse.t} |
21+
{:error, :project_not_ready} |
22+
{:error, :not_found}
2023
def create(%{"project_id" => project_id} = attributes) do
21-
with {:ok, %Project{} = project} <- get_project(project_id) |> ProjectCanEnableDonations.validate,
24+
with {:ok, %Project{} = project} <- get_project(project_id),
25+
{:ok, %Project{}} <- ProjectCanEnableDonations.validate(project),
2226
%{} = create_attributes <- get_create_attributes(project_id),
2327
connect_account_id <- project.organization.stripe_connect_account.id_from_stripe,
2428
{:ok, plan} <- @api.Plan.create(create_attributes, connect_account: connect_account_id),
@@ -28,12 +32,10 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do
2832
|> StripeConnectPlan.create_changeset(params)
2933
|> Repo.insert
3034
else
31-
nil -> {:error, :not_found}
3235
failure -> failure
3336
end
3437
end
3538

36-
@spec get_create_attributes(binary) :: map
3739
defp get_create_attributes(project_id) do
3840
%{
3941
amount: 1, # in cents
@@ -46,8 +48,11 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do
4648
end
4749

4850
defp get_project(project_id) do
49-
Project
50-
|> Repo.get(project_id)
51-
|> Repo.preload([:donation_goals, {:organization, :stripe_connect_account}, :stripe_connect_plan])
51+
preloads = [:donation_goals, {:organization, :stripe_connect_account}, :stripe_connect_plan]
52+
53+
case Project |> Repo.get(project_id) |> Repo.preload(preloads) do
54+
nil -> {:error, :not_found}
55+
record -> {:ok, record}
56+
end
5257
end
5358
end

0 commit comments

Comments
 (0)