add is_changed to ActiveModelTrait (#683)
* add is_changed to ActiveModelTrait * add test for `ActiveModelTrait::is_changed()`
This commit is contained in:
parent
415ec78681
commit
23e95761ca
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user