Module: Tina4::Crud
- Defined in:
- lib/tina4/crud.rb
Overview
Crud — Auto-generate a complete HTML CRUD interface from a SQL query or ORM model.
Usage:
Tina4.get "/admin/users" do |request, response|
response.html(Tina4::Crud.to_crud(request, {
sql: "SELECT id, name, email FROM users",
title: "User Management",
primary_key: "id"
}))
end
Or with an ORM model class:
Tina4.get "/admin/users" do |request, response|
response.html(Tina4::Crud.to_crud(request, {
model: User,
title: "User Management"
}))
end
Class Method Summary collapse
-
.generate_form(fields, action: "/", method: "POST", table_name: "data") ⇒ Object
Generate an HTML form from a field definition array.
-
.generate_table(records, table_name: "data", primary_key: "id", editable: true) ⇒ Object
Generate an HTML table from an array of record hashes.
-
.to_crud(request, options = {}) ⇒ String
Generate a complete CRUD HTML interface: searchable/paginated table with create, edit, and delete modals.
Class Method Details
.generate_form(fields, action: "/", method: "POST", table_name: "data") ⇒ Object
Generate an HTML form from a field definition array.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/tina4/crud.rb', line 140 def generate_form(fields, action: "/", method: "POST", table_name: "data") html = "<form action=\"#{action}\" method=\"#{method}\" class=\"needs-validation\" novalidate>" html += "<input type=\"hidden\" name=\"_method\" value=\"#{method}\">" if %w[PUT PATCH DELETE].include?(method.upcase) fields.each do |field| name = field[:name] type = field[:type] || :string label = field[:label] || name.to_s.capitalize value = field[:value] || "" required = field[:required] || false html += "<div class=\"mb-3\">" html += "<label for=\"#{name}\" class=\"form-label\">#{label}</label>" case type.to_sym when :text html += "<textarea class=\"form-control\" id=\"#{name}\" name=\"#{name}\" #{'required' if required}>#{value}</textarea>" when :boolean checked = value ? "checked" : "" html += "<div class=\"form-check\">" html += "<input class=\"form-check-input\" type=\"checkbox\" id=\"#{name}\" name=\"#{name}\" #{checked}>" html += "</div>" when :select html += "<select class=\"form-select\" id=\"#{name}\" name=\"#{name}\" #{'required' if required}>" (field[:options] || []).each do |opt| selected = opt[:value].to_s == value.to_s ? "selected" : "" html += "<option value=\"#{opt[:value]}\" #{selected}>#{opt[:label]}</option>" end html += "</select>" when :date html += "<input type=\"date\" class=\"form-control\" id=\"#{name}\" name=\"#{name}\" value=\"#{value}\" #{'required' if required}>" when :integer, :number html += "<input type=\"number\" class=\"form-control\" id=\"#{name}\" name=\"#{name}\" value=\"#{value}\" #{'required' if required}>" else html += "<input type=\"text\" class=\"form-control\" id=\"#{name}\" name=\"#{name}\" value=\"#{value}\" #{'required' if required}>" end html += "</div>" end html += "<button type=\"submit\" class=\"btn btn-primary\">Submit</button>" html += "</form>" html end |
.generate_table(records, table_name: "data", primary_key: "id", editable: true) ⇒ Object
Generate an HTML table from an array of record hashes.
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 |
# File 'lib/tina4/crud.rb', line 93 def generate_table(records, table_name: "data", primary_key: "id", editable: true) return "<p>No records found.</p>" if records.nil? || records.empty? columns = records.first.keys html = <<~HTML <div class="table-responsive"> <table class="table table-striped table-hover" id="crud-#{table_name}"> <thead class="table-dark"><tr> HTML columns.each do |col| html += "<th>#{col}</th>" end html += "<th>Actions</th>" if editable html += "</tr></thead><tbody>" records.each do |row| pk_value = row[primary_key.to_sym] || row[primary_key.to_s] html += "<tr data-id=\"#{pk_value}\">" columns.each do |col| value = row[col] if editable html += "<td contenteditable=\"true\" data-field=\"#{col}\">#{value}</td>" else html += "<td>#{value}</td>" end end if editable html += "<td>" html += "<button class=\"btn btn-sm btn-primary me-1\" onclick=\"crudSave('#{table_name}', '#{pk_value}')\">Save</button>" html += "<button class=\"btn btn-sm btn-danger\" onclick=\"crudDelete('#{table_name}', '#{pk_value}')\">Delete</button>" html += "</td>" end html += "</tr>" end html += "</tbody></table></div>" if editable html += inline_crud_javascript(table_name) end html end |
.to_crud(request, options = {}) ⇒ String
Generate a complete CRUD HTML interface: searchable/paginated table with create, edit, and delete modals. Also registers the supporting REST API routes on first call per table.
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 |
# File 'lib/tina4/crud.rb', line 38 def to_crud(request, = {}) sql = [:sql] model = [:model] title = [:title] || "CRUD" pk = [:primary_key] || "id" prefix = [:prefix] || "/api" limit = ([:limit] || 10).to_i # Determine table name and columns from SQL or model if model table_name = model.table_name.to_s pk = (model.primary_key_field || :id).to_s columns = model.field_definitions.keys.map(&:to_s) elsif sql table_name = extract_table_name(sql) columns = extract_columns(sql) else raise ArgumentError, "Crud.to_crud requires either :sql or :model option" end # Parse request params for pagination, search, and sorting query_params = request.respond_to?(:query) ? request.query : {} page = [(query_params["page"] || 1).to_i, 1].max search = query_params["search"].to_s.strip sort_col = query_params["sort"] || pk sort_dir = query_params["sort_dir"] == "desc" ? "desc" : "asc" offset = (page - 1) * limit # Build the data query if model records, total = fetch_model_data(model, search: search, sort: sort_col, sort_dir: sort_dir, limit: limit, offset: offset) else records, total = fetch_sql_data(sql, search: search, sort: sort_col, sort_dir: sort_dir, limit: limit, offset: offset) end total_pages = total > 0 ? (total.to_f / limit).ceil : 1 api_path = "#{prefix}/#{table_name}" # Register supporting CRUD API routes (idempotent) register_crud_routes(model, table_name, pk, prefix) unless crud_routes_registered?(table_name, prefix) # Build the HTML build_crud_html( title: title, table_name: table_name, pk: pk, columns: columns, records: records, page: page, total_pages: total_pages, total: total, limit: limit, search: search, sort_col: sort_col, sort_dir: sort_dir, api_path: api_path, request_path: request.path ) end |