Skip to content

[QUESTION] link to other resources from index #336

@Sgoettschkes

Description

@Sgoettschkes

I was wondering if there is an easier way to link to another resource from the index page. My use cases are:

Relationship of user to company (1:n), and in the user list, I have a column showing the company name (by using a customer value function and a custom_index_query which preloads the company. Now I'd like to not only show the company name, but provide an easy way to navigate to this company.

The way I was able to pull this of:

This is the definition of the index field:

company: %{
        value: fn user ->
          Phoenix.HTML.raw(
            "<a href=\"#{url_for_resource(App.Models.Company, user.company_id)}\">#{user.company.name}</a>"
          )
        end
      }

and this is my hacked together url_for_resource function:

defp url_for_resource(object_type, object_id) do
    resource_path =
      nil
      |> Kaffy.Utils.full_resources()
      |> Enum.reduce_while(nil, fn {section_id, section}, _acc ->
        resource =
          Enum.reduce_while(Keyword.get(section, :resources), nil, fn {resource_id, resource},
                                                                      _acc ->
            resource_string = resource |> Keyword.get(:schema) |> to_string()

            if "Elixir." <> object_type == resource_string do
              {:halt, resource_id}
            else
              {:cont, nil}
            end
          end)

        case resource do
          nil -> {:cont, nil}
          resource_id -> {:halt, "#{section_id}/#{resource_id}"}
        end
      end)

    case resource_path do
      nil -> "#"
      path -> "/app/admin_tool/#{path}/#{object_id}"
    end
  end

Is there an easier way to do this? Something like a Util taking the model module and returning a path?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions