terrazzo 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f76fda78e25cb79116bd5f5f00b0b57e750503e3ebfdbee9a7bfe55dd8c17f34
4
- data.tar.gz: 5cf113e4e1bfdd17591a3e50e1e993668da1408041efcc28a7f17b4a1b0eac05
3
+ metadata.gz: 7c7c9b21f28f73f5cf332ac0d4b21d7eb32ae2461bfb2f52c5652eecd7642a8b
4
+ data.tar.gz: 7185c42a9d472e4d84ff7047a36a5468f761f272b9af5e0d9cb86930b130857f
5
5
  SHA512:
6
- metadata.gz: b60b58629158604c651402f6f920372f07146115b05f46613a2909be8af868f17005092bf7c84e288a4789724408e59150801fdc0b4f59c6575db1e05bb60f37
7
- data.tar.gz: 9f6342b35c22e98a56f465221ef5e0bcc8911fcf75ee33ebea146144232e84a85b3c199b4ae14ce5969868c983587ca2cb59660ba96e2d2123c9ee1cc71aec36
6
+ metadata.gz: f751974222e4ad11bd348dc07e3d8563c67454cf96d1b6faf3054b0b77f978dbba9dd1a10b16084b544e17799d8969df3e7ee50bf4485c0b1ce5849ab8f44716
7
+ data.tar.gz: b22856b13ec0ab28ba3e689cb92e99427311834f4e0e807e1a15506488832ce33f7c90a6d982b55d1c474ff4a1837394eed02d4a9bf3882164b9da988bb3b2e8
@@ -17,6 +17,7 @@ module Terrazzo
17
17
  prepend Terrazzo::UsesSuperglue::TemplateLookupOverride
18
18
 
19
19
  helper_method :namespace, :dashboard, :resource_name, :resource_class, :application_title, :terrazzo_page_identifier, :route_exists?
20
+ helper Terrazzo::CollectionActionsHelper
20
21
 
21
22
  def index
22
23
  search = Terrazzo::Search.new(scoped_resource, dashboard, params[:search])
@@ -129,9 +130,6 @@ module Terrazzo
129
130
 
130
131
  def find_resource(id)
131
132
  scoped_resource.find(id)
132
- rescue ActiveRecord::RecordNotFound
133
- # Support models that override to_param (e.g., slug-based URLs)
134
- scoped_resource.find_by!(slug: id)
135
133
  end
136
134
 
137
135
  def resource_params(action = nil)
@@ -0,0 +1,22 @@
1
+ module Terrazzo
2
+ module CollectionActionsHelper
3
+ def collection_item_actions(resource)
4
+ resource_dashboard = "#{resource.class.name}Dashboard".safe_constantize&.new
5
+ if resource_dashboard&.respond_to?(:collection_item_actions)
6
+ resource_dashboard.collection_item_actions(resource, self)
7
+ else
8
+ default_collection_item_actions(resource)
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def default_collection_item_actions(resource)
15
+ actions = []
16
+ actions << { label: "Show", url: polymorphic_path([namespace, resource]) } rescue nil
17
+ actions << { label: "Edit", url: edit_polymorphic_path([namespace, resource]) } rescue nil
18
+ actions << { label: "Destroy", url: polymorphic_path([namespace, resource]), method: "delete", confirm: "Are you sure?" } rescue nil
19
+ actions.compact
20
+ end
21
+ end
22
+ end
@@ -33,8 +33,7 @@ json.table do
33
33
  json.array! @resources do |resource|
34
34
  json.id resource.id
35
35
  json.showPath polymorphic_path([namespace, resource]) rescue nil
36
- json.editPath route_exists?(:edit) ? (edit_polymorphic_path([namespace, resource]) rescue nil) : nil
37
- json.deletePath route_exists?(:destroy) ? (polymorphic_path([namespace, resource]) rescue nil) : nil
36
+ json.collectionItemActions collection_item_actions(resource)
38
37
 
39
38
  json.cells do
40
39
  json.array! @page.attribute_names do |attr|
@@ -11,6 +11,9 @@ show_field_json = ->(json, field) do
11
11
  json.itemShowPaths(field.data.each_with_object({}) do |record, paths|
12
12
  paths[record.id.to_s] = polymorphic_path([namespace, record]) rescue nil
13
13
  end)
14
+ json.collectionItemActions(field.data.each_with_object({}) do |record, hash|
15
+ hash[record.id.to_s] = collection_item_actions(record)
16
+ end)
14
17
  else
15
18
  json.showPath polymorphic_path([namespace, field.data]) rescue nil
16
19
  end
@@ -11,9 +11,10 @@ import {
11
11
  } from "terrazzo/ui";
12
12
  import { Badge } from "terrazzo/ui";
13
13
  import { Button } from "terrazzo/ui";
14
+ import { CollectionItemActions } from "terrazzo/components";
14
15
  import { FieldRenderer } from "../FieldRenderer";
15
16
 
16
- export function ShowField({ value, itemShowPaths }) {
17
+ export function ShowField({ value, itemShowPaths, collectionItemActions }) {
17
18
  if (!value) return <span className="text-muted-foreground">None</span>;
18
19
 
19
20
  const { items, headers, total, initialLimit } = value;
@@ -46,6 +47,7 @@ export function ShowField({ value, itemShowPaths }) {
46
47
  {headers.map((header) =>
47
48
  <TableHead key={header.attribute}>{header.label}</TableHead>
48
49
  )}
50
+ {collectionItemActions && <TableHead></TableHead>}
49
51
  </TableRow>
50
52
  </TableHeader>
51
53
  <TableBody>
@@ -67,6 +69,11 @@ export function ShowField({ value, itemShowPaths }) {
67
69
  )}
68
70
  </TableCell>
69
71
  )}
72
+ {collectionItemActions && (
73
+ <TableCell>
74
+ <CollectionItemActions actions={collectionItemActions?.[String(item.id)]} />
75
+ </TableCell>
76
+ )}
70
77
  </TableRow>
71
78
  );
72
79
  })}
@@ -1,7 +1,7 @@
1
1
  import React, { useContext } from "react";
2
2
  import { useContent, NavigationContext } from "@thoughtbot/superglue";
3
3
 
4
- import { Layout, SearchBar, Pagination, SortableHeader } from "terrazzo/components";
4
+ import { Layout, SearchBar, Pagination, SortableHeader, CollectionItemActions } from "terrazzo/components";
5
5
  import { FieldRenderer } from "terrazzo/fields";
6
6
  import { Button, Table, TableHeader, TableBody, TableRow, TableHead, TableCell } from "terrazzo/ui";
7
7
 
@@ -66,39 +66,7 @@ export default function AdminIndex() {
66
66
  </TableCell>
67
67
  )}
68
68
  <TableCell>
69
- <div className="flex gap-1">
70
- {row.showPath &&
71
- <a href={row.showPath} data-sg-visit>
72
- <Button variant="ghost" size="sm">Show</Button>
73
- </a>
74
- }
75
- {row.editPath &&
76
- <a href={row.editPath} data-sg-visit>
77
- <Button variant="ghost" size="sm">Edit</Button>
78
- </a>
79
- }
80
- {row.deletePath &&
81
- <form
82
- action={row.deletePath}
83
- method="post"
84
- data-sg-visit
85
- style={{ display: "inline" }}
86
- onSubmit={(e) => {
87
- if (!window.confirm("Are you sure?")) e.preventDefault();
88
- }}>
89
-
90
- <input type="hidden" name="_method" value="delete" />
91
- <input
92
- type="hidden"
93
- name="authenticity_token"
94
- value={document.querySelector('meta[name="csrf-token"]')?.content ?? ""} />
95
-
96
- <Button type="submit" variant="ghost" size="sm" className="text-destructive">
97
- Delete
98
- </Button>
99
- </form>
100
- }
101
- </div>
69
+ <CollectionItemActions actions={row.collectionItemActions} />
102
70
  </TableCell>
103
71
  </TableRow>
104
72
  )}
@@ -44,7 +44,14 @@ module Terrazzo
44
44
  associated_class.all
45
45
  end
46
46
  pk = association_primary_key
47
- scope.map { |r| [display_name(r), r.public_send(pk).to_s] }
47
+ dashboard = associated_dashboard
48
+ scope.map { |r| [dashboard ? dashboard.display_resource(r) : display_name(r), r.public_send(pk).to_s] }
49
+ end
50
+
51
+ def associated_dashboard
52
+ "#{associated_class.name}Dashboard".constantize.new
53
+ rescue NameError
54
+ nil
48
55
  end
49
56
 
50
57
  def association_primary_key
@@ -1,3 +1,3 @@
1
1
  module Terrazzo
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terrazzo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Terrazzo Contributors
@@ -117,6 +117,7 @@ files:
117
117
  - LICENSE
118
118
  - Rakefile
119
119
  - app/controllers/terrazzo/application_controller.rb
120
+ - app/helpers/terrazzo/collection_actions_helper.rb
120
121
  - app/views/terrazzo/application/_navigation.json.props
121
122
  - app/views/terrazzo/application/edit.json.props
122
123
  - app/views/terrazzo/application/index.json.props