calendar_sniper 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in DateRange.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris MacNaughton
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,37 @@
1
+ # CalendarSniper
2
+
3
+ CalendarSniper adds date related scopes to ActiveRecord models
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'CaendarSniper'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install CalendarSniper
18
+
19
+ ## Usage
20
+
21
+ CalendarSniper adds ```with_date_range```, ```with_to_date```, ```with_from_date```, and ```in_date_range``` scopes to ActiveRecord models that it is included into.
22
+
23
+ ```with_date_range``` takes a number that is the number of days
24
+ ```in_date_range``` takes a start and end date
25
+ ```with_to_date``` and ```with_from_date``` each take a single date as an argument
26
+
27
+ These scopes integrate with ActiveRecord's created_at or you can select a custom column to filter on by calling ```filterable_by_date_range``` with the name of the column you want to be the date column.
28
+
29
+ These scopes take into considering time in their scopes allowing you to search from the beginning of one day until the end of another.
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'calendar_sniper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "calendar_sniper"
8
+ spec.version = CalendarSniper::VERSION
9
+ spec.authors = ["Chris MacNaughton", "Rob DiMarco"]
10
+ spec.email = ["chris@elocal.com", "rob@elocal.com"]
11
+ spec.description = %q{Gem that adds date related scopes to ActiveRecord models}
12
+ spec.summary = %q{CalendarSniper adds with_date_range, with_to_date, with_from_date, and
13
+ in_date_range scopes to ActiveRecord models that it is included into.}
14
+ spec.homepage = "https://github.com/eLocal/CalendarSniper"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,74 @@
1
+ require "calendar_sniper/version"
2
+
3
+ module CalendarSniper
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ scope :with_date_range, lambda { |num_days| with_from_date(num_days.to_f.days.ago) }
8
+ scope :with_to_date, lambda { |date| search_in_date_range :<, date }
9
+ scope :with_from_date, lambda { |date| search_in_date_range :>, date }
10
+ scope :in_date_range, lambda { |from,to| with_from_date(from).with_to_date(to) }
11
+ end
12
+
13
+ module ClassMethods
14
+ attr_reader :_filterable_by_date_range_field
15
+
16
+ # Set the field to search date range on in your model.
17
+ #
18
+ # Example:
19
+ #
20
+ # filterable_by_date_range :internal_lead_received_at
21
+ #
22
+ def filterable_by_date_range with_name=:created_at
23
+ @_filterable_by_date_range_field = with_name.to_s
24
+ end
25
+
26
+ # Search through the date range in a given direction. Uses the
27
+ # field given in the +filterable_by_date_range+ macro that you
28
+ # can add to ActiveRecord, otherwise it defaults to 'created_at'.
29
+ def search_in_date_range by_direction, with_date
30
+ logger.debug "Searched #{date_field} on #{self.class.name} with a scope"
31
+ where("#{table_name}.#{date_field} #{by_direction}= ?", coalesce_date(with_date, by_direction))
32
+ end
33
+
34
+ private
35
+
36
+ def coalesce_date(from_date_or_string, by_direction)
37
+ if from_date_or_string.is_a?(String)
38
+ date_from_string = DateTime.strptime(from_date_or_string, date_format_for_string(from_date_or_string))
39
+ set_time_for_direction(date_from_string, by_direction)
40
+ else
41
+ set_time_for_direction(from_date_or_string, by_direction)
42
+ end
43
+ end
44
+
45
+ def set_time_for_direction(date_from_string, by_direction)
46
+ case by_direction.to_sym
47
+ when :<
48
+ date_from_string.end_of_day
49
+ when :>
50
+ date_from_string.beginning_of_day
51
+ else
52
+ date_from_string
53
+ end
54
+ end
55
+
56
+ def date_format_for_string(str)
57
+ if /^\d{1,2}\/\d{1,2}\/\d{4}/ =~ str
58
+ '%m/%d/%Y'
59
+ elsif /^\d{4}-\d{1,2}-\d{1,2}/ =~ str
60
+ if /^\d{4}-\d{1,2}-\d{1,2} \S+/ =~ str
61
+ '%Y-%m-%d %Z'
62
+ else
63
+ '%Y-%m-%d'
64
+ end
65
+ else
66
+ raise "Date string in unknown format: #{str}"
67
+ end
68
+ end
69
+
70
+ def date_field
71
+ @date_field ||= @_filterable_by_date_range_field || 'created_at'
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module CalendarSniper
2
+ VERSION = "1.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: calendar_sniper
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris MacNaughton
9
+ - Rob DiMarco
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-10-01 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.3'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Gem that adds date related scopes to ActiveRecord models
48
+ email:
49
+ - chris@elocal.com
50
+ - rob@elocal.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - calendar_sniper.gemspec
61
+ - lib/calendar_sniper.rb
62
+ - lib/calendar_sniper/version.rb
63
+ homepage: https://github.com/eLocal/CalendarSniper
64
+ licenses:
65
+ - MIT
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.25
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: CalendarSniper adds with_date_range, with_to_date, with_from_date, and in_date_range
88
+ scopes to ActiveRecord models that it is included into.
89
+ test_files: []
90
+ has_rdoc: