This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule MyApp.Projects.Project.Policy do | |
| alias MyApp.System | |
| import Ecto.Query, only: [from: 2] | |
| def scope(%{role: "admin"}, _action, query), do: query | |
| def scope(user, _action, query), do: projects_for_user(query, user) | |
| defp projects_for_user(query, user) do | |
| user_projects = | |
| from project in query, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Projections.Repo.Migrations.CreateProjectionVersions do | |
| use Ecto.Migration | |
| def change do | |
| create table(:projection_versions, primary_key: false) do | |
| add :projection_name, :text, primary_key: true | |
| add :last_seen_event_id, :bigint | |
| timestamps | |
| end |
A port of a cheat sheet authored by Hafiz Ismail.
defmodule MyApp.Schema do
use Absinthe.Schema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Example.Server do | |
| use GenServer | |
| def start_link(opts \\ []) do | |
| GenServer.start_link(__MODULE__, :ok, opts) | |
| end | |
| def init(state) do | |
| Process.flag(:trap_exit, true) # must trap exit for terminate call back to work | |
| IO.inspect "Starting server..." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://groups.google.com/d/msg/growing-object-oriented-software/mCjbhcRAGDs/P7pkf62XBgAJ | |
| interface UserLookup { | |
| fun findByName(name: String): User | |
| } | |
| interface PermissionDecision { | |
| fun rejected(user: User) | |
| fun allowed(user: User) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # http://slack.elixirhq.com/absinthe-graphql/2016-08-16/ | |
| defp flatten_errors(changeset = %Ecto.Changeset{}) do | |
| Ecto.Changeset.traverse_errors(changeset, fn {error, _interpolates} -> error end) | |
| |> Enum.map(&flatten_errors/1) | |
| |> List.flatten | |
| end | |
| defp flatten_errors(errors) when is_map(errors), | |
| do: Enum.map(errors, &flatten_errors/1) | |
| defp flatten_errors({key, errors}) when is_list(errors), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule MyApp.Subscriber do | |
| use GenServer | |
| def start_link(channel) do | |
| GenServer.start_link(__MODULE__, channel) | |
| end | |
| def init(channel) do | |
| pid = self | |
| ref = MyApp.Endpoint.subscribe(pid, channel) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Foo do | |
| def call(x) do | |
| IO.puts x | |
| end | |
| def bar(x, y) do | |
| IO.puts x | |
| IO.puts y | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DraftNewLetter | |
| def self.build(current_user) | |
| @create_letter = CreateLetter.new | |
| @authorize = Authorize.new(current_user, @create_letter) | |
| @error_handler = ActiveRecordErrorHandler.new(@authorize) | |
| @assign_automatic_ccs = AssignAutomaticCCs.new(@create_letter) | |
| @transaction = Transaction.new(@assign_automatic_ccs) | |
| end | |
| end |