Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select / typeahead combo for foreign key field #222

Merged
merged 23 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
94d568f
add optional typeahead component for selecting relationship
mkschell Oct 29, 2024
821b7f6
static files changes re: relationship picker
mkschell Oct 30, 2024
f66aebb
remove extraneous attribute name
mkschell Oct 30, 2024
621c749
remove custom expression from solution
mkschell Nov 1, 2024
e03e368
some renaming inside relationship_field
mkschell Nov 1, 2024
eaa9377
inline datalayer position strategies into calculation
mkschell Nov 1, 2024
1c8958a
fix policy issue in relationship field component
mkschell Nov 28, 2024
1d38087
improve relationship field stuff in form component
mkschell Jan 11, 2025
1c22330
add some label_fields to dev app resources
mkschell Jan 11, 2025
ad04ccd
to_name fix in helpers
mkschell Jan 11, 2025
78220d3
fix relationship_field to handle Ash.Page.Offset result from read query
mkschell Jan 13, 2025
e231479
expand to_name helper beyond Attributes
mkschell Jan 29, 2025
2b363d4
flag :read forms for potential relationship_field component
mkschell Jan 29, 2025
4cecf1e
remove IO.inspect
mkschell Feb 4, 2025
02097a5
update formatter
mkschell Feb 4, 2025
a04fb1d
update Resource DSL docs
mkschell Feb 4, 2025
2085b4c
code style stuff to make credo happier
mkschell Feb 4, 2025
8438c81
escape user input before regex replace
mkschell Feb 4, 2025
98d1300
Raise default relationship_select_max_items from 18 to 50
mkschell Feb 7, 2025
81e262e
simplify typeahead results sorting calculation
mkschell Feb 9, 2025
2f2250a
remove check for existence of string_position function
mkschell Feb 12, 2025
8cfe174
bump dependencies
mkschell Feb 13, 2025
e763046
update demo resources to showcase a one_to_many relationship
mkschell Feb 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ spark_locals_without_parens = [
field: 2,
format_fields: 1,
generic_actions: 1,
label_field: 1,
name: 1,
polymorphic_actions: 1,
polymorphic_tables: 1,
read_actions: 1,
relationship_display_fields: 1,
relationship_select_max_items: 1,
resource_group: 1,
resource_group_labels: 1,
show?: 1,
Expand Down
43 changes: 40 additions & 3 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Hooks.JsonEditor = {
target.dispatchEvent(
new Event("change", { bubbles: true, target: this.el.name }),
);
} catch (_e) {}
} catch (_e) { }
},
onChange: () => {
try {
Expand All @@ -37,7 +37,7 @@ Hooks.JsonEditor = {
target.dispatchEvent(
new Event("change", { bubbles: true, target: this.el.name }),
);
} catch (_e) {}
} catch (_e) { }
},
onModeChange: (newMode) => {
hook.mode = newMode;
Expand All @@ -63,7 +63,7 @@ Hooks.JsonEditorSource = {
} else {
}
}
} catch (_e) {}
} catch (_e) { }
},
};

Expand Down Expand Up @@ -163,6 +163,43 @@ Hooks.MaintainAttrs = {
},
};

Hooks.Typeahead = {
mounted() {
const target_id = this.el.getAttribute("data-target-id");
const target_el = document.getElementById(target_id);

switch (this.el.tagName) {
case "INPUT":
this.el.addEventListener("keydown", e => {
if (e.key === "Enter") {
e.preventDefault();
}
});
this.el.addEventListener("keyup", e => {
switch (e.key) {
case "Enter":
case "Escape":
this.el.blur();
window.setTimeout(function () { target_el.dispatchEvent(new Event("input", { bubbles: true })) }, 750);
break;
}
});
break;

case "LI":
this.el.addEventListener("click", e => {
window.setTimeout(function () { target_el.dispatchEvent(new Event("input", { bubbles: true })) }, 750);
});
break;
}
},
updated() {
if (this.el.tagName === "INPUT" && this.el.name.match(/suggest$/) && this.el.value.length === 0) {
this.el.focus();
}
}
};

