Skip to content

Commit f23007e

Browse files
committed
conform to credo style guides and add documentation explaining style guidelines
1 parent 7f985d2 commit f23007e

33 files changed

Lines changed: 73 additions & 108 deletions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Have everything installed and ready to work? Read [our usage guides](docs/USAGE.
3737
- [Run normal `mix` commands through Docker](docs/USAGE.md#interacting-with-the-app)
3838
- [Stop and start the server](docs/USAGE.md#stopping-and-starting-the-server)
3939
- [Run tests](docs/USAGE.md#running-tests)
40+
- [Linting and Style Guidelines](docs/USAGE.md#linting-code-with-credo)
4041
- [Rebuild Docker containers](docs/USAGE.md#rebuilding-docker-containers)
4142
- [Work with the Ember front-end](docs/USAGE.md#serving-ember)
4243
- [Set up environment variables](docs/USAGE.md#environment)

docs/USAGE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ If you ever need to rebuild you can run `docker-compose up --build`. Unless you'
3535

3636
To run the tests you can run `docker-compose run test mix test`.
3737

38+
### Linting Code with Credo
39+
40+
[Credo](https://github.com/rrrene/credo) is a static code analysis tool for Elixir. In general, we conform to the [Credo Style Guide](https://github.com/rrrene/elixir-style-guide) when writing Elixir code. You can run `mix credo` to check your code for design, readability, and consistency against this guide.
41+
42+
Credo's style guide is influenced by this more popular and exhaustive community [Elixir Style Guide](https://github.com/levionessa/elixir_style_guide). We defer to that guide where the Credo guide is ambiguous, e.g. [external module references](https://github.com/levionessa/elixir_style_guide#modules).
43+
3844
### Serving Ember
3945

4046
The Code Corps API is intended to work alongside a client written in Ember. For that purpose, the Elixir application exposes all of its API endpoints behind an `api.` subdomain.

lib/code_corps/base64_image.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule CodeCorps.Base64Image do
99
|> Base.decode64!
1010
|> save_as_image(content_type)
1111

12-
{ path_to_image, content_type }
12+
{path_to_image, content_type}
1313
end
1414

1515
defp save_as_image(content, content_type) do
@@ -25,7 +25,10 @@ defmodule CodeCorps.Base64Image do
2525
defp random_filename(length, extension), do: random_string(length) <> "." <> extension
2626

2727
defp random_string(length) do
28-
:crypto.strong_rand_bytes(length) |> Base.url_encode64 |> binary_part(0, length)
28+
length
29+
|> :crypto.strong_rand_bytes
30+
|> Base.url_encode64
31+
|> binary_part(0, length)
2932
end
3033

3134
defp ensure_tmp_dir do

lib/code_corps/validators/slug_validator.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule CodeCorps.Validators.SlugValidator do
22
@moduledoc """
3-
Used for validating slug fields in a given changeset.
3+
Used for validating slug fields in a given changeset.
44
"""
55

66
alias Ecto.Changeset

web/controllers/comment_controller.ex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
defmodule CodeCorps.CommentController do
2-
@analytics Application.get_env(:code_corps, :analytics)
3-
42
use CodeCorps.Web, :controller
5-
63
alias CodeCorps.Comment
74
alias JaSerializer.Params
85

9-
plug :load_and_authorize_resource, model: Comment, only: [:create, :update]
6+
@analytics Application.get_env(:code_corps, :analytics)
107

8+
plug :load_and_authorize_resource, model: Comment, only: [:create, :update]
119
plug :scrub_params, "data" when action in [:create, :update]
1210

1311
def index(conn, params = %{"task_id" => _}) do

web/controllers/organization_controller.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
defmodule CodeCorps.OrganizationController do
22
use CodeCorps.Web, :controller
3-
3+
import CodeCorps.Organization, only: [changeset: 2]
44
alias CodeCorps.Organization
55
alias JaSerializer.Params
66

7-
import Organization, only: [changeset: 2]
8-
97
plug :load_and_authorize_resource, model: Organization, only: [:create, :update]
108
plug :scrub_params, "data" when action in [:create, :update]
119

@@ -61,5 +59,4 @@ defmodule CodeCorps.OrganizationController do
6159
|> render(CodeCorps.ChangesetView, "error.json-api", changeset: changeset)
6260
end
6361
end
64-
6562
end

web/controllers/organization_membership_controller.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
defmodule CodeCorps.OrganizationMembershipController do
2-
@analytics Application.get_env(:code_corps, :analytics)
3-
42
use CodeCorps.Web, :controller
5-
63
alias JaSerializer.Params
74
alias CodeCorps.OrganizationMembership
85

6+
@analytics Application.get_env(:code_corps, :analytics)
7+
98
plug :load_and_authorize_resource, model: OrganizationMembership, only: [:create, :update, :delete]
109
plug :scrub_params, "data" when action in [:create, :update]
1110

web/controllers/task_controller.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
defmodule CodeCorps.TaskController do
2-
@analytics Application.get_env(:code_corps, :analytics)
3-
42
use CodeCorps.Web, :controller
5-
63
alias CodeCorps.Task
74
alias JaSerializer.Params
85

6+
@analytics Application.get_env(:code_corps, :analytics)
7+
98
plug :load_and_authorize_resource, model: Task, only: [:create, :update]
109
plug :scrub_params, "data" when action in [:create, :update]
1110

web/controllers/token_controller.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
defmodule CodeCorps.TokenController do
2-
@analytics Application.get_env(:code_corps, :analytics)
3-
42
use CodeCorps.Web, :controller
53
import Comeonin.Bcrypt, only: [checkpw: 2, dummy_checkpw: 0]
64
alias CodeCorps.GuardianSerializer
75
alias CodeCorps.Repo
86
alias CodeCorps.User
97

8+
@analytics Application.get_env(:code_corps, :analytics)
9+
1010
def create(conn, params = %{"username" => _, "password" => _}) do
1111
case login_by_email_and_pass(params) do
1212
{:ok, user} ->
@@ -31,7 +31,7 @@ defmodule CodeCorps.TokenController do
3131
|> put_status(:created)
3232
|> render("show.json", token: new_token, user_id: user.id)
3333
else
34-
{ :error, reason } -> handle_unauthorized(conn, reason)
34+
{:error, reason} -> handle_unauthorized(conn, reason)
3535
end
3636
end
3737

web/controllers/user_category_controller.ex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
defmodule CodeCorps.UserCategoryController do
2-
@analytics Application.get_env(:code_corps, :analytics)
3-
42
use CodeCorps.Web, :controller
5-
63
import CodeCorps.AuthenticationHelpers, only: [authorize: 2, authorized?: 1]
7-
84
alias CodeCorps.UserCategory
95
alias JaSerializer.Params
106

7+
@analytics Application.get_env(:code_corps, :analytics)
8+
119
plug :load_and_authorize_resource, model: UserCategory, only: [:delete]
1210
plug :scrub_params, "data" when action in [:create]
1311

0 commit comments

Comments
 (0)