add is_changed to ActiveModelTrait (#683)

* add is_changed to ActiveModelTrait

* add test for `ActiveModelTrait::is_changed()`
This commit is contained in:
Kirawi 2022-05-09 09:58:20 -04:00 committed by GitHub
parent 415ec78681
commit 23e95761ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -548,6 +548,12 @@ pub trait ActiveModelTrait: Clone + Debug {
Ok(am)
}
/// Returns `true` if any columns were changed.
fn is_changed(&self) -> bool {
<Self::Entity as EntityTrait>::Column::iter()
.any(|col| self.get(col).is_set() && !self.get(col).is_unchanged())
}
}
/// A Trait for overriding the ActiveModel behavior
@ -1088,4 +1094,13 @@ mod tests {
Ok(())
}
#[test]
fn test_active_model_is_changed() {
let mut fruit: fruit::ActiveModel = Default::default();
assert!(!fruit.is_changed());
fruit.set(fruit::Column::Name, "apple".into());
assert!(fruit.is_changed());
}
}