temporal_scopes 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01054fa8697a5d6a48d6cd36198905e919911a2e
4
+ data.tar.gz: 447d77b095f5c0d98c4972ab4debf32770e85c12
5
+ SHA512:
6
+ metadata.gz: 395a9c350971e89678b547a9b6397eedc9817637f4c4f5ec98a299352e6acd95eee54ff9fd3723d63fb15dd367da221445b8d7e6b6e55137eca01a97a76560a5
7
+ data.tar.gz: aa871fb961337b880c652c9aef1507a094904bf5dd85d2ed3d73bf311e193a112dced1e6d392ca4a6876362d8a4da78f74b54c3b36e76832b8ac59dd3f0d9117
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Sebastian Fiedlschuster.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # TemporalScopes [![Build Status](https://travis-ci.org/fiedl/temporal_scopes.svg?branch=master)](https://travis-ci.org/fiedl/temporal_scopes)
2
+
3
+ Providing temporal scopes for an ActiveRecord model to allow queries by time. For example, `MyModel.now.where(...)`, `my_model.archive`, `MyModel.past.where(...)`.
4
+
5
+ This is done by adding the database columns `valid_from` and `valid_to` to the model's table.
6
+
7
+ ## Usage
8
+
9
+ ### Make an `ActiveRecord` have temporal scopes
10
+
11
+ ```ruby
12
+ class Article < ActiveRecord::Base
13
+ has_temporal_scopes
14
+
15
+ end
16
+ ```
17
+
18
+ ### Archive an object
19
+
20
+ ```ruby
21
+ current_article = Article.create(title: 'My new article', body: 'Lorem ipsum')
22
+
23
+ past_article = Article.create(title: 'My new article', body: 'Lorem ipsum')
24
+ past_article.archive
25
+
26
+ # or provide a datetime:
27
+ past_article.archive at: 1.hour.ago
28
+ ```
29
+
30
+ ### Use temporal scopes for filtering
31
+
32
+ ```ruby
33
+ Article.now # => [current_article]
34
+ Article.past # => [past_article]
35
+ Article.with_past # => [current_article, past_article]
36
+ ```
37
+
38
+ Note that the **default scope** is set to `now`.
39
+
40
+ ```ruby
41
+ Article.all # => [current_article]
42
+ Article.now # => [current_article]
43
+ Article.with_past # => [current_article, past_article]
44
+ Article.without_temporal_condition # => [current_article, past_article]
45
+ ```
46
+
47
+ ### Documentation
48
+
49
+ Further [documentation can be found on rubydoc.info](http://rubydoc.info/github/fiedl/temporal_scopes/master/frames).
50
+
51
+ ### Caveats
52
+
53
+ * There is only one `valid_from` and one `valid_to` time per object. Therefore, you can't keep track of first archiving an object and later un-archiving it. Un-archiving an object loses the information of first archiving it.
54
+
55
+ ## Installation
56
+
57
+ ### Installing the Gem
58
+
59
+ Add the gem to your `Gemfile`:
60
+
61
+ ```ruby
62
+ # Gemfile
63
+ # ...
64
+ gem 'temporal_scopes'
65
+ ```
66
+
67
+ Then, run `bundle install`.
68
+
69
+ ### Database
70
+
71
+ Add the columns `valid_from` and `valid_to` to the model you would like to have temporal scopes.
72
+
73
+ ```
74
+ bundle exec rails generate migration add_validity_period_to_articles valid_from:datetime:index valid_to:datetime:index
75
+ bundle exec rake db:migrate
76
+ ```
77
+
78
+ ## Author and License
79
+
80
+ Copyright (c) 2014 Sebastian Fiedlschuster.
81
+
82
+ Released under the [MIT License](MIT-LICENSE).
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+
8
+ require 'bundler/gem_tasks'
9
+ require 'rspec/core/rake_task'
10
+ require 'rdoc/task'
11
+
12
+ RDoc::Task.new(:rdoc) do |rdoc|
13
+ rdoc.rdoc_dir = 'rdoc'
14
+ rdoc.title = 'TemporalScopes'
15
+ rdoc.options << '--line-numbers'
16
+ rdoc.rdoc_files.include('README.rdoc')
17
+ rdoc.rdoc_files.include('lib/**/*.rb')
18
+ end
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ RSpec::Core::RakeTask.new(:spec)
23
+ task default: :spec
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :temporal_scopes do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,45 @@
1
+ require 'active_record'
2
+
3
+ module TemporalScopes
4
+ module HasTemporalScopes
5
+
6
+ def has_temporal_scopes
7
+
8
+ scope :without_temporal_condition, -> {
9
+ relation = unscope(where: [:valid_from, :valid_to])
10
+ relation.where_values.delete_if { |query| query.to_sql.include?("\"valid_from\"") || query.to_sql.include?("\"valid_to\"") }
11
+ relation
12
+ }
13
+
14
+ scope :now, -> {
15
+ without_temporal_condition
16
+ .where(arel_table[:valid_from].eq(nil).or(arel_table[:valid_from].lteq(Time.zone.now)))
17
+ .where(arel_table[:valid_to].eq(nil).or(arel_table[:valid_to].gteq(Time.zone.now)))
18
+ }
19
+ scope :past, -> { without_temporal_condition.where('valid_to < ?', Time.zone.now) }
20
+ scope :with_past, -> { without_temporal_condition }
21
+
22
+ default_scope { now }
23
+
24
+ extend ClassMethods
25
+ include InstanceMethods
26
+ end
27
+
28
+ module ClassMethods
29
+ end
30
+
31
+ module InstanceMethods
32
+
33
+ def archive(params = {})
34
+ unless self.valid_to
35
+ archive_at = params[:at] || Time.zone.now
36
+ update_attribute(:valid_to, archive_at)
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
44
+
45
+ ActiveRecord::Base.send(:extend, TemporalScopes::HasTemporalScopes)
@@ -0,0 +1,3 @@
1
+ module TemporalScopes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ require 'temporal_scopes/has_temporal_scopes'
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: temporal_scopes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Fiedlschuster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Providing temporal scopes for an ActiveRecord model to allow queries
42
+ by time. For example, `MyModel.now.where(...)`, `my_model.archive`, `MyModel.past.where(...)`.
43
+ email:
44
+ - sebastian@fiedlschuster.de
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - lib/tasks/temporal_scopes_tasks.rake
53
+ - lib/temporal_scopes.rb
54
+ - lib/temporal_scopes/has_temporal_scopes.rb
55
+ - lib/temporal_scopes/version.rb
56
+ homepage: https://github.com/fiedl/temporal_scopes
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.2
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Providing temporal scopes for an ActiveRecord model to allow queries by time.
80
+ For example, `MyModel.now.where(...)`, `my_model.archive`, `MyModel.past.where(...)`.
81
+ test_files: []
82
+ has_rdoc: