date_range_scopes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +15 -0
  3. data/Rakefile +34 -0
  4. data/lib/date_range_scopes.rb +65 -0
  5. metadata +74 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Nick Ragaz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ DateRangeScopes
2
+ ===============
3
+
4
+ Automatically add scopes for day, week, month and year ranges to Active Record models. See [date_range_scopes.rb](https://github.com/nragaz/date_range_scopes/blob/master/lib/date_range_scopes.rb) for a full example.
5
+
6
+ Usage
7
+ -----
8
+
9
+ class User < ActiveRecord::Base
10
+ include DateRangeScopes
11
+ end
12
+
13
+ # => User.created_on
14
+ # => User.created_in_week(5.weeks.ago)
15
+ # => User.created_in_year
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'DateRangeScopes'
18
+ rdoc.options << '--line-numbers' << '--inline-source'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task :default => :test
@@ -0,0 +1,65 @@
1
+ module DateRangeScopes
2
+ extend ActiveSupport::Concern
3
+
4
+ # Include this module in an Active Record class to add the following scopes
5
+ # for every column ending in _at or _on (e.g. updated_at):
6
+ #
7
+ # > #{updated}_on
8
+ # > #{updated}_in_week
9
+ # > #{updated}_in_month
10
+ # > #{updated}_in_year
11
+ #
12
+ # These scopes accept a date or time to define the period. e.g. if you pass a
13
+ # date on a Wednesday, `#{updated}_in_week` will return records with dates
14
+ # between the beginning and end of that week.
15
+ #
16
+ # The default is always today / now.
17
+ #
18
+ # Convenience aliases:
19
+ # > #{updated}_today -> same as #{updated}_on(Date.today)
20
+ # > today(date) -> same as created_on(date)
21
+ included do
22
+ date_columns = column_names.select { |col| col =~ /((_at)|(_on))\z/ }
23
+
24
+ date_columns.each do |col|
25
+ key = col.slice(0..-4)
26
+ full_name = "#{table_name}.#{col}"
27
+
28
+ scope "#{key}_on", lambda { |time=nil|
29
+ time ||= Time.zone.now
30
+ time = time.respond_to?(:to_time_in_current_zone) ?
31
+ time.to_time_in_current_zone : time
32
+
33
+ where(
34
+ "#{full_name} >= ? AND #{full_name} <= ?",
35
+ time.beginning_of_day,
36
+ time.end_of_day
37
+ )
38
+ }
39
+
40
+ scope "#{key}_today", send("#{key}_on")
41
+
42
+ %w( week month year ).each do |period|
43
+ scope "#{key}_in_#{period}", lambda { |time=nil|
44
+ time ||= Time.zone.now
45
+ time = time.respond_to?(:to_time_in_current_zone) ?
46
+ time.to_time_in_current_zone : time
47
+
48
+ where(
49
+ "#{full_name} >= ? AND #{full_name} <= ?",
50
+ time.send("beginning_of_#{period}"),
51
+ time.send("end_of_#{period}")
52
+ )
53
+ }
54
+
55
+ scope "#{key}_this_#{period}", send("#{key}_in_#{period}")
56
+ end
57
+ end
58
+ end
59
+
60
+ module ClassMethods
61
+ def today(date=nil)
62
+ created_on(date)
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: date_range_scopes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nick Ragaz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-29 00:00:00.000000000 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: &2157626560 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2157626560
26
+ - !ruby/object:Gem::Dependency
27
+ name: sqlite3
28
+ requirement: &2157626160 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2157626160
37
+ description: Automatically add scopes for day, week, month and year ranges to Active
38
+ Record models.
39
+ email: nick.ragaz@gmail.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - lib/date_range_scopes.rb
45
+ - MIT-LICENSE
46
+ - Rakefile
47
+ - README.md
48
+ has_rdoc: true
49
+ homepage: http://github.com/nragaz/date_range_scopes
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.6.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Automatically add scopes for day, week, month and year ranges to Active Record
73
+ models.
74
+ test_files: []