RicoTrevisan

RicoTrevisan

Best Practices / Understanding %Scope{}

Thinking out loud so that you can give me feedback if I got this correct.

I can use Scope both to make sure that the calls I’m making are properly scope and I can use it to have a few handy things at hand on each different LiveView.

So, if I take the example from the docs.

defmodule MyApp.Scope do
  defstruct [:current_user, :current_tenant, :locale]

  defimpl Ash.Scope.ToOpts do
    def get_actor(%{current_user: current_user}), do: {:ok, current_user}
    def get_tenant(%{current_tenant: current_tenant}), do: {:ok, current_tenant}
    def get_context(%{locale: locale}), do: {:ok, %{shared: %{locale: locale}}}
    # You typically configure tracers in config files
    # so this will typically return :error
    def get_tracer(_), do: :error

    # This should likely always return :error
    # unless you want a way to bypass authorization configured in your scope
    def get_authorize?(_), do: :error
  end
end

current_user and current_tenant should be the :id , correct? Everything else I want to “carry with me” I should put in the :context.

I’m asking this because I initially started putting together a project and I thought it would be best to put the whole %User{} and the %Tenant{} so that I can access it all easily, but I’m not sure that’s what the rest of the community is doing.

So, if I want to have the tenant_slug with me at all times, I should modify the above example like:


defmodule MyApp.Scope do
  defstruct [:current_user, :current_tenant, :locale, :slug]

  defimpl Ash.Scope.ToOpts do
    def get_actor(%{current_user: current_user}), do: {:ok, current_user}
    def get_tenant(%{current_tenant: current_tenant}), do: {:ok, current_tenant}

    def get_context(%{locale: locale, slug: slug}) do
      {:ok, %{shared: %{locale: locale, slug: slug}}}
    end

    def get_tracer(_), do: :error
    def get_authorize?(_), do: :error
  end
end

Most Liked

franckstifler

franckstifler

current_user and current_tenant are the stucts representing the actor and the tenant which should be applied.
for the tenant, you can implement the protocol Ash.ToTenant that would transform the tenant to what is needed (id in case of attribute multitenancy, or schema name in case of schema multintenancy)

defimpl Ash.ToTenant, for: MyApp.Tenant do
  def to_tenant(%{id: id}, _resource), do: id
end

Last Post!

RicoTrevisan

RicoTrevisan

Thanks! I’m going to give this a try.

I’ll set the scope with the %User{} and %Org{} instead of just the id to reference them. That way I can get rid of the slug – the %Org{} already has the slug inside of it. (I use the slug for navigation /orgs/{slug}/stuff)

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement