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

Style refactors #62

Merged
merged 13 commits into from
Jun 29, 2020
25 changes: 9 additions & 16 deletions lib/liquid_voting/delegations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ defmodule LiquidVoting.Delegations do
"""

import Ecto.Query, warn: false
alias LiquidVoting.Repo

alias LiquidVoting.Delegations.Delegation
alias LiquidVoting.Voting
alias __MODULE__.Delegation
alias LiquidVoting.{Repo, Voting}

@doc """
Returns the list of delegations for an organization uuid
Expand Down Expand Up @@ -63,8 +62,8 @@ defmodule LiquidVoting.Delegations do
delegator = Voting.get_participant_by_email!(delegator_email, organization_uuid)
delegate = Voting.get_participant_by_email!(delegate_email, organization_uuid)

Delegation
|> Repo.get_by!(
Repo.get_by!(
Delegation,
delegator_id: delegator.id,
delegate_id: delegate.id,
proposal_url: proposal_url,
Expand All @@ -90,8 +89,8 @@ defmodule LiquidVoting.Delegations do
delegator = Voting.get_participant_by_email!(delegator_email, organization_uuid)
delegate = Voting.get_participant_by_email!(delegate_email, organization_uuid)

Delegation
|> Repo.get_by!(
Repo.get_by!(
Delegation,
delegator_id: delegator.id,
delegate_id: delegate.id,
organization_uuid: organization_uuid
Expand Down Expand Up @@ -152,9 +151,7 @@ defmodule LiquidVoting.Delegations do
{:error, %Ecto.Changeset{}}

"""
def delete_delegation(%Delegation{} = delegation) do
Repo.delete(delegation)
end
def delete_delegation(%Delegation{} = delegation), do: Repo.delete(delegation)

@doc """
Deletes a Delegation.
Expand All @@ -168,9 +165,7 @@ defmodule LiquidVoting.Delegations do
** (Ecto.NoResultsError)

"""
def delete_delegation!(%Delegation{} = delegation) do
Repo.delete!(delegation)
end
def delete_delegation!(%Delegation{} = delegation), do: Repo.delete!(delegation)

@doc """
Returns an `%Ecto.Changeset{}` for tracking delegation changes.
Expand All @@ -181,7 +176,5 @@ defmodule LiquidVoting.Delegations do
%Ecto.Changeset{source: %Delegation{}}

"""
def change_delegation(%Delegation{} = delegation) do
Delegation.changeset(delegation, %{})
end
def change_delegation(%Delegation{} = delegation), do: Delegation.changeset(delegation, %{})
end
51 changes: 20 additions & 31 deletions lib/liquid_voting/voting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ defmodule LiquidVoting.Voting do
"""

import Ecto.Query, warn: false
alias LiquidVoting.Repo

alias LiquidVoting.Voting.{Vote, Participant}
alias LiquidVoting.Delegations
alias __MODULE__.{Vote, Participant}
alias LiquidVoting.{Repo, Delegations}
alias LiquidVoting.Delegations.Delegation

@doc """
Expand All @@ -25,7 +24,12 @@ defmodule LiquidVoting.Voting do
"""
def create_vote(attrs \\ %{}) do
Repo.transaction(fn ->
case %Vote{} |> Vote.changeset(attrs) |> Repo.insert() do
# TODO: refactor case statements into small functions.

%Vote{}
|> Vote.changeset(attrs)
|> Repo.insert()
|> case do
{:ok, vote} ->
if delegation =
Repo.get_by(Delegation,
Expand Down Expand Up @@ -160,9 +164,7 @@ defmodule LiquidVoting.Voting do
{:error, %Ecto.Changeset{}}

"""
def delete_vote(%Vote{} = vote) do
Repo.delete(vote)
end
def delete_vote(%Vote{} = vote), do: Repo.delete(vote)

@doc """
Deletes a Vote.
Expand All @@ -176,9 +178,7 @@ defmodule LiquidVoting.Voting do
Ecto.*Error

"""
def delete_vote!(%Vote{} = vote) do
Repo.delete!(vote)
end
def delete_vote!(%Vote{} = vote), do: Repo.delete!(vote)

@doc """
Returns an `%Ecto.Changeset{}` for tracking vote changes.
Expand All @@ -189,9 +189,7 @@ defmodule LiquidVoting.Voting do
%Ecto.Changeset{source: %Vote{}}

"""
def change_vote(%Vote{} = vote) do
Vote.changeset(vote, %{})
end
def change_vote(%Vote{} = vote), do: Vote.changeset(vote, %{})

@doc """
Returns the list of participants for an organization uuid
Expand Down Expand Up @@ -222,10 +220,8 @@ defmodule LiquidVoting.Voting do
** (Ecto.NoResultsError)

"""
def get_participant!(id, organization_uuid) do
Participant
|> Repo.get_by!(id: id, organization_uuid: organization_uuid)
end
def get_participant!(id, organization_uuid),
do: Repo.get_by!(Participant, id: id, organization_uuid: organization_uuid)

@doc """
Gets a single participant for an organization uuid by their email
Expand All @@ -241,10 +237,8 @@ defmodule LiquidVoting.Voting do
nil

"""
def get_participant_by_email(email, organization_uuid) do
Participant
|> Repo.get_by(email: email, organization_uuid: organization_uuid)
end
def get_participant_by_email(email, organization_uuid),
do: Repo.get_by(Participant, email: email, organization_uuid: organization_uuid)

@doc """
Gets a single participant for an organization uuid by their email
Expand All @@ -260,10 +254,8 @@ defmodule LiquidVoting.Voting do
** (Ecto.NoResultsError)

"""
def get_participant_by_email!(email, organization_uuid) do
Participant
|> Repo.get_by!(email: email, organization_uuid: organization_uuid)
end
def get_participant_by_email!(email, organization_uuid),
do: Repo.get_by!(Participant, email: email, organization_uuid: organization_uuid)

@doc """
Creates a participant.
Expand Down Expand Up @@ -328,9 +320,7 @@ defmodule LiquidVoting.Voting do
{:error, %Ecto.Changeset{}}

"""
def delete_participant(%Participant{} = participant) do
Repo.delete(participant)
end
def delete_participant(%Participant{} = participant), do: Repo.delete(participant)

@doc """
Returns an `%Ecto.Changeset{}` for tracking participant changes.
Expand All @@ -341,7 +331,6 @@ defmodule LiquidVoting.Voting do
%Ecto.Changeset{source: %Participant{}}

"""
def change_participant(%Participant{} = participant) do
Participant.changeset(participant, %{})
end
def change_participant(%Participant{} = participant),
do: Participant.changeset(participant, %{})
end
19 changes: 7 additions & 12 deletions lib/liquid_voting/voting_results.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ defmodule LiquidVoting.VotingResults do
"""

import Ecto.Query, warn: false
alias LiquidVoting.Repo
alias LiquidVoting.Voting
alias LiquidVoting.VotingWeight
alias LiquidVoting.VotingResults.Result

alias __MODULE__.Result
alias LiquidVoting.{Repo, Voting, VotingWeight}

@doc """
Creates or updates voting result based on votes
Expand Down Expand Up @@ -96,10 +95,8 @@ defmodule LiquidVoting.VotingResults do
** (Ecto.NoResultsError)

"""
def get_result!(id, organization_uuid) do
Result
|> Repo.get_by!(id: id, organization_uuid: organization_uuid)
end
def get_result!(id, organization_uuid),
do: Repo.get_by!(Result, id: id, organization_uuid: organization_uuid)

@doc """
Gets a single result by its proposal url and organization_uuid
Expand All @@ -115,10 +112,8 @@ defmodule LiquidVoting.VotingResults do
nil

"""
def get_result_by_proposal_url(proposal_url, organization_uuid) do
Result
|> Repo.get_by(proposal_url: proposal_url, organization_uuid: organization_uuid)
end
def get_result_by_proposal_url(proposal_url, organization_uuid),
do: Repo.get_by(Result, proposal_url: proposal_url, organization_uuid: organization_uuid)

@doc """
Creates a result.
Expand Down
7 changes: 2 additions & 5 deletions lib/liquid_voting/voting_weight.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ defmodule LiquidVoting.VotingWeight do
"""

import Ecto.Query, warn: false
alias LiquidVoting.Repo

alias LiquidVoting.Voting
alias LiquidVoting.{Repo, Voting}

@doc """
Updates vote weight based on delegations given to voter
Expand Down Expand Up @@ -72,7 +71,5 @@ defmodule LiquidVoting.VotingWeight do
# Base case for the above recursion:
#
# If no delegations left, just return the latest weight
defp delegation_weight(_ = [], _, weight) do
weight
end
defp delegation_weight(_ = [], _, weight), do: weight
end
8 changes: 4 additions & 4 deletions lib/liquid_voting_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule LiquidVotingWeb do

import Plug.Conn
import LiquidVotingWeb.Gettext

alias LiquidVotingWeb.Router.Helpers, as: Routes
end
end
Expand All @@ -35,16 +36,17 @@ defmodule LiquidVotingWeb do

# Import convenience functions from controllers
import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1]

import LiquidVotingWeb.ErrorHelpers
import LiquidVotingWeb.Gettext

alias LiquidVotingWeb.Router.Helpers, as: Routes
end
end

def router do
quote do
use Phoenix.Router

import Plug.Conn
import Phoenix.Controller
end
Expand All @@ -60,7 +62,5 @@ defmodule LiquidVotingWeb do
@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
defmacro __using__(which) when is_atom(which), do: apply(__MODULE__, which, [])
end
4 changes: 1 addition & 3 deletions lib/liquid_voting_web/channels/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ defmodule LiquidVotingWeb.UserSocket do
#
# See `Phoenix.Token` documentation for examples in
# performing token verification on connect.
def connect(_params, socket, _connect_info) do
{:ok, socket}
end
def connect(_params, socket, _connect_info), do: {:ok, socket}

# Socket id's are topics that allow you to identify all sockets for a given user:
#
Expand Down
38 changes: 19 additions & 19 deletions lib/liquid_voting_web/resolvers/delegations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ defmodule LiquidVotingWeb.Resolvers.Delegations do
alias LiquidVoting.{Delegations, Voting, VotingResults}
alias LiquidVotingWeb.Schema.ChangesetErrors

def delegations(_, _, %{context: %{organization_uuid: organization_uuid}}) do
{:ok, Delegations.list_delegations(organization_uuid)}
end
def delegations(_, _, %{context: %{organization_uuid: organization_uuid}}),
do: {:ok, Delegations.list_delegations(organization_uuid)}

def delegation(_, %{id: id}, %{context: %{organization_uuid: organization_uuid}}) do
{:ok, Delegations.get_delegation!(id, organization_uuid)}
end
def delegation(_, %{id: id}, %{context: %{organization_uuid: organization_uuid}}),
do: {:ok, Delegations.get_delegation!(id, organization_uuid)}

# Will add participants to the db if they don't exist yet, or fetch them if they do.
# Their ids are used for delegator_id and delegate_id when inserting the delegation
# with create_delegation_with_valid_arguments/1
# TODO: break up into smaller functions.
def create_delegation(
_,
%{delegator_email: delegator_email, delegate_email: delegate_email} = args,
Expand All @@ -25,8 +24,10 @@ defmodule LiquidVotingWeb.Resolvers.Delegations do
details: ChangesetErrors.error_details(changeset)}

{:ok, delegator} ->
args = Map.put(args, :delegator_id, delegator.id)
args = Map.put(args, :organization_uuid, organization_uuid)
args =
args
|> Map.put(:delegator_id, delegator.id)
|> Map.put(:organization_uuid, organization_uuid)

case Voting.upsert_participant(%{
email: delegate_email,
Expand All @@ -38,15 +39,17 @@ defmodule LiquidVotingWeb.Resolvers.Delegations do
details: ChangesetErrors.error_details(changeset)}

{:ok, delegate} ->
args = Map.put(args, :delegate_id, delegate.id)
create_delegation_with_valid_arguments(args)
args
|> Map.put(:delegate_id, delegate.id)
|> create_delegation_with_valid_arguments()
end
end
end

def create_delegation(_, %{} = args, %{context: %{organization_uuid: organization_uuid}}) do
args = Map.put(args, :organization_uuid, organization_uuid)
create_delegation_with_valid_arguments(args)
args
|> Map.put(:organization_uuid, organization_uuid)
|> create_delegation_with_valid_arguments()
end

def create_delegation_with_valid_arguments(args) do
Expand All @@ -71,12 +74,8 @@ defmodule LiquidVotingWeb.Resolvers.Delegations do
%{context: %{organization_uuid: organization_uuid}}
) do
deleted_delegation =
Delegations.get_delegation!(
delegator_email,
delegate_email,
proposal_url,
organization_uuid
)
delegator_email
|> Delegations.get_delegation!(delegate_email, proposal_url, organization_uuid)
|> Delegations.delete_delegation!()

VotingResults.publish_voting_result_change(proposal_url, organization_uuid)
Expand All @@ -90,7 +89,8 @@ defmodule LiquidVotingWeb.Resolvers.Delegations do
context: %{organization_uuid: organization_uuid}
}) do
deleted_delegation =
Delegations.get_delegation!(delegator_email, delegate_email, organization_uuid)
delegator_email
|> Delegations.get_delegation!(delegate_email, organization_uuid)
|> Delegations.delete_delegation!()

# VotingResults.publish_voting_result_change(proposal_url, organization_uuid)
Expand Down
Loading