motion_data_wrapper icon indicating copy to clipboard operation
motion_data_wrapper copied to clipboard

(WIP) Added support for defining named scopes

Open macfanatic opened this issue 10 years ago • 5 comments

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

macfanatic avatar Oct 24 '13 01:10 macfanatic

@jamonholmgren Love to get your feedback on this implementation :)

macfanatic avatar Oct 24 '13 01:10 macfanatic

O_o

jamonholmgren avatar Oct 24 '13 01:10 jamonholmgren

Your code is always annoyingly clean. Annoyingly because mine isn't.

jamonholmgren avatar Oct 24 '13 02:10 jamonholmgren

I like it a lot. Have you been using this in production apps?

jamonholmgren avatar Oct 24 '13 02:10 jamonholmgren

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.

macfanatic avatar Oct 24 '13 10:10 macfanatic