use crate::admin::admin_form_bridge::{AdminUiField, AdminUiModel};
#[derive(Debug, Clone)]
pub struct AdminModelConfig {
pub slug: &'static str,
pub model_name: &'static str,
pub table_name: &'static str,
pub primary_key: &'static str,
pub fields: Vec<AdminUiField>,
pub searchable_fields: Vec<&'static str>,
pub primary_status_field: Option<&'static str>,
pub ensure_table_sql: Option<&'static str>,
}
impl AdminModelConfig {
pub fn new(slug: &'static str, model_name: &'static str) -> Self {
Self {
slug,
model_name,
table_name: "",
primary_key: "id",
fields: Vec::new(),
searchable_fields: Vec::new(),
primary_status_field: None,
ensure_table_sql: None,
}
}
pub fn table(mut self, table_name: &'static str) -> Self {
self.table_name = table_name;
self
}
pub fn primary_key(mut self, primary_key: &'static str) -> Self {
self.primary_key = primary_key;
self
}
pub fn fields(mut self, fields: Vec<AdminUiField>) -> Self {
self.fields = fields;
self
}
pub fn searchable(mut self, searchable_fields: Vec<&'static str>) -> Self {
self.searchable_fields = searchable_fields;
self
}
pub fn status_field(mut self, name: &'static str) -> Self {
self.primary_status_field = Some(name);
self
}
pub fn ensure_sql(mut self, sql: &'static str) -> Self {
self.ensure_table_sql = Some(sql);
self
}
}
pub struct GeneratedAdminModel {
pub config: AdminModelConfig,
}
impl AdminUiModel for GeneratedAdminModel {
fn slug(&self) -> &'static str {
self.config.slug
}
fn model_name(&self) -> &'static str {
self.config.model_name
}
fn table_name(&self) -> &'static str {
self.config.table_name
}
fn primary_key(&self) -> &'static str {
self.config.primary_key
}
fn fields(&self) -> Vec<AdminUiField> {
self.config.fields.clone()
}
fn searchable_fields(&self) -> Vec<&'static str> {
self.config.searchable_fields.clone()
}
fn primary_status_field(&self) -> Option<&'static str> {
self.config.primary_status_field
}
fn ensure_table_sql(&self) -> Option<&'static str> {
self.config.ensure_table_sql
}
}
pub fn from_config(config: AdminModelConfig) -> Box<dyn AdminUiModel> {
Box::new(GeneratedAdminModel { config })
}