Skip to content

A plugin for ActiveRecord which adds very helpful methods for querying records over time ranges (weeks/days/months/years), and it also respects time-zones!

License

Notifications You must be signed in to change notification settings

907th/datetime-scopes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Date/time scopes for ActiveRecord models

Gem Version Build Status Code Climate Test Coverage

Date/time scopes for ActiveRecord models you always missed! With proper time-zones support.

Quick start

# Gemfile
gem "datetime-scopes"
# app/models/my_model.rb
class MyModel < ActiveRecord::Base
  datetime_scopes :created_at  # with 'datetime' typed attribute
  date_scopes :birthday        # with 'date' typed attribute
end

Now your model has these scopes:

for created_at for birthday
  • created_within(from, to)
  • created_within_days(from, to)
  • created_within_months(from, to)
  • created_within_years(from, to)
  • created_on_day(t)
  • created_on_month(t)
  • created_on_year(t)
  • created_before(t)
  • created_before_day(t)
  • created_before_month(t)
  • created_before_year(t)
  • created_after(t)
  • created_after_day(t)
  • created_after_month(t)
  • created_after_year(t)
  • created_on_or_before(t)
  • created_on_or_before_day(t)
  • created_on_or_before_month(t)
  • created_on_or_before_year(t)
  • created_on_or_after(t)
  • created_on_or_after_day(t)
  • created_on_or_after_month(t)
  • created_on_or_after_year(t)
  • birthday_within_days(from, to)
  • birthday_within_months(from, to)
  • birthday_within_years(from, to)
  • birthday_on_day(t)
  • birthday_on_month(t)
  • birthday_on_year(t)
  • birthday_before_day(t)
  • birthday_before_month(t)
  • birthday_before_year(t)
  • birthday_after_day(t)
  • birthday_after_month(t)
  • birthday_after_year(t)
  • birthday_on_or_before_day(t)
  • birthday_on_or_before_month(t)
  • birthday_on_or_before_year(t)
  • birthday_on_or_after_day(t)
  • birthday_on_or_after_month(t)
  • birthday_on_or_after_year(t)

Examples:

# All records created yesterday (in current `::Time.zone`)
MyModel.created_on_day(Date.yesterday)

# Records created since 11 Sep 2001 (in current `::Time.zone`)
MyModel.created_on_or_after("2001.09.11")

# Records create since 11 Sep 2001 (in New York). Yes, the result may differ to previous example!
MyModel.created_on_or_after("2001.09.11", time_zone: "Eastern Time (US & Canada)")

# Records with birthday in 2015
MyModel.birthday_on_year(Date.new 2015)

Time-zone support

You know, when it is Sunday in San Francisco, it is Monday in Manila! All parameters passed to a date/time scopes are first converted to a project-specific (::Time.zone) or specified (time_zone: "XXX" param) time-zone with the help of magic ActiveSupport's #in_time_zone helper. See rake time:zones:all for all supported time-zones.

Additional options

You can pass a default time-zone and custom scopes' prefix to a date(time)_scopes method:

# app/models/my_model.rb
class MyModel < ActiveRecord::Base
  datetime_scopes :created_at, prefix: "issued", time_zone: "Ekaterinburg"
end

# All records created yesterday (in Ekaterinburg)
MyModel.issued_on_day(Date.yesterday)

TODO

  • Complete pending tests!
  • ...

Contributing

Any feedback is welcome!

License

Distributed under the MIT License (see LICENSE.txt).

Copyright © 2015. Alexey Chernenkov

About

A plugin for ActiveRecord which adds very helpful methods for querying records over time ranges (weeks/days/months/years), and it also respects time-zones!

Topics

Resources

License

Stars

Watchers

Forks

Languages