Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run the Elixir formatter on the codebase #31

Merged
merged 2 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#
# You can give explicit globs or simply directories.
# In the latter case `**/*.{ex,exs}` will be used.
#
#
included: ["lib/", "src/", "web/", "apps/"],
excluded: [~r"/_build/", ~r"/deps/"]
},
Expand Down Expand Up @@ -79,7 +79,6 @@
#
{Credo.Check.Design.TagTODO, exit_status: 2},
{Credo.Check.Design.TagFIXME},

{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.LargeNumbers},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 80},
Expand All @@ -97,7 +96,6 @@
{Credo.Check.Readability.VariableNames},
{Credo.Check.Readability.Semicolons},
{Credo.Check.Readability.SpaceAfterCommas},

{Credo.Check.Refactor.DoubleBooleanNegation},
{Credo.Check.Refactor.CondStatements},
{Credo.Check.Refactor.CyclomaticComplexity},
Expand All @@ -109,7 +107,6 @@
{Credo.Check.Refactor.Nesting},
{Credo.Check.Refactor.PipeChainStart},
{Credo.Check.Refactor.UnlessWithElse},

{Credo.Check.Warning.BoolOperationOnSameValues},
{Credo.Check.Warning.IExPry},
{Credo.Check.Warning.IoInspect},
Expand Down Expand Up @@ -140,7 +137,7 @@
{Credo.Check.Warning.NameRedeclarationByAssignment, false},
{Credo.Check.Warning.NameRedeclarationByCase, false},
{Credo.Check.Warning.NameRedeclarationByDef, false},
{Credo.Check.Warning.NameRedeclarationByFn, false},
{Credo.Check.Warning.NameRedeclarationByFn, false}

# Custom checks can be created using `mix credo.gen.check`.
#
Expand Down
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
import_deps: [:phoenix],
inputs: ["*.{ex,exs}", "{config,lib,priv,test}/**/*.{ex,exs}"]
]
5 changes: 2 additions & 3 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ config :habits, HabitsWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: System.get_env("SECRET_KEY_BASE"),
render_errors: [view: HabitsWeb.ErrorView, accepts: ~w(html json)],
pubsub: [name: HabitsWeb.PubSub,
adapter: Phoenix.PubSub.PG2]
pubsub: [name: HabitsWeb.PubSub, adapter: Phoenix.PubSub.PG2]

# Configures Elixir's Logger
config :logger, :console,
Expand All @@ -26,4 +25,4 @@ config :phoenix, :json_library, Jason

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
import_config "#{Mix.env()}.exs"
11 changes: 8 additions & 3 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ config :habits, HabitsWeb.Endpoint,
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
cd: Path.expand("../assets", __DIR__)]]

watchers: [
node: [
"node_modules/brunch/bin/brunch",
"watch",
"--stdin",
cd: Path.expand("../assets", __DIR__)
]
]

# Watch static and templates for browser reloading.
config :habits, HabitsWeb.Endpoint,
Expand Down
15 changes: 7 additions & 8 deletions lib/habits/accounts/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ defmodule Habits.Accounts.Account do
alias HabitsWeb.{Habit, Session}

schema "accounts" do
field :email, :string
field :encrypted_password, :string
field :password, :string, virtual: true
has_many :habits, Habit
has_many :sessions, Session
field(:email, :string)
field(:encrypted_password, :string)
field(:password, :string, virtual: true)
has_many(:habits, Habit)
has_many(:sessions, Session)

timestamps()
end
Expand All @@ -31,12 +31,11 @@ defmodule Habits.Accounts.Account do
|> unique_constraint(:email)
end

defp encrypt_password_if_possible(
%Ecto.Changeset{changes: %{password: password}} = changeset) do

defp encrypt_password_if_possible(%Ecto.Changeset{changes: %{password: password}} = changeset) do
changeset
|> put_change(:encrypted_password, hashed_password(password))
end

