datetime-scopes 1.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +139 -0
- data/Rakefile +1 -0
- data/datetime-scopes.gemspec +25 -0
- data/lib/datetime-scopes.rb +8 -0
- data/lib/datetime-scopes/abstract_proxy.rb +13 -0
- data/lib/datetime-scopes/active_record_extension.rb +45 -0
- data/lib/datetime-scopes/date_proxy.rb +82 -0
- data/lib/datetime-scopes/datetime_proxy.rb +99 -0
- data/lib/datetime-scopes/version.rb +3 -0
- data/spec/datetime-scopes/active_record_extension_spec.rb +43 -0
- data/spec/datetime-scopes/date_proxy_spec.rb +7 -0
- data/spec/datetime-scopes/datetime_proxy_spec.rb +7 -0
- data/spec/spec_helper.rb +32 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 75b2ecf33e5ba50fe1ed72e304e9bec585582159
|
4
|
+
data.tar.gz: 2b468452b20e581866d4df006b659e72a4110e1e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a1e4ea76089c638563bb62e7fe0eedca6eb69dd962cf6adc6eafa1908401b76f580d3c4522d024c7fee4fc21fc02c1c352ac59a3f82c9b97f0f84fe9fe925545
|
7
|
+
data.tar.gz: 51e7f73f17bea88420b2853633ef75b528a71f13d7827d792ec38c988b7652280973cc2b973c732df28c94fc40c4b6567e271e485b9ba59181320b824d27018c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
rvm:
|
4
|
+
- 2.3.0
|
5
|
+
|
6
|
+
addons:
|
7
|
+
code_climate:
|
8
|
+
repo_token: ca3a5a1d9af873ba0b6c6fa198a1e9e10c12c738211243d279181f58ca2c73ea
|
9
|
+
|
10
|
+
matrix:
|
11
|
+
- RAILS_VERSION=4.0.0
|
12
|
+
- RAILS_VERSION=4.1.0
|
13
|
+
- RAILS_VERSION=4.2.0
|
14
|
+
|
15
|
+
script: "bundle exec rspec --format=progress --no-color"
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
if ENV["RAILS_VERSION"]
|
4
|
+
gem "activerecord", "~> #{ENV["RAILS_VERSION"]}"
|
5
|
+
gem "activesupport", "~> #{ENV["RAILS_VERSION"]}"
|
6
|
+
end
|
7
|
+
|
8
|
+
gem "sqlite3"
|
9
|
+
gem "rspec", "~> 3.4.0"
|
10
|
+
gem "database_cleaner"
|
11
|
+
gem "codeclimate-test-reporter"
|
12
|
+
|
13
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Alexey Chernenkov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# Date/time scopes for ActiveRecord models
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/907th/datetime-scopes.svg?branch=master)](https://travis-ci.org/907th/datetime-scopes)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/907th/datetime-scopes/badges/gpa.svg)](https://codeclimate.com/github/907th/datetime-scopes)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/907th/datetime-scopes/badges/coverage.svg)](https://codeclimate.com/github/907th/datetime-scopes/coverage)
|
6
|
+
|
7
|
+
Date/time scopes for ActiveRecord models you always missed! With proper time-zones support.
|
8
|
+
|
9
|
+
## Quick start
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
# Gemfile
|
13
|
+
gem "datetime-scopes"
|
14
|
+
```
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
# app/models/my_model.rb
|
18
|
+
class MyModel < ActiveRecord::Base
|
19
|
+
datetime_scopes :created_at # with 'datetime' typed attribute
|
20
|
+
date_scopes :birthday # with 'date' typed attribute
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
Now your model has these scopes:
|
25
|
+
|
26
|
+
<table>
|
27
|
+
<tr>
|
28
|
+
<td>
|
29
|
+
for <b>created_at</b>
|
30
|
+
</td>
|
31
|
+
<td>
|
32
|
+
for <b>birthday</b>
|
33
|
+
</td>
|
34
|
+
</tr>
|
35
|
+
<tr>
|
36
|
+
<td valign="top">
|
37
|
+
<ul>
|
38
|
+
<li>created_within(from, to)</li>
|
39
|
+
<li>created_within_days(from, to)</li>
|
40
|
+
<li>created_within_months(from, to)</li>
|
41
|
+
<li>created_within_years(from, to)</li>
|
42
|
+
<li>created_on_day(t)</li>
|
43
|
+
<li>created_on_month(t)</li>
|
44
|
+
<li>created_on_year(t)</li>
|
45
|
+
<li>created_before(t)</li>
|
46
|
+
<li>created_before_day(t)</li>
|
47
|
+
<li>created_before_month(t)</li>
|
48
|
+
<li>created_before_year(t)</li>
|
49
|
+
<li>created_after(t)</li>
|
50
|
+
<li>created_after_day(t)</li>
|
51
|
+
<li>created_after_month(t)</li>
|
52
|
+
<li>created_after_year(t)</li>
|
53
|
+
<li>created_on_or_before(t)</li>
|
54
|
+
<li>created_on_or_before_day(t)</li>
|
55
|
+
<li>created_on_or_before_month(t)</li>
|
56
|
+
<li>created_on_or_before_year(t)</li>
|
57
|
+
<li>created_on_or_after(t)</li>
|
58
|
+
<li>created_on_or_after_day(t)</li>
|
59
|
+
<li>created_on_or_after_month(t)</li>
|
60
|
+
<li>created_on_or_after_year(t)</li>
|
61
|
+
</ul>
|
62
|
+
</td>
|
63
|
+
<td valign="top">
|
64
|
+
<ul>
|
65
|
+
<li>birthday_within_days(from, to)</li>
|
66
|
+
<li>birthday_within_months(from, to)</li>
|
67
|
+
<li>birthday_within_years(from, to)</li>
|
68
|
+
<li>birthday_on_day(t)</li>
|
69
|
+
<li>birthday_on_month(t)</li>
|
70
|
+
<li>birthday_on_year(t)</li>
|
71
|
+
<li>birthday_before_day(t)</li>
|
72
|
+
<li>birthday_before_month(t)</li>
|
73
|
+
<li>birthday_before_year(t)</li>
|
74
|
+
<li>birthday_after_day(t)</li>
|
75
|
+
<li>birthday_after_month(t)</li>
|
76
|
+
<li>birthday_after_year(t)</li>
|
77
|
+
<li>birthday_on_or_before_day(t)</li>
|
78
|
+
<li>birthday_on_or_before_month(t)</li>
|
79
|
+
<li>birthday_on_or_before_year(t)</li>
|
80
|
+
<li>birthday_on_or_after_day(t)</li>
|
81
|
+
<li>birthday_on_or_after_month(t)</li>
|
82
|
+
<li>birthday_on_or_after_year(t)</li>
|
83
|
+
</ul>
|
84
|
+
</td>
|
85
|
+
</tr>
|
86
|
+
</table>
|
87
|
+
|
88
|
+
Examples:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
# All records created yesterday (in current `::Time.zone`)
|
92
|
+
MyModel.created_on_day(Date.yesterday)
|
93
|
+
|
94
|
+
# Records created since 11 Sep 2001 (in current `::Time.zone`)
|
95
|
+
MyModel.created_on_or_after("2001.09.11")
|
96
|
+
|
97
|
+
# Records create since 11 Sep 2001 (in New York). Yes, the result may differ to previous example!
|
98
|
+
MyModel.created_on_or_after("2001.09.11", time_zone: "Eastern Time (US & Canada)")
|
99
|
+
|
100
|
+
# Records with birthday in 2015
|
101
|
+
MyModel.birthday_on_year(Date.new 2015)
|
102
|
+
```
|
103
|
+
|
104
|
+
## Time-zone support
|
105
|
+
|
106
|
+
You know, when it is Sunday in San Francisco, it is Monday in Manila!
|
107
|
+
All parameters passed to a date/time scopes are first converted to a
|
108
|
+
project-specific (`::Time.zone`) or specified (`time_zone: "XXX"` param) time-zone with the help
|
109
|
+
of magic ActiveSupport's `#in_time_zone` helper. See `rake time:zones:all` for
|
110
|
+
all supported time-zones.
|
111
|
+
|
112
|
+
## Additional options
|
113
|
+
|
114
|
+
You can pass a default time-zone and custom scopes' prefix to a `date(time)_scopes` method:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
# app/models/my_model.rb
|
118
|
+
class MyModel < ActiveRecord::Base
|
119
|
+
datetime_scopes :created_at, prefix: "issued", time_zone: "Ekaterinburg"
|
120
|
+
end
|
121
|
+
|
122
|
+
# All records created yesterday (in Ekaterinburg)
|
123
|
+
MyModel.issued_on_day(Date.yesterday)
|
124
|
+
```
|
125
|
+
|
126
|
+
## TODO
|
127
|
+
|
128
|
+
- Complete pending tests!
|
129
|
+
- ...
|
130
|
+
|
131
|
+
## Contributing
|
132
|
+
|
133
|
+
Any feedback is welcome!
|
134
|
+
|
135
|
+
## License
|
136
|
+
|
137
|
+
Distributed under the MIT License (see LICENSE.txt).
|
138
|
+
|
139
|
+
Copyright © 2015. Alexey Chernenkov
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "datetime-scopes/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "datetime-scopes"
|
7
|
+
spec.version = DateTimeScopes::VERSION
|
8
|
+
spec.authors = ["Alexey Chernenkov"]
|
9
|
+
spec.email = ["laise@pisem.net"]
|
10
|
+
spec.summary = "Date/time scopes for ActiveRecord models you always missed!"
|
11
|
+
spec.description = spec.summary
|
12
|
+
spec.homepage = "https://github.com/907th/datetime-scopes"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.11.2"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.4.2"
|
22
|
+
|
23
|
+
spec.add_dependency "activerecord", ">= 4.0"
|
24
|
+
spec.add_dependency "activesupport", ">= 4.0"
|
25
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "active_support/core_ext/date"
|
3
|
+
require "active_support/core_ext/time"
|
4
|
+
require "active_support/core_ext/date_time"
|
5
|
+
|
6
|
+
require "datetime-scopes/active_record_extension"
|
7
|
+
|
8
|
+
ActiveRecord::Base.send :include, DateTimeScopes::ActiveRecordExtension
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require_relative "datetime_proxy"
|
2
|
+
require_relative "date_proxy"
|
3
|
+
|
4
|
+
module DateTimeScopes
|
5
|
+
module ActiveRecordExtension
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def datetime_scopes(attr, prefix: nil, time_zone: ::Time.zone)
|
12
|
+
proxy = DateTimeProxy.new(
|
13
|
+
klass: self,
|
14
|
+
attribute: "#{table_name}.#{attr}",
|
15
|
+
time_zone: time_zone
|
16
|
+
)
|
17
|
+
prefix = datetime_scope_prefix(attr) if prefix.nil?
|
18
|
+
declare_datetime_scopes prefix, proxy
|
19
|
+
end
|
20
|
+
|
21
|
+
def date_scopes(attr, prefix: nil, time_zone: nil)
|
22
|
+
proxy = DateProxy.new(
|
23
|
+
klass: self,
|
24
|
+
attribute: "#{table_name}.#{attr}",
|
25
|
+
time_zone: time_zone
|
26
|
+
)
|
27
|
+
prefix = datetime_scope_prefix(attr) if prefix.nil?
|
28
|
+
declare_datetime_scopes prefix, proxy
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def declare_datetime_scopes(prefix, proxy)
|
34
|
+
proxy.proxy_methods.each do |method_name|
|
35
|
+
scope_name = "#{prefix}_#{method_name}"
|
36
|
+
scope scope_name, -> (*attrs) { proxy.send method_name, *attrs }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def datetime_scope_prefix(attr)
|
41
|
+
attr.to_s.sub /_(at|on|time|date)$/, ""
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require_relative "abstract_proxy"
|
2
|
+
|
3
|
+
module DateTimeScopes
|
4
|
+
class DateProxy < AbstractProxy
|
5
|
+
PROXY_METHODS = %w[
|
6
|
+
within_days
|
7
|
+
within_months
|
8
|
+
within_years
|
9
|
+
on_day
|
10
|
+
on_month
|
11
|
+
on_year
|
12
|
+
before_day
|
13
|
+
before_month
|
14
|
+
before_year
|
15
|
+
after_day
|
16
|
+
after_month
|
17
|
+
after_year
|
18
|
+
on_or_before_day
|
19
|
+
on_or_before_month
|
20
|
+
on_or_before_year
|
21
|
+
on_or_after_day
|
22
|
+
on_or_after_month
|
23
|
+
on_or_after_year
|
24
|
+
].freeze
|
25
|
+
|
26
|
+
def proxy_methods
|
27
|
+
PROXY_METHODS
|
28
|
+
end
|
29
|
+
|
30
|
+
# Intervals
|
31
|
+
|
32
|
+
def within_days(from, to, time_zone: @time_zone)
|
33
|
+
@klass.where(
|
34
|
+
"#{@attribute}) >= ? AND #{@attribute} <= ?",
|
35
|
+
from.in_time_zone(time_zone).to_date,
|
36
|
+
to.in_time_zone(time_zone).to_date
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def within_months(from, to, time_zone: @time_zone)
|
41
|
+
within_days(
|
42
|
+
from.in_time_zone(time_zone).beginning_of_month,
|
43
|
+
to.in_time_zone(time_zone).end_of_month,
|
44
|
+
time_zone: time_zone
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
def within_years(from, to, time_zone: @time_zone)
|
49
|
+
within_days(
|
50
|
+
from.in_time_zone(time_zone).beginning_of_year,
|
51
|
+
to.in_time_zone(time_zone).end_of_year,
|
52
|
+
time_zone: time_zone
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Specific day/month/year
|
57
|
+
|
58
|
+
def on_day(day, time_zone: @time_zone) ; within_days day, day, time_zone: time_zone ; end
|
59
|
+
def on_month(month, time_zone: @time_zone) ; within_months month, month, time_zone: time_zone ; end
|
60
|
+
def on_year(year, time_zone: @time_zone) ; within_years year, year, time_zone: time_zone ; end
|
61
|
+
|
62
|
+
# Strict equations
|
63
|
+
|
64
|
+
def before_day(day, time_zone: @time_zone) ; @klass.where "#{@attribute} < ?", day.in_time_zone(time_zone).to_date ; end
|
65
|
+
def before_month(month, time_zone: @time_zone) ; before_day month.in_time_zone(time_zone).beginning_of_month, time_zone: time_zone ; end
|
66
|
+
def before_year(year, time_zone: @time_zone) ; before_day year.in_time_zone(time_zone).beginning_of_year, time_zone: time_zone ; end
|
67
|
+
|
68
|
+
def after_day(day, time_zone: @time_zone) ; @klass.where "#{@attribute} > ?", day.in_time_zone(time_zone).to_date ; end
|
69
|
+
def after_month(month, time_zone: @time_zone) ; after_day month.in_time_zone(time_zone).end_of_month, time_zone: time_zone ; end
|
70
|
+
def after_year(year, time_zone: @time_zone) ; after_day year.in_time_zone(time_zone).end_of_year, time_zone: time_zone ; end
|
71
|
+
|
72
|
+
# Non-strict equations
|
73
|
+
|
74
|
+
def on_or_before_day(day, time_zone: @time_zone) ; @klass.where "#{@attribute} <= ?", day.in_time_zone(time_zone).to_date ; end
|
75
|
+
def on_or_before_month(month, time_zone: @time_zone) ; on_or_before_day month.in_time_zone(time_zone).end_of_month, time_zone: time_zone ; end
|
76
|
+
def on_or_before_year(year, time_zone: @time_zone) ; on_or_before_day year.in_time_zone(time_zone).end_of_year, time_zone: time_zone ; end
|
77
|
+
|
78
|
+
def on_or_after_day(day, time_zone: @time_zone) ; @klass.where "#{@attribute} >= ?", day.in_time_zone(time_zone).to_date ; end
|
79
|
+
def on_or_after_month(month, time_zone: @time_zone) ; on_or_after_day month.in_time_zone(time_zone).beginning_of_month, time_zone: time_zone ; end
|
80
|
+
def on_or_after_year(year, time_zone: @time_zone) ; on_or_after_day year.in_time_zone(time_zone).beginning_of_year, time_zone: time_zone ; end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require_relative "abstract_proxy"
|
2
|
+
|
3
|
+
module DateTimeScopes
|
4
|
+
class DateTimeProxy < AbstractProxy
|
5
|
+
PROXY_METHODS = %w[
|
6
|
+
within
|
7
|
+
within_days
|
8
|
+
within_months
|
9
|
+
within_years
|
10
|
+
on_day
|
11
|
+
on_month
|
12
|
+
on_year
|
13
|
+
before
|
14
|
+
before_day
|
15
|
+
before_month
|
16
|
+
before_year
|
17
|
+
after
|
18
|
+
after_day
|
19
|
+
after_month
|
20
|
+
after_year
|
21
|
+
on_or_before
|
22
|
+
on_or_before_day
|
23
|
+
on_or_before_month
|
24
|
+
on_or_before_year
|
25
|
+
on_or_after
|
26
|
+
on_or_after_day
|
27
|
+
on_or_after_month
|
28
|
+
on_or_after_year
|
29
|
+
].freeze
|
30
|
+
|
31
|
+
def proxy_methods
|
32
|
+
PROXY_METHODS
|
33
|
+
end
|
34
|
+
|
35
|
+
# Intervals
|
36
|
+
|
37
|
+
def within(from, to, time_zone: @time_zone)
|
38
|
+
@klass.where(
|
39
|
+
"#{@attribute} >= ? AND #{@attribute} <= ?",
|
40
|
+
from.in_time_zone(time_zone),
|
41
|
+
to.in_time_zone(time_zone)
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
def within_days(from, to, time_zone: @time_zone)
|
46
|
+
within(
|
47
|
+
from.in_time_zone(time_zone).beginning_of_day,
|
48
|
+
to.in_time_zone(time_zone).end_of_day,
|
49
|
+
time_zone: time_zone
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
def within_months(from, to, time_zone: @time_zone)
|
54
|
+
within(
|
55
|
+
from.in_time_zone(time_zone).beginning_of_month,
|
56
|
+
to.in_time_zone(time_zone).end_of_month,
|
57
|
+
time_zone: time_zone
|
58
|
+
)
|
59
|
+
end
|
60
|
+
|
61
|
+
def within_years(from, to, time_zone: @time_zone)
|
62
|
+
within(
|
63
|
+
from.in_time_zone(time_zone).beginning_of_year,
|
64
|
+
to.in_time_zone(time_zone).end_of_year,
|
65
|
+
time_zone: time_zone
|
66
|
+
)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Specific day/month/year
|
70
|
+
|
71
|
+
def on_day(day, time_zone: @time_zone) ; within_days day, day, time_zone: time_zone ; end
|
72
|
+
def on_month(month, time_zone: @time_zone) ; within_months month, month, time_zone: time_zone ; end
|
73
|
+
def on_year(year, time_zone: @time_zone) ; within_years year, year, time_zone: time_zone ; end
|
74
|
+
|
75
|
+
# Strict equations
|
76
|
+
|
77
|
+
def before(time, time_zone: @time_zone) ; @klass.where "#{@attribute} < ?", time.in_time_zone(time_zone) ; end
|
78
|
+
def before_day(day, time_zone: @time_zone) ; before day.in_time_zone(time_zone).beginning_of_day, time_zone: time_zone ; end
|
79
|
+
def before_month(month, time_zone: @time_zone) ; before month.in_time_zone(time_zone).beginning_of_month, time_zone: time_zone ; end
|
80
|
+
def before_year(year, time_zone: @time_zone) ; before year.in_time_zone(time_zone).beginning_of_year, time_zone: time_zone ; end
|
81
|
+
|
82
|
+
def after(time, time_zone: @time_zone) ; @klass.where "#{@attribute} > ?", time.in_time_zone(time_zone) ; end
|
83
|
+
def after_day(day, time_zone: @time_zone) ; after day.in_time_zone(time_zone).end_of_day, time_zone: time_zone ; end
|
84
|
+
def after_month(month, time_zone: @time_zone) ; after month.in_time_zone(time_zone).end_of_month, time_zone: time_zone ; end
|
85
|
+
def after_year(year, time_zone: @time_zone) ; after year.in_time_zone(time_zone).end_of_year, time_zone: time_zone ; end
|
86
|
+
|
87
|
+
# Non-strict equations
|
88
|
+
|
89
|
+
def on_or_before(time, time_zone: @time_zone) ; @klass.where "#{@attribute} <= ?", time.in_time_zone(time_zone) ; end
|
90
|
+
def on_or_before_day(day, time_zone: @time_zone) ; on_or_before day.in_time_zone(time_zone).end_of_day, time_zone: time_zone ; end
|
91
|
+
def on_or_before_month(month, time_zone: @time_zone) ; on_or_before month.in_time_zone(time_zone).end_of_month, time_zone: time_zone ; end
|
92
|
+
def on_or_before_year(year, time_zone: @time_zone) ; on_or_before year.in_time_zone(time_zone).end_of_year, time_zone: time_zone ; end
|
93
|
+
|
94
|
+
def on_or_after(time, time_zone: @time_zone) ; @klass.where "#{@attribute} >= ?", time.in_time_zone(time_zone) ; end
|
95
|
+
def on_or_after_day(day, time_zone: @time_zone) ; on_or_after day.in_time_zone(time_zone).beginning_of_day, time_zone: time_zone ; end
|
96
|
+
def on_or_after_month(month, time_zone: @time_zone) ; on_or_after month.in_time_zone(time_zone).beginning_of_month, time_zone: time_zone ; end
|
97
|
+
def on_or_after_year(year, time_zone: @time_zone) ; on_or_after year.in_time_zone(time_zone).beginning_of_year, time_zone: time_zone ; end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.describe FooBar do
|
4
|
+
describe "::datetime_scopes" do
|
5
|
+
let!(:proxy_methods) { DateTimeScopes::DateTimeProxy::PROXY_METHODS }
|
6
|
+
|
7
|
+
it "defines datetime scopes" do
|
8
|
+
proxy_methods.each do |method_name|
|
9
|
+
expect(FooBar).to respond_to "foo_#{method_name}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "delegates a scope call to a proxy" do
|
14
|
+
proxy_methods.each do |method_name|
|
15
|
+
dbl = double("result")
|
16
|
+
expect_any_instance_of(DateTimeScopes::DateTimeProxy).to receive(method_name).with(1, 2, 3).and_return(dbl)
|
17
|
+
res = FooBar.send("foo_#{method_name}", 1, 2, 3)
|
18
|
+
expect(res).to eq(dbl)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "::date_scopes" do
|
24
|
+
def proxy_methods
|
25
|
+
DateTimeScopes::DateProxy::PROXY_METHODS
|
26
|
+
end
|
27
|
+
|
28
|
+
it "defines date scopes" do
|
29
|
+
proxy_methods.each do |method_name|
|
30
|
+
expect(FooBar).to respond_to "bar_#{method_name}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "delegates a scope call to a proxy" do
|
35
|
+
proxy_methods.each do |method_name|
|
36
|
+
dbl = double("result")
|
37
|
+
expect_any_instance_of(DateTimeScopes::DateProxy).to receive(method_name).with(1, 2, 3).and_return(dbl)
|
38
|
+
res = FooBar.send("bar_#{method_name}", 1, 2, 3)
|
39
|
+
expect(res).to eq(dbl)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "codeclimate-test-reporter"
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
4
|
+
require "rspec/core"
|
5
|
+
require "database_cleaner"
|
6
|
+
require "datetime-scopes"
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
9
|
+
DatabaseCleaner.strategy = :truncation
|
10
|
+
|
11
|
+
RSpec.configure do |c|
|
12
|
+
c.before :suite do
|
13
|
+
DatabaseCleaner.clean
|
14
|
+
end
|
15
|
+
c.after :example do
|
16
|
+
DatabaseCleaner.clean
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
# Create table and declare AR model
|
22
|
+
|
23
|
+
ActiveRecord::Migration.verbose = false
|
24
|
+
ActiveRecord::Migration.create_table :foo_bar do |t|
|
25
|
+
t.datetime :foo
|
26
|
+
t.date :bar
|
27
|
+
end
|
28
|
+
|
29
|
+
class FooBar < ActiveRecord::Base
|
30
|
+
datetime_scopes :foo
|
31
|
+
date_scopes :bar
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: datetime-scopes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.alpha1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexey Chernenkov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.11.2
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.11.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 10.4.2
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 10.4.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.0'
|
69
|
+
description: Date/time scopes for ActiveRecord models you always missed!
|
70
|
+
email:
|
71
|
+
- laise@pisem.net
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- datetime-scopes.gemspec
|
84
|
+
- lib/datetime-scopes.rb
|
85
|
+
- lib/datetime-scopes/abstract_proxy.rb
|
86
|
+
- lib/datetime-scopes/active_record_extension.rb
|
87
|
+
- lib/datetime-scopes/date_proxy.rb
|
88
|
+
- lib/datetime-scopes/datetime_proxy.rb
|
89
|
+
- lib/datetime-scopes/version.rb
|
90
|
+
- spec/datetime-scopes/active_record_extension_spec.rb
|
91
|
+
- spec/datetime-scopes/date_proxy_spec.rb
|
92
|
+
- spec/datetime-scopes/datetime_proxy_spec.rb
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
homepage: https://github.com/907th/datetime-scopes
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">"
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: 1.3.1
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.4.8
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Date/time scopes for ActiveRecord models you always missed!
|
118
|
+
test_files:
|
119
|
+
- spec/datetime-scopes/active_record_extension_spec.rb
|
120
|
+
- spec/datetime-scopes/date_proxy_spec.rb
|
121
|
+
- spec/datetime-scopes/datetime_proxy_spec.rb
|
122
|
+
- spec/spec_helper.rb
|