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

Add some magic #3

Open
scottjacobsen opened this issue Feb 14, 2017 · 3 comments
Open

Add some magic #3

scottjacobsen opened this issue Feb 14, 2017 · 3 comments

Comments

@scottjacobsen
Copy link
Member

What do you think about adding some magic: - inferring the table name from the scope name. created_between, and updated_between, foo_between would all work given columns named created_at, updated_at, and foo_at?

@teeparham
Copy link
Member

teeparham commented Feb 14, 2017

Good idea. There are 2 ways to do this:

  1. method_missing on the class when the method name matches _between & other patterns. I think there be dragons with that approach.

  2. Define a class method that takes a column name as an argument & then defines explicit scopes dynamically. Something like:

class Doge
  include Scopy::DateScopes

  date_scopes_for :updated_at

which would generate something like

  scope :updated_since, -> (date) { where(...) }
  scope :updated_before, -> (date) { where(...) }
  scope :updated_between, -> (first, last) { ... }
  # etc

Option 2 is more explicit in the model code and probably easier to write & understand, too.

@manojmj92
Copy link

@teeparham can I take a dig at this with option 2?

@teeparham
Copy link
Member

@manojmj92 yes!

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

No branches or pull requests

3 participants