motion_data_wrapper
motion_data_wrapper copied to clipboard
(WIP) Added support for defining named scopes
Add support for defining named scopes on a model, providing class & relation-level methods for chain-ability.
- [x] Support for named scopes from class or relation
- [x] Do not allow redefining a scope
- [ ] Ensure scope is only defined on entity, not across all relations (currently Task.overdue pollutes into every model....)
class Task
scope :overdue, ->{ where "date < ?", NSDate.date }
end
Task.overdue
# => Relation
Task.overdue.count
# => 0
Task.where("title = ?", "My Task").overdue
# => Relation
Task.overdue.exists?
# => false
@jamonholmgren Love to get your feedback on this implementation :)
O_o
Your code is always annoyingly clean. Annoyingly because mine isn't.
I like it a lot. Have you been using this in production apps?
I have not used it yet, just worked on it in the last day or so. You can account my code quality improvement to guys like @21croissants and @pcreux who are stickers ;)
I'm thinking about adding some checks so you can't add a scope that would override an existing method, but that's about all I can think of at this point.