Skip to content

Instantly share code, notes, and snippets.

@feymartynov
Created November 24, 2018 01:02
Show Gist options
  • Save feymartynov/1101755722bd5519686c760ecf11c027 to your computer and use it in GitHub Desktop.
Save feymartynov/1101755722bd5519686c760ecf11c027 to your computer and use it in GitHub Desktop.
ExOperation article: Setup
# mix.exs
{:ex_operation, “~> 0.4”}
# config/config.exs
config :ex_operation, repo: MyApp.Repo
# lib/myapp/operation.ex
defmodule MyApp.Operation do
defmacro __using__(opts) do
quote do
use ExOperation.Operation, unquote(opts)
import MyApp.Operation
end
end
# custom DSL extensions
end
# lib/myapp_web.ex
def run(operation, conn, params) do
context = %{user: conn.assigns.current_user}
operation |> ExOperation.run(context, params)
end
# lib/myapp_web/controllers/order_controller.ex
def checkout(conn, params) do
with {:ok, %{result: order}} <- Order.Checkout |> run(conn, params) do
render conn, order: order
end
end
@churcho
Copy link

churcho commented Jun 11, 2020

I've never had any success with the function at lib/myapp_web.ex
Do you have a sample that shows this setup?

@feymartynov
Copy link
Author

@churcho
Copy link

churcho commented Jun 11, 2020

Yea I just hooked that in. The gist should say where within lib/myapp_web.ex preferably within the def controller ...
It's tripped me a few times :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment