Class: Oversee::Resources::Table

Inherits:
Base
  • Object
show all
Includes:
Phlex::Rails::Helpers::TurboFrameTag
Defined in:
app/components/oversee/resources/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(resources:, resource_class:, params:, **options) ⇒ Table



6
7
8
9
10
11
12
13
# File 'app/components/oversee/resources/table.rb', line 6

def initialize(resources:, resource_class:, params:, **options)
  @resources = resources
  @resource_class = resource_class || resources.first.class
  @params = params
  @options = options

  @oversee_resource = Oversee::Resource.new(resource_class: @resource_class)
end

Instance Method Details

#view_templateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/components/oversee/resources/table.rb', line 15

def view_template
  turbo_frame_tag dom_id(@resource_class, :table), target: "_top" do
    render Oversee::Table.new do |table|
      table.head do |head|
        head.row do
          th(scope: "col", class: "px-4 py-3 text-left text-xs text-gray-900 uppercase")
          # Attributes
          @resource_class.columns_hash.each do |key, |
            next if @oversee_resource.foreign_keys.include?(key.to_s)
            th(scope: "col", class: "text-left text-xs text-gray-900 uppercase whitespace-nowrap hover:bg-gray-50 transition relative") do
              a(
                href:
                  (
                    helpers.resources_path(
                      resource: @params[:resource],
                      sort_attribute: key,
                      sort_direction:
                        @params[:sort_direction] == "asc" ? :desc : :asc
                    )
                  ),
                target: "_top",
                class: "px-4 py-3 flex items-center justify-between gap-2 w-full h-full hover:text-gray-900 transition-colors"
              ) do
                render Oversee::Field::Label.new(key: key, datatype: ..type)
                render Phlex::Icons::Iconoir::ArrowSeparateVertical.new(class: "size-3 text-gray-500", stroke_width: 2.5)
              end
            end
          end

          # Associations
          resource_associations.each do |association|
            th(scope: "col", class: "text-left text-xs text-gray-900 uppercase whitespace-nowrap hover:bg-gray-50 transition relative") do
              span(
                class: "px-4 py-3 flex items-center justify-between gap-2 w-full h-full hover:text-gray-900 transition-colors"
              ) do
                render Oversee::Field::Label.new(key: association.name, datatype: nil)
              end
            end
          end
        end
      end

      table.body do |body|
        @resources.each do |resource|

          body.row do |row|
            td do
              div(class: "flex space-x-2 mx-4") do
                a(
                  href:
                    (
                      helpers.resource_path(
                        resource.id,
                        resource_class_name: @params[:resource_class_name]
                      )
                    ),
                  data: { turbo_stream: true },
                  class: "size-6 bg-gray-100 inline-flex items-center justify-center hover:bg-gray-200 group"
                ) { render Phlex::Icons::Iconoir::Eye.new(class: "size-4 text-gray-500") }
              end
            end

            @resource_class.columns_hash.each do |key, |
              next if @oversee_resource.foreign_keys.include?(key.to_s)
              row.data do
                div(class: "max-w-96") do
                  render Oversee::Field::Value.new(key:, datatype: ..type, value: resource.send(key), for_table: true)
                end
              end
            end

            resource_associations.each do |association|
              foreign_id = resource.send(association.foreign_key)
              resource_class_name = association.class_name

              path = !!foreign_id ? helpers.resource_path(id: foreign_id, resource_class_name:) : helpers.resources_path(resource_class_name: resource_class_name)

              row.data do
                div(class: "max-w-96") do
                  a(
                    href: path,
                    class:"px-2 py-1 bg-gray-100 text-gray-500 text-sm flex items-center justify-between gap-2 hover:bg-gray-200 hover:text-gray-900 min-w-20") do
                    span { foreign_id.presence || "N/A" }
                    render Phlex::Icons::Iconoir::ArrowRight.new(class: "size-3")
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end