britton-jb / belial

Shared Elixir Code Repo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Belial

This library is a random assortments of boilerplate removers or other pieces of standardization I like in my Elixir apps. I'd actively advise other folks to stear clear.

It was also pulled out of an existing project, and shows in some of it's organization and current lack of internal coherence. I may address that at some point in the future, but it currently isn't a high priority for me, where a successful extraction was.

Using the modules

There are a few key modules with using macros:

  • Belial.Schema - adds a number of useful defaults to each ecto schema
  • Belial.MultiSchemaContext - this one adds default CRUD actions etc.
  • Belial.Web.Controllers.Admin - adds admin controller and view actions for a given Belial compliant context
  • Belial.Web.Controllers.Api - adds REST controller support with JSON rendering for a given Belial compliant context
  • Belial.Web.Absinthe.Schema - adds a sane set of default schema supports for an Absinthe implementation
  • Belial.Web.Absinthe.Notation - adds GraphqQL support for an individual schema, including basic CRUD actions
  • Belial.Web.Absinthe.FieldTypeMapperHelper - assists in mapping ecto to absinthe types, including custom, appropriately

Installation

Currently only available via GitHub install.

Configuration

config :belial,
  admin_view_router: MyAppWeb.Router.Helpers,
  field_type_mapper: MyApp.Belial.FieldTypeMapper, # optional
  modifiable_fields: MyApp.Belial.ChangeValueForField # optional

config :scrivener_html,
  routes_helper: MyAppWeb.Router.Helpers,
  view_style: :bootstrap

config :speakeasy,
  user_key: :current_user,
  authn_error_message: :unauthenticated
def router do
  quote do
    ...
    import Phoenix.LiveView.Router
  end
end
  socket "/live", Phoenix.LiveView.Socket,
    websocket: [connect_info: [session: @session_options]]
   # Where @session_options are the options given to plug Plug.Session extracted to a module attribute.

Add LiveView NPM dependencies in your assets/package.json. For a regular project, do:

{
  "dependencies": {
    "phoenix": "file:../deps/phoenix",
    "phoenix_html": "file:../deps/phoenix_html",
    "phoenix_live_view": "file:../deps/phoenix_live_view"
  }
}

For umbrella installs see https://hexdocs.pm/phoenix_live_view/installation.html

Ensure you have placed a CSRF meta tag inside the tag in your layout (lib/my_app_web/templates/layout/app.html.eex) like so:

<%= csrf_meta_tag() %>

Enable connecting to a LiveView socket in your app.js file.

// assets/js/app.js
import { Socket } from "phoenix"
import { LiveSocket } from "phoenix_live_view"

Hooks.AdminSearch = {
  mounted() {
    const adminSearch = document.getElementById("adminSearch")

    adminSearch.addEventListener("input", e => {
      const val = adminSearch.value;
      const opts = document.getElementById("matches").childNodes;
      opts.forEach(element => {
        if (element.value == val) { this.pushEvent("search", { "q": val }) }
      })
    })
  }
}

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, { hooks: Hooks, params: { _csrf_token: csrfToken } })

liveSocket.connect()

Question marks

Could modifiable_fields be handled in the same way I handle field_type_mapper? - Yes, yes it could. It could also be handled by an application config by combining?

mapping = Keyword.merge(
  Belial.Web.Absinthe.FieldTypeMapperHelper.mapping(),
  Application.get_env(:belial, :field_mapping_module_fn_tuple)
)
defimpl Belial.Web.Absinthe.FieldTypeMapper, for: Atom do
  Belial.Web.Absinthe.FieldTypeMapperHelper.convert_type_fragment(mapping)
end

Remove SingularContext, and rename MultipleContext to Context?

Add a default mapping for utc datetime?

For other contributors/future

Add belial multiple context callbacks to allow compliant APIs with some compiler help.

Do the JS includes for admin search the cool way that phx does?

Dynamically define enums from ecto enums, along with the types?

Add to_struct - based on @derive?, also absinthe schema based on a derive alike?

The handful of FIXMEs

String to atom noise reduction in absinthe

Support only/except options?

Move assoc_constraint, change_value_for_field, default_value, errors_on, exclusive_belongs_to, modifiable_fields, required_fields, and unique_constraint tests into a changeset context and dir?

About

Shared Elixir Code Repo


Languages

Language:Elixir 96.7%Language:HTML 3.3%