defp encrypt_password_if_possible(%Ecto.Changeset{} = changeset) do
changeset
end
Expand Down
1 change: 1 addition & 0 deletions lib/habits/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule Habits.Accounts do
ORDER BY week
;
"""

%Postgrex.Result{rows: rows} = Ecto.Adapters.SQL.query!(Repo, query, [])
Enum.map(rows, &List.first/1)
end
Expand Down
1 change: 1 addition & 0 deletions lib/habits/achievements/achievements.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule Habits.Achievements do
HabitCount.new(account, 10, "Track 10 Habits")
]
end

def all_for(%Habit{} = habit) do
[
CheckInCount.new(habit, 100, "Check In 100 Times"),
Expand Down
1 change: 1 addition & 0 deletions lib/habits/achievements/check_in_count.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Habits.Achievements.CheckInCount do
defp value_for(%Account{} = account) do
Repo.count(Ecto.assoc(account, [:habits, :check_ins]))
end

defp value_for(%Habit{} = habit) do
Habit.check_in_count(habit)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/habits/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Habits.Application do
# Start the Ecto repository
supervisor(Habits.Repo, []),
# Start the endpoint when the application starts
supervisor(HabitsWeb.Endpoint, []),
supervisor(HabitsWeb.Endpoint, [])
# Start your own worker by calling: Habits.Worker.start_link(arg1, arg2, arg3)
# worker(Habits.Worker, [arg1, arg2, arg3]),
]
Expand Down
2 changes: 2 additions & 0 deletions lib/habits/congratulations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ defmodule Habits.Congratulations do

def for(%Habit{} = habit) do
check_in_count = Habit.check_in_count(habit)

if check_in_count > 0 && rem(check_in_count, 50) == 0 do
Notification.new(habit.name, "Checked in #{check_in_count} times!")
end

current_streak = Habit.get_current_streak(habit)

if current_streak > 0 && rem(current_streak, 25) == 0 do
Notification.new(habit.name, "#{current_streak} in a row!")
end
Expand Down
12 changes: 6 additions & 6 deletions lib/habits/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ defmodule Habits.Date do

def shift_days(date, diff_days) do
date
|> Date.to_erl
|> :calendar.date_to_gregorian_days
|> Date.to_erl()
|> :calendar.date_to_gregorian_days()
|> diff_date(diff_days)
|> :calendar.gregorian_days_to_date()
|> Date.from_erl!
|> Date.from_erl!()
end

def today do
:calendar.local_time
:calendar.local_time()
|> extract_date_from_erl_datetime
|> Date.from_erl!
|> Date.from_erl!()
end

def yesterday do
Expand All @@ -25,7 +25,7 @@ defmodule Habits.Date do

def today_string do
today()
|> Date.to_iso8601
|> Date.to_iso8601()
end

defp diff_date(date, diff), do: date + diff
Expand Down
2 changes: 1 addition & 1 deletion lib/habits/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ defmodule Habits.Repo do
import Ecto.Query, only: [from: 2]

def count(queryable) do
Habits.Repo.one(from x in queryable, select: count(x.id))
Habits.Repo.one(from(x in queryable, select: count(x.id)))
end
end
5 changes: 3 additions & 2 deletions lib/habits_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ defmodule Habits.Web do

def view do
quote do
use Phoenix.View, root: "lib/habits_web/templates",
namespace: HabitsWeb
use Phoenix.View,
root: "lib/habits_web/templates",
namespace: HabitsWeb

# Import convenience functions from controllers
import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
Expand Down
2 changes: 1 addition & 1 deletion lib/habits_web/channels/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule HabitsWeb.UserSocket do
# def connect(%{"user" => user}, socket) do
def connect(%{"token" => token}, socket) do
case Repo.get_by(Session, token: token) do
nil -> :error
nil -> :error
session -> {:ok, assign(socket, :account_id, session.account_id)}
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/habits_web/controllers/api/v1/account_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ defmodule HabitsWeb.API.V1.AccountController do
"""
def create(conn, %{"account" => account_params}) do
with {:ok, account} <- Accounts.create_account(account_params),
{:ok, session} <- create_session(account)
do
{:ok, session} <- create_session(account) do
conn
|> put_status(:created)
|> put_view(SessionView)
Expand All @@ -35,6 +34,6 @@ defmodule HabitsWeb.API.V1.AccountController do
defp create_session(%Accounts.Account{id: account_id}) do
%Session{}
|> Session.changeset(%{account_id: account_id, location: "Initial registration"})
|> Repo.insert
|> Repo.insert()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule HabitsWeb.API.V1.AchievementController do
conn
|> render("index.json", achievements: Achievements.all_for(habit))
end

def index(conn, _params, current_account) do
conn
|> render("index.json", achievements: Achievements.all_for(current_account))
Expand Down
21 changes: 10 additions & 11 deletions lib/habits_web/controllers/api/v1/habit_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ defmodule HabitsWeb.API.V1.HabitController do
"""
def index(conn, %{"date" => date_string}, current_account) do
date = Date.from_iso8601!(date_string)

habits =
current_account
|> assoc(:habits)
|> order_by(:name)
|> Repo.all
|> Repo.all()

render conn, "index.json", habits: habits, date: date
render(conn, "index.json", habits: habits, date: date)
end

@doc """
Expand All @@ -35,7 +36,7 @@ defmodule HabitsWeb.API.V1.HabitController do
|> assoc(:habits)
|> Repo.get(habit_id)

render conn, "show.json", habit: habit
render(conn, "show.json", habit: habit)
end

@doc """
Expand All @@ -45,7 +46,7 @@ defmodule HabitsWeb.API.V1.HabitController do
habit =
%Habit{account_id: current_account.id}
|> Habit.changeset(habit_params)
|> Repo.insert!
|> Repo.insert!()

conn
|> put_status(:created)
Expand All @@ -61,7 +62,7 @@ defmodule HabitsWeb.API.V1.HabitController do
|> assoc(:habits)
|> Repo.get(habit_id)
|> Habit.changeset(%{name: name})
|> Repo.update!
|> Repo.update!()

conn
|> put_status(:ok)
Expand All @@ -75,9 +76,9 @@ defmodule HabitsWeb.API.V1.HabitController do
current_account
|> assoc(:habits)
|> Repo.get(habit_id)
|> Repo.delete
|> Repo.delete()

render conn, "success.json"
render(conn, "success.json")
end

@doc """
Expand All @@ -88,9 +89,8 @@ defmodule HabitsWeb.API.V1.HabitController do

with {:ok, habit} <- Habit.get_by_account(current_account, habit_id),
{:ok, _check_in} <- CheckIn.create_for_date(habit, date) do

Congratulations.for(habit)
render conn, "habit.json", habit: habit, date: date_string
render(conn, "habit.json", habit: habit, date: date_string)
else
{:error, message} ->
conn
Expand All @@ -107,9 +107,8 @@ defmodule HabitsWeb.API.V1.HabitController do

with {:ok, habit} <- Habit.get_by_account(current_account, habit_id),
{:ok, check_in} <- CheckIn.get_by_date(habit, date) do

Repo.delete!(check_in)
render conn, "habit.json", habit: habit, date: date_string
render(conn, "habit.json", habit: habit, date: date_string)
else
{:error, message} ->
conn
Expand Down
Loading