function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
defmodule Demo.Repo.Migrations.MigrateResourcesExtensions1 do
@moduledoc """
Installs any extensions that are mentioned in the repo's `installed_extensions/0` callback

This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
execute("ALTER FUNCTION ash_raise_error(jsonb) STABLE;")
execute("ALTER FUNCTION ash_raise_error(jsonb, ANYCOMPATIBLE) STABLE")

execute("""
CREATE OR REPLACE FUNCTION uuid_generate_v7()
RETURNS UUID
AS $$
DECLARE
timestamp TIMESTAMPTZ;
microseconds INT;
BEGIN
timestamp = clock_timestamp();
microseconds = (cast(extract(microseconds FROM timestamp)::INT - (floor(extract(milliseconds FROM timestamp))::INT * 1000) AS DOUBLE PRECISION) * 4.096)::INT;

RETURN encode(
set_byte(
set_byte(
overlay(uuid_send(gen_random_uuid()) placing substring(int8send(floor(extract(epoch FROM timestamp) * 1000)::BIGINT) FROM 3) FROM 1 FOR 6
),
6, (b'0111' || (microseconds >> 8)::bit(4))::bit(8)::int
),
7, microseconds::bit(8)::int
),
'hex')::UUID;
END
$$
LANGUAGE PLPGSQL
SET search_path = ''
VOLATILE;
""")

execute("""
CREATE OR REPLACE FUNCTION timestamp_from_uuid_v7(_uuid uuid)
RETURNS TIMESTAMP WITHOUT TIME ZONE
AS $$
SELECT to_timestamp(('x0000' || substr(_uuid::TEXT, 1, 8) || substr(_uuid::TEXT, 10, 4))::BIT(64)::BIGINT::NUMERIC / 1000);
$$
LANGUAGE SQL
SET search_path = ''
IMMUTABLE PARALLEL SAFE STRICT;
""")
end

def down do
# Uncomment this if you actually want to uninstall the extensions
# when this migration is rolled back:
execute("DROP FUNCTION IF EXISTS uuid_generate_v7(), timestamp_from_uuid_v7(uuid)")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Demo.Repo.Migrations.MigrateResources8 do
@moduledoc """
Updates resources based on their most recent snapshots.

This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
alter table(:users) do
add(
:organization_id,
references(:organizations,
column: :id,
name: "users_organization_id_fkey",
type: :uuid,
prefix: "public"
)
)
end
end

def down do
drop(constraint(:users, "users_organization_id_fkey"))

alter table(:users) do
remove(:organization_id)
end
end
end
10 changes: 6 additions & 4 deletions dev/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ defmodule Demo.Seeder do
})
end

@spec insert_representative!(String.t(), String.t()) :: Representative.t()
def insert_representative!(first_name, last_name) do
@spec insert_representative!(String.t(), String.t(), Organization.t()) :: Representative.t()
def insert_representative!(first_name, last_name, organization \\ nil) do
Seed.seed!(Representative, %{
first_name: first_name,
last_name: last_name,
representative: true
representative: true,
organization_id: organization && organization.id
})
end

Expand Down Expand Up @@ -87,6 +88,7 @@ Demo.Seeder.insert_admin!("Super", "Admin");
org = Demo.Seeder.insert_organization!("Ash Project");
Demo.Seeder.insert_user!("Alice", "Courtney", "Lorem ipsum dolor sit amet", "Duis aute irure dolor in reprehenderit in voluptate velit esse", "123456");
bob = Demo.Seeder.insert_customer!("Bob", "Maclean");
carol = Demo.Seeder.insert_representative!("Carol", "White");
carol = Demo.Seeder.insert_representative!("Carol", "White", org);
rasha = Demo.Seeder.insert_representative!("Rasha", "Khan");

Demo.Seeder.insert_ticket!("Lorem ipsum", "Duis aute irure dolor in reprehenderit in voluptate", bob, carol, org);
4 changes: 2 additions & 2 deletions dev/resource_snapshots/repo/extensions.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"ash_functions_version": 5,
"installed": [
"uuid-ossp",
"pg_trgm",
"citext",
"ash-functions"
],
"ash_functions_version": 3
]
}
Loading