Module: TemporalScopes::HasTemporalScopes
- Defined in:
- lib/temporal_scopes/has_temporal_scopes.rb
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Instance Method Summary collapse
-
#has_temporal_scopes ⇒ Object
Adds scopes and methods for handing temporal filtering.
Instance Method Details
#has_temporal_scopes ⇒ Object
Adds scopes and methods for handing temporal filtering.
## Make an ActiveRecord have temporal scopes
class Article < ActiveRecord::Base
has_temporal_scopes
end
## Archive an object
current_article = Article.create(title: 'My new article', body: 'Lorem ipsum')
past_article = Article.create(title: 'My new article', body: 'Lorem ipsum')
past_article.archive
# or provide a datetime:
past_article.archive at: 1.hour.ago
## Use temporal scopes for filtering
Article.now # => [current_article]
Article.past # => [past_article]
Article.with_past # => [current_article, past_article]
Note that the **default scope** is set to now.
Article.all # => [current_article]
Article.now # => [current_article]
Article.with_past # => [current_article, past_article]
Article.without_temporal_condition # => [current_article, past_article]
38 39 40 41 42 43 44 |
# File 'lib/temporal_scopes/has_temporal_scopes.rb', line 38 def has_temporal_scopes default_scope { now } extend ClassMethods include InstanceMethods end |