Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Added support for defining named scopes #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

macfanatic
Copy link
Owner

Add support for defining named scopes on a model, providing class & relation-level methods for chain-ability.

  • Support for named scopes from class or relation
  • 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
Copy link
Owner Author

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

@jamonholmgren
Copy link

O_o

@jamonholmgren
Copy link

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

@jamonholmgren
Copy link

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

@macfanatic
Copy link
Owner Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants