less_simple_date_scopes 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +97 -0
- data/Rakefile +7 -0
- data/less_simple_date_scopes.gemspec +22 -0
- data/lib/less_simple_date_scopes.rb +10 -0
- data/lib/less_simple_date_scopes/active_record.rb +79 -0
- data/lib/less_simple_date_scopes/version.rb +3 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ef7aeca487f0d230a6689245bbefd9074e3112e4
|
4
|
+
data.tar.gz: 4dbafb74833370160126f96897640bebeb66c100
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e00cb11c6e69c39bde7ec82fdadde0205636ba70e3bc56f1227564515b94220d7738aa126fbc09badb2cae7200c071be07704b1f01e476cc7a05624ce533a0e
|
7
|
+
data.tar.gz: c036e7a6abe8a59e3230e02159b69be8888f2b40ca67032e0006ffc4d36a05a92aa1ebb5e91a7b482b742b9637a6507844dd0d4bf34b4d355557054e2f1ce6c9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Peter Hoeg
|
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,97 @@
|
|
1
|
+
# LessSimpleDateScopes
|
2
|
+
|
3
|
+
A super tiny gem that provides basic year, month and week scopes.
|
4
|
+
It allows a little more customization than the original [SimpleDateScopes](https://github.com/speartail/date_scopes).
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'less_simple_date_scopes'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
You can add a set of predefined date scopes for a certain database column by
|
19
|
+
calling `simple_date_scopes_on` in the corresponding model:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class Booking < ActiveRecord::Base
|
23
|
+
simple_date_scopes_on :created_at
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
This will generate the following groups of scopes:
|
28
|
+
|
29
|
+
**Relative Date Scopes**
|
30
|
+
|
31
|
+
* last\_year, this\_year, next\_year
|
32
|
+
* last\_month, this\_month, next\_month
|
33
|
+
* last\_week, this\_week, next\_week
|
34
|
+
* yesterday, today, tomorrow
|
35
|
+
|
36
|
+
**Week, Month and Year Scopes**
|
37
|
+
|
38
|
+
* in\_year\_of [date]
|
39
|
+
* in\_month\_of [date]
|
40
|
+
* in\_week\_of [date]
|
41
|
+
|
42
|
+
**Range Scopes**
|
43
|
+
|
44
|
+
* between [start_date] [end_date]
|
45
|
+
|
46
|
+
Start and end date may either be Date (or DateTime) objects or Strings Date.parse() can handle.
|
47
|
+
|
48
|
+
## Examples
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
Booking.created_at_this_month.all.each do |w|
|
52
|
+
puts w.to_s
|
53
|
+
end
|
54
|
+
|
55
|
+
Widget.created_at_last_month.all(:limit => 4).each do |w|
|
56
|
+
puts w.to_s
|
57
|
+
end
|
58
|
+
|
59
|
+
Booking.created_at_in_month_of(Date.new(2012, 2, 7)).all.each do |w|
|
60
|
+
puts w.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
Booking.created_at_in_month_of('2012-03-25').all.each do |w|
|
64
|
+
puts w.to_s
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
## Naming
|
69
|
+
|
70
|
+
All scopes are prefixed with the field they were defined on, in this case `created_at`.
|
71
|
+
If you need another custom prefix, you may specify it by passing in `:prefix` to `simple_date_scopes_on` as
|
72
|
+
second argument:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
simple_date_scopes_on :created_at, :prefix => 'scoped'
|
76
|
+
```
|
77
|
+
|
78
|
+
This would generate scope names in the format `scoped_created_at_...`
|
79
|
+
|
80
|
+
## Custom Tables
|
81
|
+
|
82
|
+
Sometimes it might be useful to generate date scopes for an associated model/table,
|
83
|
+
e.g. for sorting purposes. The table name used by the scope can be passed in with `:table_name`:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
simple_date_scopes_on :created_at, :table_name => 'booking_position', :prefix => 'booking_position'
|
87
|
+
```
|
88
|
+
|
89
|
+
This would generate scopes of the format `booking_position_created_at_...`.
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
1. Fork it
|
94
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
95
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
96
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
97
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'less_simple_date_scopes/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'less_simple_date_scopes'
|
8
|
+
gem.version = LessSimpleDateScopes::VERSION
|
9
|
+
gem.authors = ["Stefan Exner"]
|
10
|
+
gem.email = ["stex@sterex.de"]
|
11
|
+
gem.description = %q{A set of standard date scopes to handle years, months and weeks based on created_at or another field. Based on simple_date_scopes.}
|
12
|
+
gem.summary = %q{A set of standard date scopes to handle years, months and weeks based on created_at or another field.}
|
13
|
+
gem.homepage = 'http://www.github.com/stex/less_simple_date_scopes'
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'activerecord', '~> 2.3'
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module LessSimpleDateScopes
|
2
|
+
module ActiveRecord
|
3
|
+
DEFAULT_FIELD = :created_at
|
4
|
+
|
5
|
+
def self.included(base_class)
|
6
|
+
base_class.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
#
|
12
|
+
# Generates some named scopes in the current model which allow
|
13
|
+
# easy date based access on records for often needed tasks.
|
14
|
+
#
|
15
|
+
# @param [String, Symbol] field
|
16
|
+
# The table column (date or datetime) the scopes will be running on
|
17
|
+
#
|
18
|
+
# @param [Hash] options
|
19
|
+
# Options to customize the generated scopes
|
20
|
+
#
|
21
|
+
# @option options [String] :table_name (ActiveRecord::Base.table_name)
|
22
|
+
# The table name the scopes will be running on. This is useful
|
23
|
+
# if the scope is used to filter based on an association rather than
|
24
|
+
# on the model's own table
|
25
|
+
#
|
26
|
+
# @option options [String] :prefix (nil)
|
27
|
+
# An optional prefix for the generated scope name. Useful if
|
28
|
+
# there are generated scopes for two tables which share the same
|
29
|
+
# column name (e.g. +generated_at+)
|
30
|
+
#
|
31
|
+
def simple_date_scopes_on(field, options = {})
|
32
|
+
table_name = options.delete(:table_name) || self.table_name
|
33
|
+
prefix = options.delete(:prefix)
|
34
|
+
|
35
|
+
named_scope acts_as_date_scope_name(field, :yesterday, prefix), lambda { in_x_of(table_name, Time.zone.now - 1.day, field, :day, options) }
|
36
|
+
named_scope acts_as_date_scope_name(field, :today, prefix), lambda { in_x_of(table_name, Time.zone.now, field, :day, options) }
|
37
|
+
named_scope acts_as_date_scope_name(field, :tomorrow, prefix), lambda { in_x_of(table_name, Time.zone.now + 1.day, field, :day, options) }
|
38
|
+
|
39
|
+
named_scope acts_as_date_scope_name(field, :in_week_of, prefix), lambda {|date| in_x_of(table_name, date, field, :week, options) }
|
40
|
+
named_scope acts_as_date_scope_name(field, :last_week, prefix), lambda { in_x_of(table_name, Time.zone.now - 7.days, field, :week, options) }
|
41
|
+
named_scope acts_as_date_scope_name(field, :this_week, prefix), lambda { in_x_of(table_name, Time.zone.now, field, :week, options) }
|
42
|
+
named_scope acts_as_date_scope_name(field, :next_week, prefix), lambda { in_x_of(table_name, Time.zone.now + 7.days, field, :week, options) }
|
43
|
+
|
44
|
+
named_scope acts_as_date_scope_name(field, :in_month_of, prefix), lambda {|date| in_x_of(table_name, date, field, :month, options) }
|
45
|
+
named_scope acts_as_date_scope_name(field, :last_month, prefix), lambda { in_x_of(table_name, Time.zone.now - 1.month, field, :month, options) }
|
46
|
+
named_scope acts_as_date_scope_name(field, :this_month, prefix), lambda { in_x_of(table_name, Time.zone.now, field, :month, options) }
|
47
|
+
named_scope acts_as_date_scope_name(field, :next_month, prefix), lambda { in_x_of(table_name, Time.zone.now + 1.month, field, :month, options) }
|
48
|
+
|
49
|
+
named_scope acts_as_date_scope_name(field, :in_year_of, prefix), lambda {|date| in_x_of(table_name, date, field, :year, options) }
|
50
|
+
named_scope acts_as_date_scope_name(field, :last_year, prefix), lambda { in_x_of(table_name, Time.zone.now - 1.year, field, :year, options) }
|
51
|
+
named_scope acts_as_date_scope_name(field, :this_year, prefix), lambda { in_x_of(table_name, Time.zone.now, field, :year, options) }
|
52
|
+
named_scope acts_as_date_scope_name(field, :next_year, prefix), lambda { in_x_of(table_name, Time.zone.now + 1.year, field, :year, options) }
|
53
|
+
|
54
|
+
named_scope acts_as_date_scope_name(field, :between, prefix),
|
55
|
+
lambda {|s, e|
|
56
|
+
start_date = (s.is_a?(String) ? Time.zone.parse(s) : s).beginning_of_day
|
57
|
+
end_date = (e.is_a?(String) ? Time.zone.parse(e) : e).end_of_day
|
58
|
+
options.merge({:conditions => {table_name => {field => (start_date..end_date)}}})
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
def simple_date_scopes
|
63
|
+
simple_date_scopes_on DEFAULT_FIELD
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def in_x_of(table_name, date, field, method, options)
|
69
|
+
start_date = date.send("beginning_of_#{method.to_s}").beginning_of_day
|
70
|
+
end_date = date.send("end_of_#{method.to_s}").end_of_day
|
71
|
+
options.merge({:conditions => {table_name => {field => (start_date..end_date)}}})
|
72
|
+
end
|
73
|
+
|
74
|
+
def acts_as_date_scope_name(field, scope_name, prefix)
|
75
|
+
[prefix, field, scope_name].compact.join('_')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: less_simple_date_scopes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stefan Exner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-30 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: '2.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.3'
|
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
|
+
description: A set of standard date scopes to handle years, months and weeks based
|
42
|
+
on created_at or another field. Based on simple_date_scopes.
|
43
|
+
email:
|
44
|
+
- stex@sterex.de
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- less_simple_date_scopes.gemspec
|
55
|
+
- lib/less_simple_date_scopes.rb
|
56
|
+
- lib/less_simple_date_scopes/active_record.rb
|
57
|
+
- lib/less_simple_date_scopes/version.rb
|
58
|
+
homepage: http://www.github.com/stex/less_simple_date_scopes
|
59
|
+
licenses: []
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.2.2
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: A set of standard date scopes to handle years, months and weeks based on
|
81
|
+
created_at or another field.
|
82
|
+
test_files: []
|