activerecord-time-scope 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 893d1a58b0e6307c6131616ea70208b1ba41d56e
4
- data.tar.gz: 5768fa3193147764439c093653462d94bc69ade3
3
+ metadata.gz: d9b24d60b301dd811d2f7d19cb7dc26daa4a317b
4
+ data.tar.gz: deb5f83f93fbe7abf81622c7f7179ac637dd988a
5
5
  SHA512:
6
- metadata.gz: 63af485ae9c2a98acee8337f7b2fd6ad9276796b3380036764e150b126b423799aa8a79756c02951e6608ccc04376d3ecbfa7b6f765fba80d4ebdd50c4f2a3fb
7
- data.tar.gz: 44fec36195a17d594a01831577811533494108348cee91293847124c7477629e4c3971169c2d509aa23ca2572fd80d4c15ef38a328a8258eae153d0cdc8c83e1
6
+ metadata.gz: b9c00b0093441c037cf0d64948c193151838a2f81d9687ccb2c8f74cfe62209a26861a85ddabc6447e39957a57427420c0ee70d6bf1e0abd4bff69ebb14e57b5
7
+ data.tar.gz: 11b32c3bd339e21765dd4da05f083c41e0911babebcc9c246f2a3165fd082553ffe014c9f0612748a913def8417e12182a9e0c5562e621340dfd3af4aa3591c7
@@ -10,7 +10,6 @@ env:
10
10
  - ACTIVE_RECORD_VERSION=3.2.0
11
11
  - ACTIVE_RECORD_VERSION=4.0.0
12
12
  - ACTIVE_RECORD_VERSION=4.1.0
13
- - ACTIVE_RECORD_VERSION=4.2.0
14
13
  - ACTIVE_RECORD_VERSION=4.2.0.beta1
15
14
 
16
15
  script: "bundle exec rake spec"
data/README.md CHANGED
@@ -22,14 +22,17 @@ And run `bundle install`.
22
22
 
23
23
  ### Time
24
24
 
25
- This gem automatically create scopes for time-related columns of your ActiveRecord model such as 'created_at'.
25
+ #### Auto
26
+
27
+ To create scopes for time-related columns of your ActiveRecord model such as 'created_at',
26
28
 
27
29
  ```ruby
28
30
  class Foo < ActiveRecord::Base
31
+ create_time_scopes
29
32
  end
30
33
  ```
31
34
 
32
- Then,
35
+ Then, these scopes will be available.
33
36
 
34
37
  ```ruby
35
38
  Foo.created_before 3.days.ago
@@ -39,6 +42,24 @@ Foo.created_within 3.days.ago, 3.days.from_now
39
42
 
40
43
  Any columns with `_at`, `_on`, `_time` and `_date` postfix are considered as time-related columns.
41
44
 
45
+ #### Manual
46
+
47
+ If the column name is not time-related, you can create the time scope manually.
48
+
49
+ ```ruby
50
+ class Foo < ActiveRecord::Base
51
+ create_time_scope :started, :created_at
52
+ end
53
+ ```
54
+
55
+ Then, these scopes will be available.
56
+
57
+ ```ruby
58
+ Foo.started_before 3.days.ago
59
+ Foo.started_after 3.days.ago
60
+ Foo.started_within 3.days.ago, 3.days.from_now
61
+ ```
62
+
42
63
  ### Time Range
43
64
 
44
65
  If you want scopes for time ranges, you can create it manually.
@@ -49,7 +70,7 @@ class Round < ActiveRecord::Base
49
70
  end
50
71
  ```
51
72
 
52
- Then,
73
+ Then, these scopes will be available.
53
74
 
