include_date_scopes 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e0f891fa15dbec344fb06873f3a5642f41a5bd03
4
+ data.tar.gz: 31ab327e5716822e217f2ad00d009d284da1fd9d
5
+ SHA512:
6
+ metadata.gz: f6dd984249e0aaa39f190487989c6ae63d0a4ac9a06d365507fe7428fa1b54546476e2c0a21becbde6fb2a053f25b81754fbee7657538b978f332ba4cbfb0b6b
7
+ data.tar.gz: ccc4476d4f40a75005b0ca3b67f711eceab8c694e03b559ddeb1ca6e4c8e2f3085447c3b832a3b5b4c46eab8aebb9109613d5ba1c50d7cb87c010a681e118170
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ # VIM stuff
21
+ *.swp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ include_date_scopes
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ # *************************************
6
+ # * LOCAL GEMFILE IMPORT
7
+ # * If you have any local gems to include, create a new Gemfile at ~/.Gemfile
8
+ # * This code will import anything in there.
9
+ # *************************************
10
+ if File.exists? File.join(Dir.home,'.Gemfile')
11
+ eval File.read File.join(Dir.home,'.Gemfile')
12
+ end
13
+
data/Gemfile.lock ADDED
@@ -0,0 +1,145 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ include_date_scopes (0.0.0)
5
+ activerecord (< 4)
6
+ railties
7
+ squeel
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ actionpack (3.2.17)
13
+ activemodel (= 3.2.17)
14
+ activesupport (= 3.2.17)
15
+ builder (~> 3.0.0)
16
+ erubis (~> 2.7.0)
17
+ journey (~> 1.0.4)
18
+ rack (~> 1.4.5)
19
+ rack-cache (~> 1.2)
20
+ rack-test (~> 0.6.1)
21
+ sprockets (~> 2.2.1)
22
+ activemodel (3.2.17)
23
+ activesupport (= 3.2.17)
24
+ builder (~> 3.0.0)
25
+ activerecord (3.2.17)
26
+ activemodel (= 3.2.17)
27
+ activesupport (= 3.2.17)
28
+ arel (~> 3.0.2)
29
+ tzinfo (~> 0.3.29)
30
+ activesupport (3.2.17)
31
+ i18n (~> 0.6, >= 0.6.4)
32
+ multi_json (~> 1.0)
33
+ arel (3.0.3)
34
+ better_errors (1.1.0)
35
+ coderay (>= 1.0.0)
36
+ erubis (>= 2.6.6)
37
+ builder (3.0.4)
38
+ bullet (4.8.0)
39
+ activesupport
40
+ uniform_notifier (>= 1.4.0)
41
+ coderay (1.1.0)
42
+ columnize (0.3.6)
43
+ daemons (1.1.9)
44
+ debugger (1.6.6)
45
+ columnize (>= 0.3.1)
46
+ debugger-linecache (~> 1.2.0)
47
+ debugger-ruby_core_source (~> 1.3.2)
48
+ debugger-linecache (1.2.0)
49
+ debugger-ruby_core_source (1.3.2)
50
+ diff-lcs (1.2.5)
51
+ erubis (2.7.0)
52
+ eventmachine (1.0.3)
53
+ ffi (1.9.3)
54
+ hike (1.2.3)
55
+ i18n (0.6.9)
56
+ interactive_editor (0.0.10)
57
+ spoon (>= 0.0.1)
58
+ journey (1.0.4)
59
+ json (1.8.1)
60
+ method_source (0.8.2)
61
+ multi_json (1.9.2)
62
+ polyamorous (0.6.4)
63
+ activerecord (>= 3.0)
64
+ pry (0.9.12.6)
65
+ coderay (~> 1.0)
66
+ method_source (~> 0.8)
67
+ slop (~> 3.4)
68
+ pry-debugger (0.2.2)
69
+ debugger (~> 1.3)
70
+ pry (~> 0.9.10)
71
+ pry-doc (0.6.0)
72
+ pry (~> 0.9)
73
+ yard (~> 0.8)
74
+ rack (1.4.5)
75
+ rack-cache (1.2)
76
+ rack (>= 0.4)
77
+ rack-mini-profiler (0.9.1)
78
+ rack (>= 1.1.3)
79
+ rack-ssl (1.3.4)
80
+ rack
81
+ rack-test (0.6.2)
82
+ rack (>= 1.0)
83
+ railties (3.2.17)
84
+ actionpack (= 3.2.17)
85
+ activesupport (= 3.2.17)
86
+ rack-ssl (~> 1.3.2)
87
+ rake (>= 0.8.7)
88
+ rdoc (~> 3.4)
89
+ thor (>= 0.14.6, < 2.0)
90
+ rake (10.2.2)
91
+ rdoc (3.12.2)
92
+ json (~> 1.4)
93
+ rspec-core (2.14.8)
94
+ rspec-expectations (2.14.5)
95
+ diff-lcs (>= 1.1.3, < 2.0)
96
+ rspec-mocks (2.14.6)
97
+ rspec-rails (2.14.2)
98
+ actionpack (>= 3.0)
99
+ activemodel (>= 3.0)
100
+ activesupport (>= 3.0)
101
+ railties (>= 3.0)
102
+ rspec-core (~> 2.14.0)
103
+ rspec-expectations (~> 2.14.0)
104
+ rspec-mocks (~> 2.14.0)
105
+ slop (3.5.0)
106
+ spoon (0.0.4)
107
+ ffi
108
+ sprockets (2.2.2)
109
+ hike (~> 1.2)
110
+ multi_json (~> 1.0)
111
+ rack (~> 1.0)
112
+ tilt (~> 1.1, != 1.3.0)
113
+ sqlite3 (1.3.9)
114
+ squeel (1.1.1)
115
+ activerecord (>= 3.0)
116
+ activesupport (>= 3.0)
117
+ polyamorous (~> 0.6.0)
118
+ thin (1.6.2)
119
+ daemons (>= 1.0.9)
120
+ eventmachine (>= 1.0.0)
121
+ rack (>= 1.0.0)
122
+ thor (0.19.1)
123
+ tilt (1.4.1)
124
+ timecop (0.7.1)
125
+ tzinfo (0.3.39)
126
+ uniform_notifier (1.4.0)
127
+ yard (0.8.7.4)
128
+
129
+ PLATFORMS
130
+ ruby
131
+
132
+ DEPENDENCIES
133
+ better_errors
134
+ bullet
135
+ debugger
136
+ include_date_scopes!
137
+ interactive_editor
138
+ pry
139
+ pry-debugger
140
+ pry-doc
141
+ rack-mini-profiler
142
+ rspec-rails
143
+ sqlite3
144
+ thin
145
+ timecop
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ include_date_scopes
2
+ ===================
3
+
4
+ Module for mixing in date related scopes to ActiveRecord models
@@ -0,0 +1,28 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "include_date_scopes/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'include_date_scopes'
6
+ s.version = IncludeDateScopes::VERSION.dup
7
+ s.authors = ['Sean Todd']
8
+ s.email = ['iphone.reply@gmail.com']
9
+ s.homepage = 'http://github.com/descentintomael/include_date_scopes'
10
+ s.summary = 'Include scopes for searching on a date column'
11
+ s.description = <<-DESCRIPTION
12
+ Add in ActiveRelation scopes that are common across all models. You can also
13
+ add in a prefix to those scopes so you can have multiple per model. See README
14
+ for more info.
15
+ DESCRIPTION
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "squeel"
23
+ s.add_dependency "activerecord", '< 4'
24
+ s.add_dependency "railties"
25
+ s.add_development_dependency "rspec-rails"
26
+ s.add_development_dependency "sqlite3"
27
+ s.add_development_dependency 'timecop'
28
+ end
@@ -0,0 +1,5 @@
1
+ require 'include_date_scopes'
2
+
3
+ class ActiveRecord::Base
4
+ include IncludeDateScopes::DateScopes
5
+ end
@@ -0,0 +1,381 @@
1
+ # Add the below date based scopes to a model.
2
+ #
3
+ # between(start_date_or_time, stop_date_or_time)
4
+ # on_or_before(date)
5
+ # before(date)
6
+ # on_or_after(date)
7
+ # after(date)
8
+ # on(date)
9
+ # day(date)
10
+ # last_24_hours() * DateTime only
11
+ # last_hour() * DateTime only
12
+ # last_week()
13
+ # last_month()
14
+ # today()
15
+ # yesterday()
16
+ # most_recent() * this is an order by, not a filter
17
+ #
18
+ # Example usage (from Payment):
19
+ #
20
+ # include_date_scopes_for :payment_date # default date scope -- ex: Payment.on(date)
21
+ # include_date_scopes_for :net_payment_settlement_date, true # prepend field name -- ex: Payment.net_payment_settlement_date_on(date)
22
+ #
23
+ #
24
+ require 'squeel'
25
+
26
+ module IncludeDateScopes
27
+ module DateScopes
28
+ extend ActiveSupport::Concern
29
+ module ClassMethods
30
+ def include_date_scopes
31
+ include_date_scopes_for :created_at
32
+ end
33
+
34
+ def include_date_scopes_for(column, prepend_name = false)
35
+ return unless self.table_exists?
36
+ if self.columns_hash[column.to_s].type == :datetime
37
+ define_timestamp_scopes_for column, prepend_name
38
+ elsif self.columns_hash[column.to_s].type == :date
39
+ define_date_scopes_for column, prepend_name
40
+ end
41
+ end
42
+
43
+ def include_named_date_scopes_for(column)
44
+ include_date_scopes_for column, true
45
+ end
46
+
47
+ protected
48
+
49
+ def define_timestamp_scopes_for(column_name, prepend_name = false)
50
+ prefix = prepend_name ? "#{column_name}_" : ""
51
+
52
+ define_singleton_method :"#{prefix}between" do |start_date_or_time, stop_date_or_time|
53
+ start_time = (start_date_or_time.is_a?(Date) ? start_date_or_time.to_time : start_date_or_time)
54
+ stop_time = (stop_date_or_time.is_a?(Date) ? stop_date_or_time.to_time + 1.day : stop_date_or_time )
55
+ where{(__send__(column_name).gte start_time) & (__send__(column_name).lt stop_time)}
56
+ end
57
+
58
+ define_singleton_method :"#{prefix}on_or_before_date" do |date|
59
+ where{ __send__(column_name).lt date.to_date.tomorrow.midnight.gmtime }
60
+ end
61
+
62
+ define_singleton_method :"#{prefix}on_or_before" do |time|
63
+ where{__send__(column_name).lte (time.is_a?(Date) ? time.to_time : time)}
64
+ end
65
+
66
+ define_singleton_method :"#{prefix}before" do |time|
67
+ where{__send__(column_name).lt (time.is_a?(Date) ? time.to_time : time)}
68
+ end
69
+
70
+ define_singleton_method :"#{prefix}on_or_after_date" do |time|
71
+ where{__send__(column_name).gte time.to_date.midnight.gmtime }
72
+ end
73
+
74
+ define_singleton_method :"#{prefix}on_or_after" do |time|
75
+ where{__send__(column_name).gte (time.is_a?(Date) ? time.to_time + 1.day : time )}
76
+ end
77
+
78
+ define_singleton_method :"#{prefix}after" do |time|
79
+ where{__send__(column_name).gt (time.is_a?(Date) ? time.to_time + 1.day : time )}
80
+ end
81
+
82
+ define_singleton_method :"#{prefix}on" do |day|
83
+ __send__(:"#{prefix}between", day.midnight, (day + 1.day).midnight)
84
+ end
85
+
86
+ define_singleton_method :"#{prefix}day" do |day|
87
+ __send__(:"#{prefix}on", day)
88
+ end
89
+
90
+ define_singleton_method :"#{prefix}last_24_hours" do
91
+ __send__(:"#{prefix}after", 24.hours.ago)
92
+ end
93
+
94
+ define_singleton_method :"#{prefix}last_hour" do
95
+ __send__(:"#{prefix}after", 1.hour.ago)
96
+ end
97
+
98
+ define_singleton_method :"#{prefix}last_week" do
99
+ __send__(:"#{prefix}after", 1.week.ago)
100
+ end
101
+
102
+ define_singleton_method :"#{prefix}last_30days" do
103
+ __send__(:"#{prefix}after", 30.days.ago)
104
+ end
105
+
106
+ define_singleton_method :"#{prefix}today" do
107
+ __send__(:"#{prefix}on", Date.today)
108
+ end
109
+
110
+ define_singleton_method :"#{prefix}this_week" do
111
+ __send__(:"#{prefix}between", (Date.today - Date.today.wday.days).midnight, (Date.today + 1.day).midnight)
112
+ end
113
+
114
+ define_singleton_method :"#{prefix}this_month" do
115
+ __send__(:"#{prefix}between", (Date.today.at_beginning_of_month).midnight, (Date.today + 1.day).midnight)
116
+ end
117
+
118
+ define_singleton_method :"#{prefix}yesterday" do
119
+ __send__(:"#{prefix}on", Date.yesterday)
120
+ end
121
+
122
+ define_singleton_method :"#{prefix}most_recent" do
123
+ order("#{column_name} desc")
124
+ end
125
+
126
+ end
127
+
128
+ def define_date_scopes_for(column_name, prepend_name = false)
129
+ prefix = prepend_name ? "#{column_name}_" : ""
130
+
131
+ define_singleton_method :"#{prefix}between" do |start_date, stop_date|
132
+ where{(__send__(column_name).gte start_date) & (__send__(column_name).lte stop_date)}
133
+ end
134
+
135
+ define_singleton_method :"#{prefix}on_or_before" do |date|
136
+ where{ __send__(column_name).lte date }
137
+ end
138
+
139
+ define_singleton_method :"#{prefix}before" do |date|
140
+ where{__send__(column_name).lt date}
141
+ end
142
+
143
+ define_singleton_method :"#{prefix}on_or_after" do |date|
144
+ where{__send__(column_name).gte date}
145
+ end
146
+
147
+ define_singleton_method :"#{prefix}after" do |date|
148
+ where{__send__(column_name).gt date}
149
+ end
150
+
151
+ define_singleton_method :"#{prefix}on" do |date|
152
+ where{__send__(column_name).eq date}
153
+ end
154
+
155
+ define_singleton_method :"#{prefix}day" do |day|
156
+ __send__(:"#{prefix}on", day)
157
+ end
158
+
159
+ define_singleton_method :"#{prefix}today" do
160
+ __send__(:"#{prefix}on", Date.today)
161
+ end
162
+
163
+ define_singleton_method :"#{prefix}last_week" do
164
+ __send__(:"#{prefix}after", 1.week.ago)
165
+ end
166
+
167
+ define_singleton_method :"#{prefix}last_30days" do
168
+ __send__(:"#{prefix}after", 30.days.ago)
169
+ end
170
+
171
+ define_singleton_method :"#{prefix}yesterday" do
172
+ __send__(:"#{prefix}on", Date.yesterday)
173
+ end
174
+
175
+ define_singleton_method :"#{prefix}most_recent" do
176
+ order("#{column_name} desc")
177
+ end
178
+
179
+ end
180
+ end
181
+ end
182
+ end
183
+ #
184
+ # module DateScopes
185
+ # extend ActiveSupport::Concern
186
+ #
187
+ # module ClassMethods
188
+ # def include_date_scopes
189
+ # include_date_scopes_for :created_at
190
+ # end
191
+ #
192
+ # def include_date_scopes_for(column, prepend_name = false)
193
+ # return unless self.table_exists?
194
+ # if self.columns_hash[column.to_s].try(:type) == :datetime
195
+ # define_timestamp_scopes_for column, prepend_name
196
+ # elsif self.columns_hash[column.to_s].try(:type) == :date
197
+ # define_date_scopes_for column, prepend_name
198
+ # end
199
+ # end
200
+ #
201
+ # def include_named_date_scopes_for(column)
202
+ # include_date_scopes_for column, true
203
+ # end
204
+ #
205
+ # protected
206
+ #
207
+ # def process_timestamp_argument(arg, inclusive_range = false)
208
+ # if arg.is_a?(Time) || arg.is_a?(DateTime)
209
+ # return arg
210
+ # elsif arg.is_a?(Date) && inclusive_range
211
+ # return arg + 1.day
212
+ # elsif arg.is_a?(Date) && !inclusive_range
213
+ # return arg
214
+ # else
215
+ # raise ArgumentError, "Unexpected argument class for timestamp scope: #{arg.class.name}"
216
+ # end
217
+ # end
218
+ #
219
+ # def process_date_argument(arg)
220
+ # if arg.is_a?(Time) || arg.is_a?(DateTime)
221
+ # return arg.to_date
222
+ # elsif arg.is_a?(Date)
223
+ # return arg
224
+ # else
225
+ # raise ArgumentError, "Unexpected argument class for timestamp scope: #{arg.class.name}"
226
+ # end
227
+ # end
228
+ #
229
+ # def define_timestamp_scopes_for(column_name, prepend_name = false)
230
+ # prefix = prepend_name ? "#{column_name}_" : ""
231
+ #
232
+ # define_singleton_method :"#{prefix}before" do |time,inclusive=false|
233
+ # time = process_timestamp_argument(time, inclusive)
234
+ # if inclusive
235
+ # where{__send__(column_name).lte time}
236
+ # else
237
+ # where{__send__(column_name).lt time}
238
+ # end
239
+ # end
240
+ #
241
+ # define_singleton_method :"#{prefix}after" do |time, inclusive=false|
242
+ # time = process_timestamp_argument(time, inclusive)
243
+ # if inclusive
244
+ # where{__send__(column_name).gte time}
245
+ # else
246
+ # where{__send__(column_name).gt time}
247
+ # end
248
+ # end
249
+ #
250
+ # define_singleton_method :"#{prefix}between" do |start_time, stop_time|
251
+ # on_or_after(start_time).on_or_before(stop_time)
252
+ # end
253
+ #
254
+ # define_singleton_method :"#{prefix}on_or_after" do |time|
255
+ # after(time,true)
256
+ # end
257
+ #
258
+ # define_singleton_method :"#{prefix}on_or_before" do |time|
259
+ # before(time,true)
260
+ # end
261
+ #
262
+ # define_singleton_method :"#{prefix}on_or_before_date" do |time|
263
+ # on_or_before(time.to_date)
264
+ # end
265
+ #
266
+ # define_singleton_method :"#{prefix}on_or_after_date" do |time|
267
+ # on_or_after(time)
268
+ # end
269
+ #
270
+ # define_singleton_method :"#{prefix}on" do |day|
271
+ # between(day.midnight, (day + 1.day).midnight)
272
+ # end
273
+ #
274
+ # define_singleton_method :"#{prefix}day" do |day|
275
+ # on(day)
276
+ # end
277
+ #
278
+ # define_singleton_method :"#{prefix}last_24_hours" do
279
+ # after(24.hours.ago)
280
+ # end
281
+ #
282
+ # define_singleton_method :"#{prefix}last_hour" do
283
+ # after(1.hour.ago)
284
+ # end
285
+ #
286
+ # define_singleton_method :"#{prefix}last_week" do
287
+ # after(1.week.ago)
288
+ # end
289
+ #
290
+ # define_singleton_method :"#{prefix}last_30days" do
291
+ # after(30.days.ago)
292
+ # end
293
+ #
294
+ # define_singleton_method :"#{prefix}today" do
295
+ # on(Date.today)
296
+ # end
297
+ #
298
+ # define_singleton_method :"#{prefix}this_week" do
299
+ # between((Date.today - Date.today.wday.days).midnight, (Date.today + 1.day).midnight)
300
+ # end
301
+ #
302
+ # define_singleton_method :"#{prefix}this_month" do
303
+ # between((Date.today.at_beginning_of_month).midnight, (Date.today + 1.day).midnight)
304
+ # end
305
+ #
306
+ # define_singleton_method :"#{prefix}yesterday" do
307
+ # on(Date.yesterday)
308
+ # end
309
+ #
310
+ # define_singleton_method :"#{prefix}most_recent" do
311
+ # order("#{column_name} desc")
312
+ # end
313
+ #
314
+ # end
315
+ #
316
+ # def define_date_scopes_for(column_name, prepend_name = false)
317
+ # prefix = prepend_name ? "#{column_name}_" : ""
318
+ #
319
+ # define_singleton_method :"#{prefix}before" do |date,inclusive=false|
320
+ # date = process_date_argument(date)
321
+ # if inclusive
322
+ # where{__send__(column_name).lte date}
323
+ # else
324
+ # where{__send__(column_name).lt date}
325
+ # end
326
+ # end
327
+ #
328
+ # define_singleton_method :"#{prefix}after" do |date, inclusive=false|
329
+ # date = process_date_argument(date)
330
+ # if inclusive
331
+ # where{__send__(column_name).gte date}
332
+ # else
333
+ # where{__send__(column_name).gt date}
334
+ # end
335
+ # end
336
+ #
337
+ # define_singleton_method :"#{prefix}on" do |date|
338
+ # date = process_date_argument(date)
339
+ # where{__send__(column_name).eq date}
340
+ # end
341
+ #
342
+ # define_singleton_method :"#{prefix}between" do |start_date, stop_date|
343
+ # on_or_after(start_date).on_or_before(stop_date)
344
+ # end
345
+ #
346
+ # define_singleton_method :"#{prefix}on_or_before" do |date|
347
+ # before(date,true)
348
+ # end
349
+ #
350
+ # define_singleton_method :"#{prefix}on_or_after" do |date|
351
+ # after(date,true)
352
+ # end
353
+ #
354
+ # define_singleton_method :"#{prefix}day" do |day|
355
+ # on(day)
356
+ # end
357
+ #
358
+ # define_singleton_method :"#{prefix}today" do
359
+ # on(Date.today)
360
+ # end
361
+ #
362
+ # define_singleton_method :"#{prefix}last_week" do
363
+ # after(1.week.ago)
364
+ # end
365
+ #
366
+ # define_singleton_method :"#{prefix}last_30days" do
367
+ # after(30.days.ago)
368
+ # end
369
+ #
370
+ # define_singleton_method :"#{prefix}yesterday" do
371
+ # on(Date.yesterday)
372
+ # end
373
+ #
374
+ # define_singleton_method :"#{prefix}most_recent" do
375
+ # order("#{column_name} desc")
376
+ # end
377
+ #
378
+ # end
379
+ # end
380
+ # end
381
+
@@ -0,0 +1,4 @@
1
+
2
+ module IncludeDateScopes
3
+ VERSION = '0.9.0'.freeze
4
+ end
@@ -0,0 +1,6 @@
1
+
2
+ module IncludeDateScopes
3
+ end
4
+
5
+ require 'include_date_scopes/date_scopes'
6
+ require_relative 'railtie'
data/lib/railtie.rb ADDED
@@ -0,0 +1,9 @@
1
+ module IncludeDateScopes
2
+ class Railtie < ::Rails::Railtie
3
+ initializer 'include_date_scopes' do |app|
4
+ ActiveSupport.on_load :active_record do
5
+ require 'include_date_scopes/active_record'
6
+ end
7
+ end
8
+ end
9
+ end
File without changes
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Post" do
4
+ describe 'default date scopes' do
5
+ before(:all) do
6
+ define_model_class do
7
+ include_date_scopes
8
+ end
9
+ end
10
+
11
+ it_behaves_like "with basic date scopes"
12
+ end
13
+
14
+ describe 'date scopes on another column' do
15
+ before(:all) do
16
+ define_model_class do
17
+ include_date_scopes_for :updated_at
18
+ end
19
+ end
20
+
21
+ it_behaves_like "with basic date scopes", :updated_at
22
+ end
23
+ end
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'timecop'
3
+ require 'rails/all'
4
+ require 'active_record'
5
+ require 'active_support'
6
+ require 'include_date_scopes'
7
+
8
+ if ActiveSupport::VERSION::MAJOR < 4
9
+ require 'active_support/core_ext/logger'
10
+ end
11
+
12
+ Dir.glob(File.join(File.dirname(__FILE__), '{fixtures,matchers,support}', '*')) do |file|
13
+ require file
14
+ end
15
+
16
+ if RUBY_PLATFORM == "java"
17
+ database_adapter = "jdbcsqlite3"
18
+ else
19
+ database_adapter = "sqlite3"
20
+ end
21
+
22
+ ActiveRecord::Base.default_timezone = :local
23
+ ActiveRecord::Base.logger = Logger.new(STDERR)
24
+ ActiveRecord::Base.logger.level = Logger::WARN
25
+ ActiveRecord::Base.establish_connection(
26
+ :adapter => database_adapter,
27
+ :database => ':memory:'
28
+ )
29
+ ActiveRecord::Base.connection.create_table(:posts, :force => true) do |t|
30
+ t.string :body
31
+ t.timestamp :show_at
32
+ t.timestamp :created_at
33
+ t.timestamp :updated_at
34
+ t.boolean :flag
35
+ end
36
+
37
+ require "#{File.dirname(__FILE__)}/../lib/include_date_scopes/active_record"
38
+
39
+ RSpec.configure do |config|
40
+
41
+ config.mock_with :rspec
42
+
43
+ config.after :each do
44
+ Post.delete_all
45
+ end
46
+ end
47
+
48
+ def define_model_class(name = "Post", parent_class_name = "ActiveRecord::Base", &block)
49
+ Object.send(:remove_const, name) rescue nil
50
+ eval("class #{name} < #{parent_class_name}; end", TOPLEVEL_BINDING)
51
+ klass = eval(name, TOPLEVEL_BINDING)
52
+ klass.class_eval do
53
+ if respond_to?(:table_name=)
54
+ self.table_name = 'posts'
55
+ else
56
+ set_table_name 'posts'
57
+ end
58
+ end
59
+ klass.class_eval(&block) if block_given?
60
+ end
@@ -0,0 +1,128 @@
1
+
2
+ shared_examples "with basic date scopes" do |date_column = :created_at |
3
+ let!(:test_class) { Post }
4
+ describe "date scopes" do
5
+ before(:each) { Timecop.freeze Time.local(2013,02,01,06,30) }
6
+
7
+ let!(:last_week_obj) { test_class.create! date_column.to_sym => 1.week.ago }
8
+ let!(:yesterday_obj) { test_class.create! date_column.to_sym => 24.hours.ago }
9
+ let!(:hour_ago_obj) { test_class.create! date_column.to_sym => 1.hour.ago }
10
+ let!(:five_minute_ago_obj) { test_class.create! date_column.to_sym => 5.minutes.ago }
11
+ let!(:after_midnight_ago_obj) { test_class.create! date_column.to_sym => Date.today.midnight + 5.minutes }
12
+ let!(:before_midnight_ago_obj) { test_class.create! date_column.to_sym => Date.today.midnight - 5.minutes }
13
+
14
+ describe ":between" do
15
+ context "with DateTime" do
16
+ subject {test_class.between(2.hours.ago, 1.minute.ago).all}
17
+
18
+ it { should_not include last_week_obj }
19
+ it { should_not include yesterday_obj }
20
+ it { should include hour_ago_obj }
21
+ it { should include five_minute_ago_obj }
22
+ end
23
+ context "with Date" do
24
+ subject {test_class.between(2.days.ago.to_date, Date.today).all}
25
+
26
+ it { should_not include last_week_obj }
27
+ it { should include yesterday_obj }
28
+ it { should include hour_ago_obj }
29
+ it { should include five_minute_ago_obj }
30
+ end
31
+ end
32
+
33
+ describe ":after" do
34
+ context "with DateTime" do
35
+ subject {test_class.after(2.hours.ago).all}
36
+
37
+ it { should_not include last_week_obj }
38
+ it { should_not include yesterday_obj }
39
+ it { should include hour_ago_obj }
40
+ it { should include five_minute_ago_obj }
41
+ end
42
+ context "with Date" do
43
+ subject {test_class.after(2.days.ago.to_date).all}
44
+
45
+ it { should_not include last_week_obj }
46
+ it { should include yesterday_obj }
47
+ it { should include hour_ago_obj }
48
+ it { should include five_minute_ago_obj }
49
+ end
50
+ end
51
+
52
+ describe ":on_or_after_date" do
53
+ context "with DateTime" do
54
+ subject {test_class.on_or_after_date(2.hours.ago).all}
55
+
56
+ it { should_not include last_week_obj }
57
+ it { should_not include yesterday_obj }
58
+ it { should include hour_ago_obj }
59
+ it { should include five_minute_ago_obj }
60
+ end
61
+ context "with Date" do
62
+ subject {test_class.on_or_after_date(2.days.ago.to_date).all}
63
+
64
+ it { should_not include last_week_obj }
65
+ it { should include yesterday_obj }
66
+ it { should include hour_ago_obj }
67
+ it { should include five_minute_ago_obj }
68
+ end
69
+ end
70
+
71
+ describe ":before" do
72
+ context "with DateTime" do
73
+ subject {test_class.before(2.hours.ago).all}
74
+
75
+ it { should include last_week_obj }
76
+ it { should include yesterday_obj }
77
+ it { should_not include hour_ago_obj }
78
+ it { should_not include five_minute_ago_obj }
79
+ end
80
+ context "with Date" do
81
+ subject {test_class.before(Date.today).all}
82
+
83
+ it { should include last_week_obj }
84
+ it { should include yesterday_obj }
85
+ it { should_not include hour_ago_obj }
86
+ it { should_not include five_minute_ago_obj }
87
+ end
88
+ end
89
+
90
+ describe ":on_or_before_date" do
91
+ context "with DateTime" do
92
+ subject {test_class.on_or_before_date(25.hours.ago).all}
93
+
94
+ it { should include last_week_obj }
95
+ it { should include yesterday_obj }
96
+ it { should_not include hour_ago_obj }
97
+ it { should_not include five_minute_ago_obj }
98
+ end
99
+ context "with Date" do
100
+ subject {test_class.on_or_before_date(Date.yesterday).all}
101
+
102
+ it { should include last_week_obj }
103
+ it { should include yesterday_obj }
104
+ it { should_not include hour_ago_obj }
105
+ it { should_not include five_minute_ago_obj }
106
+ end
107
+ end
108
+
109
+ describe ":today" do
110
+ subject {test_class.today.all}
111
+
112
+ it { should_not include last_week_obj }
113
+ it { should_not include yesterday_obj }
114
+ it { should include hour_ago_obj }
115
+ it { should include five_minute_ago_obj }
116
+ end
117
+
118
+ describe ":yesterday" do
119
+ subject {test_class.yesterday.all}
120
+
121
+ it { should_not include last_week_obj }
122
+ it { should include yesterday_obj }
123
+ it { should_not include hour_ago_obj }
124
+ it { should_not include five_minute_ago_obj }
125
+ end
126
+ end
127
+ end
128
+
@@ -0,0 +1,4 @@
1
+
2
+ class Post < ActiveRecord::Base
3
+
4
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: include_date_scopes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Sean Todd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: squeel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "<"
39
+ - !ruby/object:Gem::Version
40
+ version: '4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: railties
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
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
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: "Add in ActiveRelation scopes that are common across all models. You
98
+ can also \nadd in a prefix to those scopes so you can have multiple per model. See
99
+ README\nfor more info.\n"
100
+ email:
101
+ - iphone.reply@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".ruby-gemset"
108
+ - ".ruby-version"
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - README.md
112
+ - include_date_scopes.gemspec
113
+ - lib/include_date_scopes.rb
114
+ - lib/include_date_scopes/active_record.rb
115
+ - lib/include_date_scopes/date_scopes.rb
116
+ - lib/include_date_scopes/version.rb
117
+ - lib/railtie.rb
118
+ - spec/config/database.yml
119
+ - spec/lib/date_scopes_spec.rb
120
+ - spec/spec_helper.rb
121
+ - spec/support/date_scope_examples.rb
122
+ - spec/support/post.rb
123
+ homepage: http://github.com/descentintomael/include_date_scopes
124
+ licenses: []
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.2.2
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Include scopes for searching on a date column
146
+ test_files:
147
+ - spec/config/database.yml
148
+ - spec/lib/date_scopes_spec.rb
149
+ - spec/spec_helper.rb
150
+ - spec/support/date_scope_examples.rb
151
+ - spec/support/post.rb
152
+ has_rdoc: