Class: Oversee::Resources::Show

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, resource_class:, resource_associations:, params:) ⇒ Show

Returns a new instance of Show.



12
13
14
15
16
17
18
19
# File 'app/components/oversee/resources/show.rb', line 12

def initialize(resource:, resource_class:, resource_associations:, params:)
  @resource = resource
  @resource_class = resource_class
  @resource_associations = resource_associations
  @params = params

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

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



8
9
10
# File 'app/components/oversee/resources/show.rb', line 8

def resource
  @resource
end

#resource_associationsObject (readonly)

Returns the value of attribute resource_associations.



10
11
12
# File 'app/components/oversee/resources/show.rb', line 10

def resource_associations
  @resource_associations
end

#resource_classObject (readonly)

Returns the value of attribute resource_class.



9
10
11
# File 'app/components/oversee/resources/show.rb', line 9

def resource_class
  @resource_class
end

Instance Method Details

#around_templateObject



21
22
23
# File 'app/components/oversee/resources/show.rb', line 21

def around_template
  render Oversee::Layout::Application.new { super }
end

#view_templateObject



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/components/oversee/resources/show.rb', line 25

def view_template
  render Oversee::Dashboard::Header.new(title: @resource_class.to_s.pluralize) do |h|
    h.left do
      h.separator
      div(class: "inline-flex items-center gap-1 text-sm bg-gray-100 text-gray-600 h-6 px-2") do
        render Phlex::Icons::Iconoir::Hashtag.new(class: "size-2.5 text-gray-500", stroke_width: 2)
        span { @resource.to_param }
      end
    end
    h.right do
      details(class: "relative inline-block") do
        summary(class: "cursor-pointer list-none") do
          div(class: "inline-flex items-center justify-center size-8 hover:bg-indigo-50 transition") do
            render Phlex::Icons::Iconoir::MoreHorizCircle.new(class: "size-4 text-gray-500")
          end
        end
        ul(
          class:
            "absolute p-2 mt-2 right-0 top-full min-w-40 overflow-hidden rounded-lg bg-white border border-b-2 border-gray-200/75 divide-y text-xs text-gray-500 font-medium"
        ) do
          li(class: "w-full") do
            button_to(helpers.resource_path(resource_class_name: @params[:resource_class_name]), method: :delete, data: { turbo_confirm: "Are you sure?" }, class: "p-1 hover:bg-gray-50 w-full flex items-center gap-2 transition duration-100") do
              div(class: "inline-flex items-center justify-center size-6 bg-gray-100") do
                render Phlex::Icons::Iconoir::Trash.new(class: "size-3 text-gray-500")
              end
              plain "Delete"
            end
          end
        end
      end
    end
  end

  hr(class: "my-4")

  # COLUMNS
  div(class: "flex flex-col gap-4") do
    @resource_class.columns_hash.each do |key, |
      next if @oversee_resource.foreign_keys.include?(key.to_s)
      value = @resource.send(key)
      datatype = ..type
      render Oversee::Field::Set.new(resource:, key:, value:, datatype:)
    end
  end


  # RICH TEXT Associations
  if !!rich_text_associations.length
  hr(class: "my-4")
    rich_text_associations.each do |association|

      # Remove the "rich_text_" prefix from the association name
      key = association[:name].to_s[10..].to_sym

      div do
        div(class: "space-y-4") do
          div(class:"flex items-center gap-2") do
            render Oversee::Field::Label.new(key: key.to_s.titleize, datatype: :rich_text)
            a(href: helpers.resources_path(resource_class_name: association[:class_name].to_s), class: "hover:text-blue-500") { render Phlex::Icons::Iconoir::ArrowUpRight.new(class: "size-3") }
          end

          div(id: dom_id(@resource, :"#{key.to_s}_row"), class: "flex items-center gap-2 mt-4") do
            render Oversee::Field::Display.new(resource:, key:, value: @resource.send(key).to_plain_text[..196], datatype: :rich_text)
          end
        end
      end
    end
  end

  # BELONGS_TO Associations
  if !!belongs_to_associations.length
    belongs_to_associations.each do |association|
      div(class: "py-6") do
        div(class: "space-y-4") do
          div(class:"flex items-center gap-2") do
            render Oversee::Field::Label.new(key: association[:name].to_s.titleize, datatype: :data)
            a(href: helpers.resources_path(resource_class_name: association[:class_name].to_s), class: "hover:text-blue-500") { render Phlex::Icons::Iconoir::ArrowUpRight.new(class: "size-3") }
          end

          foreign_key = association[:foreign_key]
          foreign_key_value = @resource[association[:foreign_key]]
          path = !!foreign_key_value ? helpers.resource_path(id: foreign_key_value, resource_class_name: association[:class_name]) : helpers.resources_path(resource_class_name: association[:class_name])

          div(id: dom_id(@resource, :"#{foreign_key}_row"), class: "flex items-center gap-2 mt-4") do
            render Oversee::Field::Display.new(resource:, key: foreign_key, value: foreign_key_value, datatype: :belongs_to, display_key: true)
            div(id: dom_id(@resource, :"#{foreign_key}_actions")) do
              a(href: path, class: "bg-gray-100 hover:bg-gray-200 text-gray-400 hover:text-blue-500 size-10 aspect-square inline-flex items-center justify-center transition-colors"){ render Phlex::Icons::Iconoir::ArrowUpRight.new(class: "size-4") }
            end
          end
        end
      end
    end
  end

  hr(class: "my-4")

  # HAS_MANY Associations
  if !!has_many_associations.length
    div(class: "flex flex-col gap-8") do
      has_many_associations.each do |association|
        associated_resources = @resource.send(association[:name])
        associated_resource_class = association[:class_name].constantize

        div(class: "space-y-4") do
          div(class:"flex items-center gap-2") do
            render Oversee::Field::Label.new(key: association[:name].to_s.titleize, datatype: :data)
            a(href: helpers.resources_path(resource_class_name: association[:class_name]), class: "hover:text-blue-500") { render Phlex::Icons::Iconoir::ArrowUpRight.new(class: "size-3") }
          end

          div(class: "bg-gray-50 p-2") do
            # turbo_frame_tag(
            #   dom_id(associated_resource_class, :table),
            #   src: helpers.resources_table_path(resources_table_params(association)),
            #   loading: :lazy,
            #   data: { turbo_stream: true }
            # ) do
            #   div(class: "h-20 flex items-center justify-center") { render Phlex::Icons::Iconoir::DatabaseSearch.new(class: "animate-pulse size-6 text-gray-600") }
            # end

            if associated_resources.present?
              div(class: "bg-white") do
                render Oversee::Resources::Table.new(
                  resources: associated_resources,
                  resource_class: associated_resource_class,
                  params: @params
                )
              end
            else
              p(class: "bg-gray-50 p-2 pr-4 flex gap-2 items-center text-xs") {
                render Phlex::Icons::Iconoir::DatabaseSearch.new(class: "size-3")
                plain "No #{association[:name].to_s.titleize.downcase} found"
              }
            end
          end
        end
      end
    end
  end
end