Skip to content

Instantly share code, notes, and snippets.

View zachdaniel's full-sized avatar

Zach Daniel zachdaniel

View GitHub Profile
@zachdaniel
zachdaniel / source_manipulation.ex
Last active August 17, 2022 19:13
Soource Manipulation
code_change =
CodeChange.new() # just one potential idea for what the multi-file tracking would be called
|> CodeChange.create_file(path, initial_contents)
|> CodeChange.update_file(path, [
Operation.add_defmodule(module_name),
Operation.add_use_to_module(module_name, thing_it_uses),
MyCustomOperations.do_my_special_thing(...)
], instructions_on_failure: "Add x to your module.")
# I need this for my use case because individual code changes may depend on the current state of other files
defmodule MyApp.Credo.NoAliasInResources do
@moduledoc "A custom lint rule that says there should be no aliases in resource files."
use Credo.Check,
category: :warning,
base_priority: :normal,
param_defaults: [],
explanations: [
check: """
For consistencies sake, we don't use aliases in resources.
defmodule MyApp.Types.StringLiteral do
@moduledoc "An ash type for a string that can only take a single value. Used in codegen to create literal values."
use Ash.Type
@constraints [
value: [
type: :string,
doc: "The string literal value",
required: true
]
defmodule MyApp.Types.Union do
@constraints [
types: [
type: {:custom, __MODULE__, :union_types, []},
doc: """
The types to be unioned, a map of a string name for the enum value to its configuration.
For example:
%{
defmodule MyApp.Extensions.Archive.Transformers.SetupArchival do
@moduledoc "Sets up the required resource structure for archival"
use Ash.Dsl.Transformer
@after_transformers [
Ash.Resource.Transformers.SetPrimaryActions
]
@before_transformers [
Ash.Resource.Transformers.DefaultAccept,
let habits = new Set();
const linksToHabits = {};
let days = [];
function compareName(a, b) {
if (a.file.name < b.file.name) {
return -1;
}
if (a.file.name > b.file.name) {
return 1;
}
@zachdaniel
zachdaniel / changed.ex
Created November 6, 2021 15:22
Check if an AshPhoenix Form is changed
defp changed?(form) do
is_changed?(form) ||
Enum.any?(form.forms, fn {_key, forms} ->
forms
|> List.wrap()
|> Enum.any?(&changed?/1)
end)
end
defp is_changed?(form) do
defmodule AshPhoenix.Components.DataTable2 do
use Surface.LiveComponent
import AshPhoenix.LiveView
alias AshPhoenix.Components.FilterBuilder2
data data, :list, default: []
data fields, :list
data apply_filter, :boolean, default: true
data filter, :any, default: []
defmodule AshExample.Ticket do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
authorizers: [
AshPolicyAuthorizer.Authorizer
],
extensions: [
AshJsonApi.Resource
]
defmodule AshExample.Representative do
use Ash.Resource, type: "representative", name: "representatives"
use AshPostgres, repo: AshExample.Repo
use AshJsonApi.JsonApiResource
use AshGraphql.GraphqlResource
use AshPolicyAccess
policies do