Skip to content

Commit e36eea4

Browse files
committed
Doc tests
1 parent bed74ca commit e36eea4

1 file changed

Lines changed: 84 additions & 1 deletion

File tree

src/entity/active_model.rs

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,90 @@ pub trait ActiveModelBehavior: ActiveModelTrait {
11051105
}
11061106
}
11071107

1108-
/// A Trait for any type that can be converted into an ActiveModel
1108+
/// A Trait for any type that can be converted into an `ActiveModel`,
1109+
/// can be derived using `DeriveIntoActiveModel`.
1110+
///
1111+
/// ## Derive Macro Example
1112+
///
1113+
/// ```rust
1114+
/// use sea_orm::DeriveIntoActiveModel;
1115+
/// mod fruit {
1116+
/// use sea_orm::entity::prelude::*;
1117+
/// #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
1118+
/// #[sea_orm(table_name = "fruit")]
1119+
/// pub struct Model {
1120+
/// #[sea_orm(primary_key)]
1121+
/// pub id: i32,
1122+
/// pub name: String,
1123+
/// pub cake_id: Option<i32>,
1124+
/// }
1125+
/// #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
1126+
/// pub enum Relation {}
1127+
/// impl ActiveModelBehavior for ActiveModel {}
1128+
/// }
1129+
///
1130+
/// #[derive(DeriveIntoActiveModel)]
1131+
/// #[sea_orm(active_model = "fruit::ActiveModel")]
1132+
/// struct NewFruit {
1133+
/// name: String,
1134+
/// // `id` and `cake_id` are omitted - they become `NotSet`
1135+
/// }
1136+
/// ```
1137+
///
1138+
/// ## `set(...)` - always set absent ActiveModel fields
1139+
///
1140+
/// ```rust
1141+
/// # mod fruit {
1142+
/// # use sea_orm::entity::prelude::*;
1143+
/// # #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
1144+
/// # #[sea_orm(table_name = "fruit")]
1145+
/// # pub struct Model {
1146+
/// # #[sea_orm(primary_key)]
1147+
/// # pub id: i32,
1148+
/// # pub name: String,
1149+
/// # pub cake_id: Option<i32>,
1150+
/// # }
1151+
/// # #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
1152+
/// # pub enum Relation {}
1153+
/// # impl ActiveModelBehavior for ActiveModel {}
1154+
/// # }
1155+
/// use sea_orm::DeriveIntoActiveModel;
1156+
///
1157+
/// #[derive(DeriveIntoActiveModel)]
1158+
/// #[sea_orm(active_model = "fruit::ActiveModel", set(cake_id = "None"))]
1159+
/// struct NewFruit {
1160+
/// name: String,
1161+
/// // `cake_id` is not on the struct, but will always be `Set(None)`
1162+
/// }
1163+
/// ```
1164+
///
1165+
/// ## `default = "expr"` - fallback for `Option<T>` struct fields
1166+
///
1167+
/// ```rust
1168+
/// # mod fruit {
1169+
/// # use sea_orm::entity::prelude::*;
1170+
/// # #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
1171+
/// # #[sea_orm(table_name = "fruit")]
1172+
/// # pub struct Model {
1173+
/// # #[sea_orm(primary_key)]
1174+
/// # pub id: i32,
1175+
/// # pub name: String,
1176+
/// # pub cake_id: Option<i32>,
1177+
/// # }
1178+
/// # #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
1179+
/// # pub enum Relation {}
1180+
/// # impl ActiveModelBehavior for ActiveModel {}
1181+
/// # }
1182+
/// use sea_orm::DeriveIntoActiveModel;
1183+
///
1184+
/// #[derive(DeriveIntoActiveModel)]
1185+
/// #[sea_orm(active_model = "fruit::ActiveModel")]
1186+
/// struct UpdateFruit {
1187+
/// /// `Some("Apple")` -> `Set("Apple")`, `None` ->`Set("Unnamed")`
1188+
/// #[sea_orm(default = "String::from(\"Unnamed\")")]
1189+
/// name: Option<String>,
1190+
/// }
1191+
/// ```
11091192
pub trait IntoActiveModel<A>
11101193
where
11111194
A: ActiveModelTrait,

0 commit comments

Comments
 (0)