Last active
April 18, 2017 21:21
-
-
Save Nondv/59b55e9412cff91c41c34465c0e5de08 to your computer and use it in GitHub Desktop.
[Ruby] Using lambda instead of methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module CreatedAtScopes | |
| extend ActiveSupport::Concern | |
| # Lambda does not pollute a class namespace but do the job. | |
| tz_time_today = -> { Time.zone.now.beginning_of_day } | |
| included do | |
| scope :created_before, ->(time) { where(arel_table[:created_at].lt(time)) } | |
| scope :created_after, ->(time) { where(arel_table[:created_at].gt(time)) } | |
| scope :created_today, -> { created_after(tz_time_today[]) } | |
| scope :created_yesterday, -> { created_after(tz_time_today[] - 1.day).created_before(tz_time_today[]) } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment