0.0
No commit activity in last 3 years
No release in over 3 years
A helper for building scopes that deal with moving windows.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

= 3.2.22
>= 0
>= 0
 Project Readme

Moving Window

A helper for building scopes that deal with moving windows.

Usage

require 'moving_window'

class Review < ActiveRecord::Base
  scope :recent, MovingWindow.scope { 6.months.ago }
end

Review.recent # Returns reviews from the last 6 months.

You can specify an end to the window with an array. The ordering does not matter:

scope :a_while_ago, MovingWindow.scope { [3.months.ago, 6.months.ago] }

By default, created_at is used. If you want to specify a different column:

scope :recently_published, MovingWindow.scope(:published_at) { 6.months.ago }

Dates in the future will also work.

Note: There's no need to worry about invoking the scope with a lambda. The timestamps will be re-evaluated on each call.

Manual Invocation

You'll find that .scope won't work outside of an active record model. Invoke things manually instead:

window = MovingWindow.new { 6.months.ago }
window.filter(Review, :column => :published_at)

Arel is fully supported:

window.filter(Review.published).limit(5)

Negation

You can find all records that lie outside of the moving window by negating it:

scope :not_recent, MovingWindow.scope { 6.months.ago }.not

If you're calling things manually:

window.filter(Review, :negate => true)