Class: TkInspect::Inspector::ArRelationInspectorComponent

Inherits:
TkComponent::Base
  • Object
show all
Defined in:
lib/tk_inspect_rails/inspector/ar_relation_inspector_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#inspectorObject

Returns the value of attribute inspector.



4
5
6
# File 'lib/tk_inspect_rails/inspector/ar_relation_inspector_component.rb', line 4

def inspector
  @inspector
end

Instance Method Details

#items_for_path(path = []) ⇒ Object



55
56
57
# File 'lib/tk_inspect_rails/inspector/ar_relation_inspector_component.rb', line 55

def items_for_path(path = [])
  @relation.records.map(&:attributes)
end

#render(p, parent_component) ⇒ Object



6
7
8
9
10
11
12
13
14
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
# File 'lib/tk_inspect_rails/inspector/ar_relation_inspector_component.rb', line 6

def render(p, parent_component)
  @relation = eval(inspector.expression, inspector.inspected_binding)
  @count = @relation.size if @relation.loaded?
  p.vframe(sticky: 'nsew', x_flex: 1, y_flex: 1) do |f|
    f.hframe(sticky: 'wne', padding: '8', x_flex: 1) do |hf|
      hf.label(text: "Relation Class")
      hf.entry(value: @relation.class.to_s, state: 'disabled', x_flex: 1)
      hf.button(text: "Browse...", on_click: ->(e) { inspector.browse_class(@relation.class.to_s) })
      hf.label(text: "Model Class")
      hf.entry(value: @relation.klass.to_s, state: 'disabled', x_flex: 1)
      hf.button(text: "Browse...", on_click: ->(e) { inspector.browse_class(@relation.klass.to_s) })
      hf.label(text: "Table")
      hf.entry(value: @relation.table.name, state: 'disabled', x_flex: 1)
    end
    f.vframe(sticky: 'wne', padding: '8', x_flex: 1) do |vf|
      vf.label(text: "SQL expression", sticky: 'w')
      vf.text(value: @relation.to_sql, width: 20, height: 3, x_flex: 1, sticky: 'new')
    end
    if @count.present?
      f.hframe(sticky: 'w', padding: '8', x_flex: 1) do |hf|
        hf.label(text: "Object count")
        hf.entry(value: @count, state: 'disabled')
      end
    end
    f.hframe(sticky: 'sw', padding: '8', x_flex: 1) do |hf|
      if @relation.loaded?
        @table = f.insert_component(
          TkComponent::TableViewComponent,
          self,
          data_source: self,
          columns: @relation.first.attributes.map do |k, v|
            { key: k, text: k }
          end,
          sticky: 'nsew', x_flex: 1, y_flex: 1)
      else
        hf.label(text: "Contents not loaded")
        hf.button(text: "Load contents") do |b|
          b.on_click ->(e) { @relation.load; regenerate }
        end
        if @count.blank?
          hf.button(text: "Get object count") do |b|
            b.on_click ->(e) { @count = @relation.count; regenerate }
          end
        end
      end
    end
  end
end