Skip to content

Instantly share code, notes, and snippets.

@Nondv
Last active April 18, 2017 21:21
Show Gist options
  • Select an option

  • Save Nondv/59b55e9412cff91c41c34465c0e5de08 to your computer and use it in GitHub Desktop.

Select an option

Save Nondv/59b55e9412cff91c41c34465c0e5de08 to your computer and use it in GitHub Desktop.
[Ruby] Using lambda instead of methods
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