Skip to content

Instantly share code, notes, and snippets.

@kivanio
Forked from ekampf/created_at_scopes.rb
Created June 23, 2014 00:22
Show Gist options
  • Save kivanio/193a716a38955191ec4a to your computer and use it in GitHub Desktop.
Save kivanio/193a716a38955191ec4a to your computer and use it in GitHub Desktop.
module Concerns
module CreatedAtScopes
extend ActiveSupport::Concern
included do
%w(created_at updated_at).each do |prop|
verb = prop.gsub('_at', '')
scope "#{verb}_last_h", lambda { where("#{prop} >= ?", (Time.now-1.hour).to_s(:db)) }
scope "#{verb}_last_6h", lambda { where("#{prop} >= ?", (Time.now-6.hour).to_s(:db)) }
scope "#{verb}_today", lambda { where("DATE(#{prop}) = ?", Date.today.to_s(:db)) }
scope "#{verb}_last7d", lambda { where("DATE(#{prop}) >= ?", (Date.today-7).to_s(:db)) }
scope "#{verb}_last30d", lambda { where("DATE(#{prop}) >= ?", (Date.today-30).to_s(:db)) }
scope "#{verb}_this_month", lambda { where("DATE(#{prop}) >= ?", Date.today.beginning_of_month.to_s(:db)) }
scope "#{verb}_last_6m", lambda { where("DATE(#{prop}) >= ?", (Date.today - 6.months).to_s(:db)) }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment