Skip to main content

doido_model/
scope.rs

1//! Named, chainable query scopes (Rails `scope :published, -> { where(...) }`).
2//!
3//! A scope is any query transformation `FnOnce(Select<E>) -> Select<E>` — a free
4//! function or a closure. [`Scoped`] lets them chain fluently off a sea-orm
5//! `Select`, e.g. `Entity::find().scope(published).scope(recent)`.
6
7use crate::sea_orm::{EntityTrait, Select};
8
9/// Fluent application of named scopes to a query.
10pub trait Scoped: Sized {
11    /// Apply a scope (a query transformation).
12    fn scope(self, transform: impl FnOnce(Self) -> Self) -> Self {
13        transform(self)
14    }
15
16    /// Apply a scope only when `condition` holds (Rails conditional scope).
17    fn scope_if(self, condition: bool, transform: impl FnOnce(Self) -> Self) -> Self {
18        if condition {
19            transform(self)
20        } else {
21            self
22        }
23    }
24}
25
26impl<E: EntityTrait> Scoped for Select<E> {}