Class: Monolith::TablesController::Table

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/monolith/tables_controller.rb

Overview

Inline ActiveModel-like object

Constant Summary collapse

EXCLUDED =
%w[schema_migrations ar_internal_metadata].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Table

—- instance —-



59
60
61
# File 'app/controllers/monolith/tables_controller.rb', line 59

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



45
46
47
# File 'app/controllers/monolith/tables_controller.rb', line 45

def name
  @name
end

Class Method Details

.allObject



49
50
51
# File 'app/controllers/monolith/tables_controller.rb', line 49

def self.all
  tables.map { |t| new(t) }
end

.find(name) ⇒ Object



53
54
55
56
# File 'app/controllers/monolith/tables_controller.rb', line 53

def self.find(name)
  return nil unless tables.include?(name)
  new(name)
end

Instance Method Details

#columnsObject



65
66
67
# File 'app/controllers/monolith/tables_controller.rb', line 65

def columns
  @columns ||= conn.columns(name).map(&:name)
end

#primary_keyObject



69
70
71
# File 'app/controllers/monolith/tables_controller.rb', line 69

def primary_key
  @primary_key ||= conn.primary_key(name)
end

#rows(page:, per_page:) ⇒ Object

Returns { data: [Hash], total: Integer }



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/monolith/tables_controller.rb', line 74

def rows(page:, per_page:)
  offset = (page - 1) * per_page
  total  = conn.exec_query("SELECT COUNT(*) AS c FROM #{qtn(name)}").first["c"]

  order_sql = primary_key ? "ORDER BY #{qcn(primary_key)}" : ""
  sql = "    SELECT *\n    FROM \#{qtn(name)}\n    \#{order_sql}\n    LIMIT \#{per_page.to_i} OFFSET \#{offset.to_i}\n  SQL\n\n  data = conn.exec_query(sql).to_a\n  { data: data, total: total }\nend\n"

#to_paramObject



63
# File 'app/controllers/monolith/tables_controller.rb', line 63

def to_param = name