54
75
  ```
55
76
  Round.run_before 3.days.ago
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
@@ -10,14 +10,11 @@ module ActiveRecord
10
10
  end
11
11
 
12
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
13
+ def create_time_scopes
14
+ column_names.each do |cn|
15
+ verb = cn.sub TIME_POSTFIX_REGEXP, ''
16
+ next if verb == cn
17
+ create_time_scope verb, cn
21
18
  end
22
19
  end
23
20
 
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module TimeScope
3
3
  class ScopeProxy
4
- def new(*args)
4
+ def self.new(*args)
5
5
  @proxies ||= {}
6
6
  @proxies[args] ||= super(*args)
7
7
  end
@@ -9,18 +9,18 @@ module ActiveRecord
9
9
  end
10
10
 
11
11
  def before(time, opts = {})
12
- operator = opts[:include_equal].to_s != '' ? '=<' : '<'
12
+ operator = opts[:include_equal].to_s != '' ? '<=' : '<'
13
13
  @model.where("#{@column_name} #{operator} ?", time)
14
14
  end
15
15
 
16
16
  def after(time, opts = {})
17
- operator = opts[:include_equal].to_s != '' ? '=<' : '<'
17
+ operator = opts[:include_equal].to_s != '' ? '<=' : '<'
18
18
  @model.where("? #{operator} #{@column_name}", time)
19
19
  end
20
20
 
21
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 != '' ? '=<' : '<'
22
+ from_operator = from_opts[:include_equal].to_s != '' ? '<=' : '<'
23
+ to_operator = to_opts[:include_equal].to_s != '' ? '<=' : '<'
24
24
  @model.where("? #{from_operator} #{@column_name} AND #{@column_name} #{to_operator} ?", from, to)
25
25
  end
26
26
  end
@@ -10,18 +10,18 @@ module ActiveRecord
10
10
  end
11
11
 
12
12
  def before(time, opts = {})
13
- operator = opts[:include_equal].to_s != '' ? '=<' : '<'
13
+ operator = opts[:include_equal].to_s != '' ? '<=' : '<'
14
14
  @model.where("#{@column_name1} #{operator} ? AND #{@column_name2} #{operator} ?", time, time)
15
15
  end
16
16
 
17
17
  def after(time, opts = {})
18
- operator = opts[:include_equal].to_s != '' ? '=<' : '<'
18
+ operator = opts[:include_equal].to_s != '' ? '<=' : '<'
19
19
  @model.where("? #{operator} #{@column_name1} AND ? #{operator} #{@column_name2}", time, time)
20
20
  end
21
21
 
22
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 != '' ? '=<' : '<'
23
+ from_operator = from_opts[:include_equal].to_s != '' ? '<=' : '<'
24
+ to_operator = to_opts[:include_equal].to_s != '' ? '<=' : '<'
25
25
  @model.where("? #{from_operator} #{@column_name1} AND #{@column_name2} #{to_operator} ?", from, to)
26
26
  end
27
27
  end
@@ -1,9 +1,12 @@
1
1
  require 'rubygems'
2
+ require 'database_cleaner'
2
3
  require 'simplecov'
3
4
  require 'coveralls'
4
5
  Coveralls.wear!
5
6
 
6
- require 'database_cleaner'
7
+ SimpleCov.start do
8
+ add_filter 'spec/'
9
+ end
7
10
 
8
11
  require 'activerecord-time-scope'
9
12
 
@@ -13,6 +16,7 @@ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
13
16
 
14
17
  RSpec.configure do |c|
15
18
  c.before :suite do
19
+ ActiveRecord::Migration.verbose = false
16
20
  DatabaseCleaner.clean_with :truncation
17
21
  end
18
22
  c.before :example do
@@ -1,7 +1,10 @@
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
1
+ def create_tmp_model(_columns = {}, &_block)
2
+ time = Time.now.to_f.to_s.gsub('.', '_')
3
+ _model_name = "TmpClass#{time}"
4
+ _table_name = "tmp_class#{time}s"
5
+ # if ActiveRecord::Base.connection.table_exists? _table_name
6
+ # ActiveRecord::Migration.drop_table _table_name
7
+ # end
5
8
  begin
6
9
  Object.send :remove_const, _model_name
7
10
  rescue NameError
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActiveRecord::TimeScope::ScopeProxy do
4
+ let(:klass) do
5
+ Class.new(described_class) do
6
+ def initialize(*args)
7
+ end
8
+ end
9
+ end
10
+
11
+ it "returns the same instance" do
12
+ ins = klass.new
13
+ expect(klass.new).to be ins
14
+ expect(klass.new).to be ins
15
+ expect(klass.new).to be ins
16
+ end
17
+ context "with arguments" do
18
+ it "returns the correspondent instances" do
19
+ ins1 = klass.new(1)
20
+ ins2 = klass.new(2)
21
+ expect(klass.new(1)).to be ins1
22
+ expect(klass.new(1)).to be ins1
23
+ expect(klass.new(1)).to be ins1
24
+ expect(klass.new(2)).to be ins2
25
+ expect(klass.new(2)).to be ins2
26
+ expect(klass.new(2)).to be ins2
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,167 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActiveRecord::TimeScope::TimeProxy do
4
+ let(:time) { DateTime.now }
5
+ let(:klass) do
6
+ create_tmp_model foo: :datetime, bar_on: :date do
7
+ create_time_scope :foo, :foo
8
+ create_time_scope :bar, :bar
9
+ end
10
+ end
11
+
12
+ describe "::foo_before" do
13
+ shared_examples "before" do
14
+ before do
15
+ klass.create! foo: time + 100.days
16
+ end
17
+ let(:options) { {} }
18
+ let(:a) { false }
19
+ let(:b) { false }
20
+ let(:c) { false }
21
+ subject { klass.foo_before(time, options) == [model] }
22
+ # .=...
23
+ # ..!..
24
+ context "foo < time" do
25
+ let!(:model) { klass.create! foo: time + 1.days }
26
+ it { is_expected.to be a }
27
+ end
28
+ # ...=.
29
+ # ..!..
30
+ context "time < foo" do
31
+ let!(:model) { klass.create! foo: time - 1.days }
32
+ it { is_expected.to be b }
33
+ end
34
+ # ..=..
35
+ # ..!..
36
+ context "time = foo" do
37
+ let!(:model) { klass.create! foo: time }
38
+ it { is_expected.to be c }
39
+ end
40
+ end
41
+ it_behaves_like "before" do
42
+ let(:b) { true }
43
+ end
44
+ context "with include_equal option" do
45
+ it_behaves_like "before" do
46
+ let(:options) { {include_equal: true} }
47
+ let(:b) { true }
48
+ let(:c) { true }
49
+ end
50
+ end
51
+ end
52
+ describe "::foo_after" do
53
+ shared_examples "after" do
54
+ before do
55
+ klass.create! foo: time - 100.days
56
+ end
57
+ let(:options) { {} }
58
+ let(:a) { false }
59
+ let(:b) { false }
60
+ let(:c) { false }
61
+ subject { klass.foo_after(time, options) == [model] }
62
+ # A
63
+ # .=...
64
+ # ..!..
65
+ context "foo < time" do
66
+ let!(:model) { klass.create! foo: time + 1.days }
67
+ it { is_expected.to be a }
68
+ end
69
+ # B
70
+ # ...=.
71
+ # ..!..
72
+ context "time < foo" do
73
+ let!(:model) { klass.create! foo: time - 1.days }
74
+ it { is_expected.to be b }
75
+ end
76
+ # C
77
+ # ..=..
78
+ # ..!..
79
+ context "time = foo" do
80
+ let!(:model) { klass.create! foo: time }
81
+ it { is_expected.to be c }
82
+ end
83
+ end
84
+ it_behaves_like "after" do
85
+ let(:a) { true }
86
+ end
87
+ context "with include_equal option" do
88
+ it_behaves_like "after" do
89
+ let(:options) { {include_equal: true} }
90
+ let(:a) { true }
91
+ let(:c) { true }
92
+ end
93
+ end
94
+ end
95
+ describe "::foo_within" do
96
+ shared_examples "within" do
97
+ before do
98
+ klass.create! foo: time - 100.days
99
+ end
100
+ let(:from_options) { {} }
101
+ let(:to_options) { {} }
102
+ let(:a) { false }
103
+ let(:b) { false }
104
+ let(:c) { false }
105
+ let(:d) { false }
106
+ let(:e) { false }
107
+ subject { klass.foo_within(time - 2.days, time + 2.days, from_options, to_options) == [model] }
108
+ # A
109
+ # .=...
110
+ # ..!!.
111
+ context "foo < time1 < time2" do
112
+ let!(:model) { klass.create! foo: time - 3.days }
113
+ it { is_expected.to be a }
114
+ end
115
+ # B
116
+ # .=...
117
+ # .!!..
118
+ context "foo = time1 < time2" do
119
+ let!(:model) { klass.create! foo: time - 2.days }
120
+ it { is_expected.to be b }
121
+ end
122
+ # C
123
+ # ..=..
124
+ # .!.!.
125
+ context "time1 < foo < time2" do
126
+ let!(:model) { klass.create! foo: time }
127
+ it { is_expected.to be c }
128
+ end
129
+ # D
130
+ # ...=.
131
+ # ..!!.
132
+ context "time1 < time2 = foo" do
133
+ let!(:model) { klass.create! foo: time + 2.days }
134
+ it { is_expected.to be d }
135
+ end
136
+ # E
137
+ # ...=.
138
+ # .!!..
139
+ context "time1 < time2 < foo" do
140
+ let!(:model) { klass.create! foo: time + 3.days }
141
+ it { is_expected.to be e }
142
+ end
143
+ end
144
+ it_behaves_like "within" do
145
+ let(:c) { true }
146
+ end
147
+ context "with include_equal option" do
148
+ it_behaves_like "within" do
149
+ let(:from_options) { {include_equal: true} }
150
+ let(:b) { true }
151
+ let(:c) { true }
152
+ end
153
+ it_behaves_like "within" do
154
+ let(:to_options) { {include_equal: true} }
155
+ let(:c) { true }
156
+ let(:d) { true }
157
+ end
158
+ it_behaves_like "within" do
159
+ let(:from_options) { {include_equal: true} }
160
+ let(:to_options) { {include_equal: true} }
161
+ let(:b) { true }
162
+ let(:c) { true }
163
+ let(:d) { true }
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,250 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActiveRecord::TimeScope::TimeRangeProxy do
4
+ let(:time) { DateTime.now }
5
+ let(:klass) do
6
+ create_tmp_model foo: :datetime, bar: :datetime do
7
+ create_time_range_scope :wow, :foo, :bar
8
+ end
9
+ end
10
+
11
+ describe "::wow_before" do
12
+ shared_examples "before" do
13
+ before do
14
+ klass.create! foo: time + 100.days, bar: time + 100.days
15
+ end
16
+ let(:options) { {} }
17
+ let(:a) { false }
18
+ let(:b) { false }
19
+ let(:c) { false }
20
+ let(:d) { false }
21
+ let(:e) { false }
22
+ subject { klass.wow_before(time, options) == [model] }
23
+ # A
24
+ # .=====.
25
+ # ......!
26
+ context "foo < bar < time" do
27
+ let!(:model) { klass.create! foo: time - 2.days, bar: time - 1.days }
28
+ it { is_expected.to be a }
29
+ end
30
+ # B
31
+ # .=====.
32
+ # .....!.
33
+ context "foo < bar = time" do
34
+ let!(:model) { klass.create! foo: time - 2.days, bar: time }
35
+ it { is_expected.to be b }
36
+ end
37
+ # C
38
+ # .=====.
39
+ # !......
40
+ context "time < foo < bar" do
41
+ let!(:model) { klass.create! foo: time + 1.days, bar: time + 2.days }
42
+ it { is_expected.to be c }
43
+ end
44
+ # D
45
+ # .=====.
46
+ # .!.....
47
+ context "time = foo < bar" do
48
+ let!(:model) { klass.create! foo: time, bar: time + 2.days }
49
+ it { is_expected.to be d }
50
+ end
51
+ # E
52
+ # .=====.
53
+ # ...!...
54
+ context "foo < time < bar" do
55
+ let!(:model) { klass.create! foo: time - 1.days, bar: time + 1.days }
56
+ it { is_expected.to be e }
57
+ end
58
+ end
59
+ it_behaves_like "before" do
60
+ let(:a) { true }
61
+ end
62
+ context "with include_equal option" do
63
+ it_behaves_like "before" do
64
+ let(:options) { {include_equal: true} }
65
+ let(:a) { true }
66
+ let(:b) { true }
67
+ end
68
+ end
69
+ end
70
+ describe "::wow_after" do
71
+ shared_examples "after" do
72
+ before do
73
+ klass.create! foo: time - 100.days, bar: time - 100.days
74
+ end
75
+ let(:options) { {} }
76
+ let(:a) { false }
77
+ let(:b) { false }
78
+ let(:c) { false }
79
+ let(:d) { false }
80
+ let(:e) { false }
81
+ subject { klass.wow_after(time, options) == [model] }
82
+ # A
83
+ # .=====.
84
+ # ......!
85
+ context "foo < bar < time" do
86
+ let!(:model) { klass.create! foo: time - 2.days, bar: time - 1.days }
87
+ it { is_expected.to be a }
88
+ end
89
+ # B
90
+ # .=====.
91
+ # .....!.
92
+ context "foo < bar = time" do
93
+ let!(:model) { klass.create! foo: time - 2.days, bar: time }
94
+ it { is_expected.to be b }
95
+ end
96
+ # C
97
+ # .=====.
98
+ # !......
99
+ context "time < foo < bar" do
100
+ let!(:model) { klass.create! foo: time + 1.days, bar: time + 2.days }
101
+ it { is_expected.to be c }
102
+ end
103
+ # D
104
+ # .=====.
105
+ # .!.....
106
+ context "time = foo < bar" do
107
+ let!(:model) { klass.create! foo: time, bar: time + 2.days }
108
+ it { is_expected.to be d }
109
+ end
110
+ # E
111
+ # .=====.
112
+ # ...!...
113
+ context "foo < time < bar" do
114
+ let!(:model) { klass.create! foo: time - 1.days, bar: time + 1.days }
115
+ it { is_expected.to be e }
116
+ end
117
+ end
118
+ it_behaves_like "after" do
119
+ let(:c) { true }
120
+ end
121
+ context "with include_equal option" do
122
+ it_behaves_like "after" do
123
+ let(:options) { {include_equal: true} }
124
+ let(:c) { true }
125
+ let(:d) { true }
126
+ end
127
+ end
128
+ end
129
+ describe "::wow_within" do
130
+ shared_examples "within" do
131
+ before do
132
+ klass.create! foo: time + 100.days, bar: time + 100.days
133
+ end
134
+ let(:from_options) { {} }
135
+ let(:to_options) { {} }
136
+ let(:a) { false }
137
+ let(:b) { false }
138
+ let(:c) { false }
139
+ let(:d) { false }
140
+ let(:e) { false }
141
+ let(:f) { false }
142
+ let(:g) { false }
143
+ let(:h) { false }
144
+ let(:i) { false }
145
+ let(:j) { false }
146
+ let(:k) { false }
147
+ subject { klass.wow_within(time - 2.days, time + 2.days, from_options, to_options) == [model] }
148
+ # A
149
+ # .=====.
150
+ # .!....!
151
+ context "time1 = foo < bar < time2" do
152
+ let!(:model) { klass.create! foo: time - 2.days, bar: time + 1.days }
153
+ it { is_expected.to be a }
154
+ end
155
+ # B
156
+ # .=====.
157
+ # !.....!
158
+ context "time1 < foo < bar < time2" do
159
+ let!(:model) { klass.create! foo: time - 1.days, bar: time + 1.days }
160
+ it { is_expected.to be b }
161
+ end
162
+ # C
163
+ # .=====.
164
+ # !....!.
165
+ context "time1 < foo < bar = time2" do
166
+ let!(:model) { klass.create! foo: time - 1.days, bar: time + 2.days }
167
+ it { is_expected.to be c }
168
+ end
169
+ # D
170
+ # .=====.
171
+ # !..!...
172
+ context "time1 < foo < time2 < bar" do
173
+ let!(:model) { klass.create! foo: time + 1.days, bar: time + 3.days }
174
+ it { is_expected.to be d }
175
+ end
176
+ # E
177
+ # .=====.
178
+ # ...!..!
179
+ context "foo < time1 < bar < time2" do
180
+ let!(:model) { klass.create! foo: time - 3.days, bar: time - 1.days }
181
+ it { is_expected.to be e }
182
+ end
183
+ # F
184
+ # ..=====
185
+ # !!.....
186
+ context "time1 < time2 < foo < bar" do
187
+ let!(:model) { klass.create! foo: time + 3.days, bar: time + 4.days }
188
+ it { is_expected.to be f }
189
+ end
190
+ # G
191
+ # ..=====
192
+ # !.!....
193
+ context "time1 < time2 = foo < bar" do
194
+ let!(:model) { klass.create! foo: time + 2.days, bar: time + 4.days }
195
+ it { is_expected.to be g }
196
+ end
197
+ # H
198
+ # =====..
199
+ # .....!!
200
+ context "foo < bar < time1 < time2" do
201
+ let!(:model) { klass.create! foo: time - 4.days, bar: time - 3.days }
202
+ it { is_expected.to be h }
203
+ end
204
+ # I
205
+ # =====..
206
+ # ....!.!
207
+ context "foo < bar = time1 < time2" do
208
+ let!(:model) { klass.create! foo: time - 4.days, bar: time - 2.days }
209
+ it { is_expected.to be i }
210
+ end
211
+ # J
212
+ # .=====.
213
+ # .!...!.
214
+ context "foo = time1 < time2 = bar" do
215
+ let!(:model) { klass.create! foo: time - 2.days, bar: time + 2.days }
216
+ it { is_expected.to be j }
217
+ end
218
+ # K
219
+ # .=====.
220
+ # ..!.!..
221
+ context "foo < time1 < time2 < bar" do
222
+ let!(:model) { klass.create! foo: time - 3.days, bar: time + 3.days }
223
+ it { is_expected.to be k }
224
+ end
225
+ end
226
+ it_behaves_like "within" do
227
+ let(:b) { true }
228
+ end
229
+ context "with include_equal option" do
230
+ it_behaves_like "within" do
231
+ let(:from_options) { {include_equal: true} }
232
+ let(:a) { true }
233
+ let(:b) { true }
234
+ end
235
+ it_behaves_like "within" do
236
+ let(:to_options) { {include_equal: true} }
237
+ let(:b) { true }
238
+ let(:c) { true }
239
+ end
240
+ it_behaves_like "within" do
241
+ let(:from_options) { {include_equal: true} }
242
+ let(:to_options) { {include_equal: true} }
243
+ let(:a) { true }
244
+ let(:b) { true }
245
+ let(:c) { true }
246
+ let(:j) { true }
247
+ end
248
+ end
249
+ end
250
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActiveRecord::TimeScope do
4
+ describe "::create_time_scopes" do
5
+ subject {
6
+ create_tmp_model foo_at: :datetime, bar_on: :date, xxx_time: :datetime, yyy_date: :date, zzz: :date do
7
+ create_time_scopes
8
+ end
9
+ }
10
+ it do
11
+ %w(foo bar xxx yyy).each do |key|
12
+ is_expected.to respond_to "#{key}_before"
13
+ is_expected.to respond_to "#{key}_after"
14
+ is_expected.to respond_to "#{key}_within"
15
+ end
16
+ %w(zzz).each do |key|
17
+ is_expected.not_to respond_to "#{key}_before"
18
+ is_expected.not_to respond_to "#{key}_after"
19
+ is_expected.not_to respond_to "#{key}_within"
20
+ end
21
+ end
22
+ end
23
+ describe "::create_time_scope" do
24
+ subject {
25
+ create_tmp_model zzz: :date do
26
+ create_time_scope :www, :zzz
27
+ end
28
+ }
29
+ it do
30
+ is_expected.to respond_to :www_before
31
+ is_expected.to respond_to :www_after
32
+ is_expected.to respond_to :www_within
33
+ is_expected.not_to respond_to :zzz_before
34
+ is_expected.not_to respond_to :zzz_after
35
+ is_expected.not_to respond_to :zzz_within
36
+ end
37
+ end
38
+ describe "::create_time_range_scope" do
39
+ subject {
40
+ create_tmp_model aaa: :date, bbb: :date do
41
+ create_time_range_scope :xxx, :aaa, :bbb
42
+ end
43
+ }
44
+ it do
45
+ is_expected.to respond_to :xxx_before
46
+ is_expected.to respond_to :xxx_after
47
+ is_expected.to respond_to :xxx_within
48
+ is_expected.not_to respond_to :aaa_before
49
+ is_expected.not_to respond_to :aaa_after
50
+ is_expected.not_to respond_to :aaa_within
51
+ is_expected.not_to respond_to :bbb_before
52
+ is_expected.not_to respond_to :bbb_after
53
+ is_expected.not_to respond_to :bbb_within
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-time-scope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daisuke Taniwaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-21 00:00:00.000000000 Z
11
+ date: 2014-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -89,8 +89,10 @@ files:
89
89
  - lib/activerecord-time-scope.rb
90
90
  - spec/spec_helper.rb
91
91
  - spec/support/tmp_model.rb
92
- - spec/time_scope/time_range_spec.rb
93
- - spec/time_scope/time_spec.rb
92
+ - spec/time_scope/scope_proxy_spec.rb
93
+ - spec/time_scope/time_proxy_spec.rb
94
+ - spec/time_scope/time_range_proxy_spec.rb
95
+ - spec/time_scope_spec.rb
94
96
  homepage: https://github.com/dtaniwaki/activerecord-time-scope
95
97
  licenses:
96
98
  - MIT
@@ -118,5 +120,7 @@ summary: Time-Related Scope for ActiveRecord
118
120
  test_files:
119
121
  - spec/spec_helper.rb
120
122
  - spec/support/tmp_model.rb
121
- - spec/time_scope/time_range_spec.rb
122
- - spec/time_scope/time_spec.rb
123
+ - spec/time_scope/scope_proxy_spec.rb
124
+ - spec/time_scope/time_proxy_spec.rb
125
+ - spec/time_scope/time_range_proxy_spec.rb
126
+ - spec/time_scope_spec.rb
@@ -1,68 +0,0 @@
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
@@ -1,66 +0,0 @@
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