Skip to content

Commit

Permalink
add is_changed to ActiveModelTrait (#683)
Browse files Browse the repository at this point in the history
* add is_changed to ActiveModelTrait

* add test for `ActiveModelTrait::is_changed()`
  • Loading branch information
kirawi authored May 9, 2022
1 parent 415ec78 commit 23e9576
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 23e9576

Please sign in to comment.