activerecord-time-scope 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +33 -0
- data/.travis.yml +20 -0
- data/Gemfile +8 -0
- data/LICENSE +20 -0
- data/README.md +92 -0
- data/Rakefile +8 -0
- data/VERSION +1 -0
- data/activerecord-time-scope.gemspec +24 -0
- data/lib/active_record/time_scope.rb +42 -0
- data/lib/active_record/time_scope/scope_proxy.rb +10 -0
- data/lib/active_record/time_scope/time_proxy.rb +28 -0
- data/lib/active_record/time_scope/time_range_proxy.rb +29 -0
- data/lib/active_record/time_scope/version.rb +5 -0
- data/lib/activerecord-time-scope.rb +5 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/support/tmp_model.rb +23 -0
- data/spec/time_scope/time_range_spec.rb +68 -0
- data/spec/time_scope/time_spec.rb +66 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 893d1a58b0e6307c6131616ea70208b1ba41d56e
|
4
|
+
data.tar.gz: 5768fa3193147764439c093653462d94bc69ade3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 63af485ae9c2a98acee8337f7b2fd6ad9276796b3380036764e150b126b423799aa8a79756c02951e6608ccc04376d3ecbfa7b6f765fba80d4ebdd50c4f2a3fb
|
7
|
+
data.tar.gz: 44fec36195a17d594a01831577811533494108348cee91293847124c7477629e4c3971169c2d509aa23ca2572fd80d4c15ef38a328a8258eae153d0cdc8c83e1
|
data/.gitignore
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
/log/
|
12
|
+
|
13
|
+
## Specific to RubyMotion:
|
14
|
+
.dat*
|
15
|
+
.repl_history
|
16
|
+
build/
|
17
|
+
|
18
|
+
## Documentation cache and generated files:
|
19
|
+
/.yardoc/
|
20
|
+
/_yardoc/
|
21
|
+
/doc/
|
22
|
+
/rdoc/
|
23
|
+
|
24
|
+
## Environment normalisation:
|
25
|
+
/.bundle/
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
## Misc
|
29
|
+
Gemfile.lock
|
30
|
+
gemfiles/*.lock
|
31
|
+
.ruby-version
|
32
|
+
.ruby-gemset
|
33
|
+
.rvmrc
|
data/.travis.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
rvm:
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0.0
|
6
|
+
- 2.1
|
7
|
+
|
8
|
+
env:
|
9
|
+
-
|
10
|
+
- ACTIVE_RECORD_VERSION=3.2.0
|
11
|
+
- ACTIVE_RECORD_VERSION=4.0.0
|
12
|
+
- ACTIVE_RECORD_VERSION=4.1.0
|
13
|
+
- ACTIVE_RECORD_VERSION=4.2.0
|
14
|
+
- ACTIVE_RECORD_VERSION=4.2.0.beta1
|
15
|
+
|
16
|
+
script: "bundle exec rake spec"
|
17
|
+
|
18
|
+
branches:
|
19
|
+
only:
|
20
|
+
- master
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Daisuke Taniwaki
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# activerecord-time-scope
|
2
|
+
|
3
|
+
[![Gem Version][gem-image]][gem-link]
|
4
|
+
[![Dependency Status][deps-image]][deps-link]
|
5
|
+
[![Build Status][build-image]][build-link]
|
6
|
+
[![Coverage Status][cov-image]][cov-link]
|
7
|
+
[![Code Climate][gpa-image]][gpa-link]
|
8
|
+
|
9
|
+
Add various time-related scopes for ActiveRecord!
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add the activerecord-time-scope gem to your Gemfile.
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem "activerecord-time-scope"
|
17
|
+
```
|
18
|
+
|
19
|
+
And run `bundle install`.
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Time
|
24
|
+
|
25
|
+
This gem automatically create scopes for time-related columns of your ActiveRecord model such as 'created_at'.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Foo < ActiveRecord::Base
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
Then,
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
Foo.created_before 3.days.ago
|
36
|
+
Foo.created_after 3.days.ago
|
37
|
+
Foo.created_within 3.days.ago, 3.days.from_now
|
38
|
+
```
|
39
|
+
|
40
|
+
Any columns with `_at`, `_on`, `_time` and `_date` postfix are considered as time-related columns.
|
41
|
+
|
42
|
+
### Time Range
|
43
|
+
|
44
|
+
If you want scopes for time ranges, you can create it manually.
|
45
|
+
|
46
|
+
```
|
47
|
+
class Round < ActiveRecord::Base
|
48
|
+
create_time_range_scope :run, :start_at, :end_at
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
Then,
|
53
|
+
|
54
|
+
```
|
55
|
+
Round.run_before 3.days.ago
|
56
|
+
Round.run_after 3.days.ago
|
57
|
+
Round.run_within 3.days.ago, 3.days.from_now
|
58
|
+
```
|
59
|
+
|
60
|
+
will be available for the class!
|
61
|
+
|
62
|
+
## TODO
|
63
|
+
|
64
|
+
- Handle overwrapped cases
|
65
|
+
- Include equal operators
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create new [Pull Request](../../pull/new/master)
|
74
|
+
|
75
|
+
## Copyright
|
76
|
+
|
77
|
+
Copyright (c) 2014 Daisuke Taniwaki. See [LICENSE](LICENSE) for details.
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
[gem-image]: https://badge.fury.io/rb/activerecord-time-scope.svg
|
83
|
+
[gem-link]: http://badge.fury.io/rb/activerecord-time-scope
|
84
|
+
[build-image]: https://secure.travis-ci.org/dtaniwaki/activerecord-time-scope.png
|
85
|
+
[build-link]: http://travis-ci.org/dtaniwaki/activerecord-time-scope
|
86
|
+
[deps-image]: https://gemnasium.com/dtaniwaki/activerecord-time-scope.svg
|
87
|
+
[deps-link]: https://gemnasium.com/dtaniwaki/activerecord-time-scope
|
88
|
+
[cov-image]: https://coveralls.io/repos/dtaniwaki/activerecord-time-scope/badge.png
|
89
|
+
[cov-link]: https://coveralls.io/r/dtaniwaki/activerecord-time-scope
|
90
|
+
[gpa-image]: https://codeclimate.com/github/dtaniwaki/activerecord-time-scope.png
|
91
|
+
[gpa-link]: https://codeclimate.com/github/dtaniwaki/activerecord-time-scope
|
92
|
+
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path('../lib/active_record/time_scope/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = "activerecord-time-scope"
|
5
|
+
gem.version = ActiveRecord::TimeScope::VERSION
|
6
|
+
gem.platform = Gem::Platform::RUBY
|
7
|
+
gem.authors = ["Daisuke Taniwaki"]
|
8
|
+
gem.email = ["daisuketaniwaki@gmail.com"]
|
9
|
+
gem.homepage = "https://github.com/dtaniwaki/activerecord-time-scope"
|
10
|
+
gem.summary = "Time-Related Scope for ActiveRecord"
|
11
|
+
gem.description = "Time-Related Scope for ActiveRecord"
|
12
|
+
gem.license = "MIT"
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split("\n")
|
15
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
gem.require_paths = ['lib']
|
18
|
+
|
19
|
+
gem.add_dependency "activerecord", ">= 3.2"
|
20
|
+
|
21
|
+
gem.add_development_dependency "rake"
|
22
|
+
gem.add_development_dependency "rspec", ">= 3.0"
|
23
|
+
gem.add_development_dependency "coveralls"
|
24
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'active_record/time_scope/time_proxy'
|
2
|
+
require 'active_record/time_scope/time_range_proxy'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module TimeScope
|
6
|
+
TIME_POSTFIX_REGEXP = /_(at|on|time|date)$/
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.extend ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def inherited(subclass)
|
14
|
+
super
|
15
|
+
if subclass.table_exists?
|
16
|
+
subclass.column_names.each do |cn|
|
17
|
+
verb = cn.sub TIME_POSTFIX_REGEXP, ''
|
18
|
+
next if verb == cn
|
19
|
+
subclass.create_time_scope verb, cn
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_time_scope(verb, name)
|
25
|
+
model = self
|
26
|
+
name = "#{table_name}.#{name}"
|
27
|
+
scope "#{verb}_before", ->(time, opts = {}){ TimeProxy.new(model, name).before(time, opts) }
|
28
|
+
scope "#{verb}_after", ->(time, opts = {}){ TimeProxy.new(model, name).after(time, opts) }
|
29
|
+
scope "#{verb}_within", ->(from, to, from_opts = {}, to_opts = {}){ TimeProxy.new(model, name).within(from, to, from_opts, to_opts) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_time_range_scope(verb, from_name, to_name)
|
33
|
+
model = self
|
34
|
+
from_name = "#{table_name}.#{from_name}"
|
35
|
+
to_name = "#{table_name}.#{to_name}"
|
36
|
+
model.scope "#{verb}_before", ->(time, opts = {}){ TimeRangeProxy.new(model, from_name, to_name).before(time, opts) }
|
37
|
+
scope "#{verb}_after", ->(time, opts = {}){ TimeRangeProxy.new(model, from_name, to_name).after(time, opts) }
|
38
|
+
scope "#{verb}_within", ->(from, to, from_opts = {}, to_opts = {}){ TimeRangeProxy.new(model, from_name, to_name).within(from, to, from_opts, to_opts) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'scope_proxy'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module TimeScope
|
5
|
+
class TimeProxy < ScopeProxy
|
6
|
+
def initialize(model, column_name)
|
7
|
+
@model = model
|
8
|
+
@column_name = column_name
|
9
|
+
end
|
10
|
+
|
11
|
+
def before(time, opts = {})
|
12
|
+
operator = opts[:include_equal].to_s != '' ? '=<' : '<'
|
13
|
+
@model.where("#{@column_name} #{operator} ?", time)
|
14
|
+
end
|
15
|
+
|
16
|
+
def after(time, opts = {})
|
17
|
+
operator = opts[:include_equal].to_s != '' ? '=<' : '<'
|
18
|
+
@model.where("? #{operator} #{@column_name}", time)
|
19
|
+
end
|
20
|
+
|
21
|
+
def within(from, to, from_opts = {}, to_opts = {})
|
22
|
+
from_operator = from_opts[:include_equal].to_s != '' ? '=<' : '<'
|
23
|
+
to_operator = to_opts[:include_equal].to_s != '' ? '=<' : '<'
|
24
|
+
@model.where("? #{from_operator} #{@column_name} AND #{@column_name} #{to_operator} ?", from, to)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'scope_proxy'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module TimeScope
|
5
|
+
class TimeRangeProxy < ScopeProxy
|
6
|
+
def initialize(model, column_name1, column_name2)
|
7
|
+
@model = model
|
8
|
+
@column_name1 = column_name1
|
9
|
+
@column_name2 = column_name2
|
10
|
+
end
|
11
|
+
|
12
|
+
def before(time, opts = {})
|
13
|
+
operator = opts[:include_equal].to_s != '' ? '=<' : '<'
|
14
|
+
@model.where("#{@column_name1} #{operator} ? AND #{@column_name2} #{operator} ?", time, time)
|
15
|
+
end
|
16
|
+
|
17
|
+
def after(time, opts = {})
|
18
|
+
operator = opts[:include_equal].to_s != '' ? '=<' : '<'
|
19
|
+
@model.where("? #{operator} #{@column_name1} AND ? #{operator} #{@column_name2}", time, time)
|
20
|
+
end
|
21
|
+
|
22
|
+
def within(from, to, from_opts = {}, to_opts = {})
|
23
|
+
from_operator = from_opts[:include_equal].to_s != '' ? '=<' : '<'
|
24
|
+
to_operator = to_opts[:include_equal].to_s != '' ? '=<' : '<'
|
25
|
+
@model.where("? #{from_operator} #{@column_name1} AND #{@column_name2} #{to_operator} ?", from, to)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'simplecov'
|
3
|
+
require 'coveralls'
|
4
|
+
Coveralls.wear!
|
5
|
+
|
6
|
+
require 'database_cleaner'
|
7
|
+
|
8
|
+
require 'activerecord-time-scope'
|
9
|
+
|
10
|
+
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f }
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
13
|
+
|
14
|
+
RSpec.configure do |c|
|
15
|
+
c.before :suite do
|
16
|
+
DatabaseCleaner.clean_with :truncation
|
17
|
+
end
|
18
|
+
c.before :example do
|
19
|
+
DatabaseCleaner.strategy = :truncation
|
20
|
+
DatabaseCleaner.start
|
21
|
+
end
|
22
|
+
c.after :example do
|
23
|
+
DatabaseCleaner.clean
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
def create_tmp_model(_model_name, _table_name = 'tmp', _columns = {}, &_block)
|
2
|
+
if ActiveRecord::Base.connection.table_exists? _table_name
|
3
|
+
ActiveRecord::Migration.drop_table _table_name
|
4
|
+
end
|
5
|
+
begin
|
6
|
+
Object.send :remove_const, _model_name
|
7
|
+
rescue NameError
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveRecord::Migration.create_table _table_name do |t|
|
11
|
+
_columns.each do |name, type|
|
12
|
+
t.column name, type
|
13
|
+
end
|
14
|
+
end
|
15
|
+
Object.class_eval <<-EOS
|
16
|
+
class #{_model_name} < ActiveRecord::Base
|
17
|
+
self.table_name = '#{_table_name}'
|
18
|
+
end
|
19
|
+
EOS
|
20
|
+
klass = Object.const_get _model_name
|
21
|
+
klass.class_exec(&_block) unless _block.nil?
|
22
|
+
klass
|
23
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe "time_range_spec" do
|
4
|
+
klass = create_tmp_model "Test2Class", "test2_classes", foo_at: :datetime, bar_at: :datetime do
|
5
|
+
create_time_range_scope :wow, :foo_at, :bar_at
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "::wow_before" do
|
9
|
+
subject { klass.__send__("wow_before", time) }
|
10
|
+
let(:time) { DateTime.now }
|
11
|
+
context "foo & bar < time" do
|
12
|
+
let!(:model) { klass.create! foo_at: time - 2.days, bar_at: time - 1.days }
|
13
|
+
it { is_expected.to eq [model] }
|
14
|
+
end
|
15
|
+
context "time < foo & bar" do
|
16
|
+
let!(:model) { klass.create! foo_at: time + 1.days, bar_at: time + 2.days }
|
17
|
+
it { is_expected.to eq [] }
|
18
|
+
end
|
19
|
+
context "foo < time < bar" do
|
20
|
+
let!(:model) { klass.create! foo_at: time - 1.days, bar_at: time + 1.days }
|
21
|
+
it { is_expected.to eq [] }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
describe "::wow_after" do
|
25
|
+
subject { klass.__send__("wow_after", time) }
|
26
|
+
let(:time) { DateTime.now }
|
27
|
+
context "foo & bar < time" do
|
28
|
+
let!(:model) { klass.create! foo_at: time - 2.days, bar_at: time - 1.days }
|
29
|
+
it { is_expected.to eq [] }
|
30
|
+
end
|
31
|
+
context "time < foo & bar" do
|
32
|
+
let!(:model) { klass.create! foo_at: time + 1.days, bar_at: time + 2.days }
|
33
|
+
it { is_expected.to eq [model] }
|
34
|
+
end
|
35
|
+
context "foo < time < bar" do
|
36
|
+
let!(:model) { klass.create! foo_at: time - 1.days, bar_at: time + 1.days }
|
37
|
+
it { is_expected.to eq [] }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
describe "::wow_within" do
|
41
|
+
subject { klass.__send__("wow_within", time - 2.days, time + 2.days) }
|
42
|
+
let(:time) { DateTime.now }
|
43
|
+
context "time - 2.days < foo & bar < time + 2.days" do
|
44
|
+
let!(:model) { klass.create! foo_at: time - 1.days, bar_at: time + 1.days }
|
45
|
+
it { is_expected.to eq [model] }
|
46
|
+
end
|
47
|
+
context "time - 2.days < foo < time + 2.days < bar" do
|
48
|
+
let!(:model) { klass.create! foo_at: time + 1.days, bar_at: time + 3.days }
|
49
|
+
it { is_expected.to eq [] }
|
50
|
+
end
|
51
|
+
context "foo < time - 2.days < bar < time + 2.days" do
|
52
|
+
let!(:model) { klass.create! foo_at: time - 3.days, bar_at: time - 1.days }
|
53
|
+
it { is_expected.to eq [] }
|
54
|
+
end
|
55
|
+
context "foo < time - 2.days < time + 2.days < bar" do
|
56
|
+
let!(:model) { klass.create! foo_at: time - 3.days, bar_at: time + 3.days }
|
57
|
+
it { is_expected.to eq [] }
|
58
|
+
end
|
59
|
+
context "time - 2.days < time + 2.days < foo & bar" do
|
60
|
+
let!(:model) { klass.create! foo_at: time + 3.days, bar_at: time + 4.days }
|
61
|
+
it { is_expected.to eq [] }
|
62
|
+
end
|
63
|
+
context "foo & bar < time - 2.days < time + 2.days" do
|
64
|
+
let!(:model) { klass.create! foo_at: time - 4.days, bar_at: time - 3.days }
|
65
|
+
it { is_expected.to eq [] }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe "time_range_spec" do
|
4
|
+
klass = create_tmp_model "TestClass", "test_classes", foo_at: :datetime, bar_on: :date, xxx_time: :datetime, yyy_date: :date, zzz: :date do
|
5
|
+
create_time_scope :www, :zzz
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "::create_time_scope" do
|
9
|
+
subject { klass }
|
10
|
+
it do
|
11
|
+
is_expected.to respond_to :www_before
|
12
|
+
is_expected.to respond_to :www_after
|
13
|
+
is_expected.to respond_to :www_within
|
14
|
+
end
|
15
|
+
end
|
16
|
+
describe "::inherited" do
|
17
|
+
subject { klass }
|
18
|
+
it do
|
19
|
+
%w(foo bar xxx yyy).each do |verb|
|
20
|
+
is_expected.to respond_to "#{verb}_before"
|
21
|
+
is_expected.to respond_to "#{verb}_after"
|
22
|
+
is_expected.to respond_to "#{verb}_within"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
describe "::foo_before" do
|
27
|
+
subject { klass.foo_before(time) }
|
28
|
+
let(:time) { DateTime.now }
|
29
|
+
context "foo < time" do
|
30
|
+
let!(:model) { klass.create! foo_at: time + 1.days }
|
31
|
+
it { is_expected.to eq [] }
|
32
|
+
end
|
33
|
+
context "time < foo" do
|
34
|
+
let!(:model) { klass.create! foo_at: time - 1.days }
|
35
|
+
it { is_expected.to eq [model] }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
describe "::foo_after" do
|
39
|
+
subject { klass.foo_after(time) }
|
40
|
+
let(:time) { DateTime.now }
|
41
|
+
context "foo < time" do
|
42
|
+
let!(:model) { klass.create! foo_at: time + 1.days }
|
43
|
+
it { is_expected.to eq [model] }
|
44
|
+
end
|
45
|
+
context "time < foo" do
|
46
|
+
let!(:model) { klass.create! foo_at: time - 1.days }
|
47
|
+
it { is_expected.to eq [] }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
describe "::foo_within" do
|
51
|
+
subject { klass.foo_within(time - 2.days, time + 2.days) }
|
52
|
+
let(:time) { DateTime.now }
|
53
|
+
context "foo < -2 days < +2 days" do
|
54
|
+
let!(:model) { klass.create! foo_at: time - 3.days }
|
55
|
+
it { is_expected.to eq [] }
|
56
|
+
end
|
57
|
+
context "-2 days < foo < +2 days" do
|
58
|
+
let!(:model) { klass.create! foo_at: time }
|
59
|
+
it { is_expected.to eq [model] }
|
60
|
+
end
|
61
|
+
context "-2 days < +2 days < foo" do
|
62
|
+
let!(:model) { klass.create! foo_at: time + 3.days }
|
63
|
+
it { is_expected.to eq [] }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-time-scope
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daisuke Taniwaki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.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: '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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Time-Related Scope for ActiveRecord
|
70
|
+
email:
|
71
|
+
- daisuketaniwaki@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- VERSION
|
83
|
+
- activerecord-time-scope.gemspec
|
84
|
+
- lib/active_record/time_scope.rb
|
85
|
+
- lib/active_record/time_scope/scope_proxy.rb
|
86
|
+
- lib/active_record/time_scope/time_proxy.rb
|
87
|
+
- lib/active_record/time_scope/time_range_proxy.rb
|
88
|
+
- lib/active_record/time_scope/version.rb
|
89
|
+
- lib/activerecord-time-scope.rb
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
- spec/support/tmp_model.rb
|
92
|
+
- spec/time_scope/time_range_spec.rb
|
93
|
+
- spec/time_scope/time_spec.rb
|
94
|
+
homepage: https://github.com/dtaniwaki/activerecord-time-scope
|
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: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.2.2
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Time-Related Scope for ActiveRecord
|
118
|
+
test_files:
|
119
|
+
- spec/spec_helper.rb
|
120
|
+
- spec/support/tmp_model.rb
|
121
|
+
- spec/time_scope/time_range_spec.rb
|
122
|
+
- spec/time_scope/time_spec.rb
|