date_params 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +7 -0
- data/date_params.gemspec +26 -0
- data/lib/date_params.rb +4 -0
- data/lib/date_params/controller_additions.rb +61 -0
- data/lib/date_params/version.rb +3 -0
- data/spec/date_picker/controller_additions_spec.rb +58 -0
- data/spec/spec_helper.rb +7 -0
- metadata +125 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jason Rust
|
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,67 @@
|
|
1
|
+
# DateParams
|
2
|
+
|
3
|
+
Dates passed in by date-pickers or text-input fields need to be
|
4
|
+
converted from their string format to a ruby Date to be able to be saved
|
5
|
+
and manipulated. This gem provides a simple controller add-on to
|
6
|
+
facilitate the conversion.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'date_params'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install date_params
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Include the controller additions in the controller that needs to parse
|
25
|
+
date parameters and then specify dates to be formatted:
|
26
|
+
```ruby
|
27
|
+
class UsersController < ApplicationController
|
28
|
+
# e.g. parameters come in as: { sign_up_on: '01/05/2013' }
|
29
|
+
include DateParams::ControllerAdditions
|
30
|
+
date_params :sign_up_on
|
31
|
+
# and now params[:sign_up_on] is a Date object
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Any options that a `before_filter` accepts can be passed in:
|
36
|
+
```ruby
|
37
|
+
include DateParams::ControllerAdditions
|
38
|
+
date_params :sign_up_on, only: [:index]
|
39
|
+
```
|
40
|
+
|
41
|
+
If date fields are namespaced in a model that can be specified with the
|
42
|
+
`namespace` option:
|
43
|
+
```ruby
|
44
|
+
# will parse parameters in the format of: { user: { searched_on: '01/04/2013', sign_up_on: '04/03/2013' } }
|
45
|
+
include DateParams::ControllerAdditions
|
46
|
+
date_params :searched_on, :sign_up_on, namespace: :user
|
47
|
+
```
|
48
|
+
|
49
|
+
Or specify a namespace for each parameter individually:
|
50
|
+
```ruby
|
51
|
+
include DateParams::ControllerAdditions
|
52
|
+
date_params [:user, :searched_on], [:company, :sign_up_on]
|
53
|
+
```
|
54
|
+
|
55
|
+
Date format can be passed as an option (default is `%m/%d/%Y`):
|
56
|
+
```ruby
|
57
|
+
include DateParams::ControllerAdditions
|
58
|
+
date_params :search_on, :sign_up_on, date_format: '%d-%m-%Y'
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/date_params.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'date_params/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'date_params'
|
8
|
+
spec.version = DateParams::VERSION
|
9
|
+
spec.authors = ['Jason Rust']
|
10
|
+
spec.email = ['jason@lessonplanet.com']
|
11
|
+
spec.description = %q{Dates passed in by date-pickers or text-input fields to a rails controller need to be converted to a ruby Date to be able to be saved and manipulated. This gem provides a simple controller add-on to facilitate the conversion.}
|
12
|
+
spec.summary = %q{Convert date string parameters in a rails controller into Date objects.}
|
13
|
+
spec.homepage = 'https://github.com/LessonPlanet/date_params'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'activesupport'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'rspec'
|
26
|
+
end
|
data/lib/date_params.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'active_support/core_ext/object/try'
|
4
|
+
require 'active_support/core_ext/object/blank'
|
5
|
+
|
6
|
+
module DateParams
|
7
|
+
module ControllerAdditions
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
# Usage:
|
12
|
+
#
|
13
|
+
# To convert only on specific actions use:
|
14
|
+
# date_params :param_name, [:namespaced, :param_name], only: :action_name
|
15
|
+
# If all variables are in a common namespace, use the namespace option:
|
16
|
+
# date_params :param_name1, :param_name2, namespace: :namespaced
|
17
|
+
# @see #parse_date_param!
|
18
|
+
def date_params(*args)
|
19
|
+
options = args.extract_options!
|
20
|
+
before_filter options do |controller|
|
21
|
+
controller.parse_date_params(args, options)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Usage:
|
27
|
+
#
|
28
|
+
# To convert params[:my_date]
|
29
|
+
# date_params :my_date
|
30
|
+
#
|
31
|
+
# To convert params[:my_model][:my_date1] use one of the following:
|
32
|
+
# parse_date_param!([:my_model, :my_date1])
|
33
|
+
# parse_date_param!(:my_date1, namespace: :my_model)
|
34
|
+
#
|
35
|
+
# if the format of the data is not '%m/%d/%Y'
|
36
|
+
# parse_date_param!(:my_date1, date_format: '%m/%Y')
|
37
|
+
def parse_date_param!(param, options)
|
38
|
+
param_path = Array(param).dup
|
39
|
+
param_path.unshift options[:namespace] if options[:namespace] && param_path.length == 1
|
40
|
+
date_format = options.fetch(:date_format, '%m/%d/%Y')
|
41
|
+
traversed_params = params
|
42
|
+
|
43
|
+
if param_path.length > 1
|
44
|
+
param_key = param_path.pop
|
45
|
+
param_path.each { |k| traversed_params = traversed_params.try :[], k }
|
46
|
+
else
|
47
|
+
param_key = param_path.first
|
48
|
+
end
|
49
|
+
|
50
|
+
value = traversed_params.try(:[], param_key)
|
51
|
+
return nil if value.blank?
|
52
|
+
date_format = value =~ /\d{4}-\d{2}-\d{2}/ ? '%Y-%m-%d' : date_format
|
53
|
+
date = Date.strptime(value, date_format)
|
54
|
+
traversed_params[param_key] = date if date
|
55
|
+
end
|
56
|
+
|
57
|
+
def parse_date_params(the_params, options)
|
58
|
+
the_params.each{ |param| parse_date_param! param, options }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DateParams::ControllerAdditions do
|
4
|
+
let(:mock_controller) do
|
5
|
+
Class.new do
|
6
|
+
attr_accessor :params
|
7
|
+
include DateParams::ControllerAdditions
|
8
|
+
end.new
|
9
|
+
end
|
10
|
+
let(:custom_params) do
|
11
|
+
{
|
12
|
+
date: '08/10/2012',
|
13
|
+
paginated_date: '2012-08-10',
|
14
|
+
invalid_date: '000000',
|
15
|
+
model: {
|
16
|
+
date: '08/10/2012'
|
17
|
+
}
|
18
|
+
}
|
19
|
+
end
|
20
|
+
let(:date) { Date.parse '2012-08-10' }
|
21
|
+
|
22
|
+
context 'invalid format' do
|
23
|
+
let(:param_spec) { :invalid_date }
|
24
|
+
it 'should raise when unknown format' do
|
25
|
+
mock_controller.params = custom_params
|
26
|
+
expect { mock_controller.parse_date_param!(param_spec, {}) }.to raise_error ArgumentError, 'invalid date'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'valid format' do
|
31
|
+
before do
|
32
|
+
mock_controller.params = custom_params
|
33
|
+
mock_controller.parse_date_param!(param_spec, {})
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'should update params' do
|
37
|
+
let(:param_spec) { :date }
|
38
|
+
subject { mock_controller.params[:date] }
|
39
|
+
it { should eq date }
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'should update nested params' do
|
43
|
+
let(:param_spec) { [:model, :date] }
|
44
|
+
subject { mock_controller.params[:model][:date] }
|
45
|
+
it { should eq date }
|
46
|
+
|
47
|
+
it 'should not change the original' do
|
48
|
+
param_spec.should == [:model, :date]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when date format is yyyy-mm-dd from pagination' do
|
53
|
+
let(:param_spec) { :paginated_date }
|
54
|
+
subject { mock_controller.params[:paginated_date] }
|
55
|
+
it { should eq date }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jason Rust
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Dates passed in by date-pickers or text-input fields to a rails controller
|
79
|
+
need to be converted to a ruby Date to be able to be saved and manipulated. This
|
80
|
+
gem provides a simple controller add-on to facilitate the conversion.
|
81
|
+
email:
|
82
|
+
- jason@lessonplanet.com
|
83
|
+
executables: []
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files: []
|
86
|
+
files:
|
87
|
+
- .gitignore
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- date_params.gemspec
|
93
|
+
- lib/date_params.rb
|
94
|
+
- lib/date_params/controller_additions.rb
|
95
|
+
- lib/date_params/version.rb
|
96
|
+
- spec/date_picker/controller_additions_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
homepage: https://github.com/LessonPlanet/date_params
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 1.8.23
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: Convert date string parameters in a rails controller into Date objects.
|
123
|
+
test_files:
|
124
|
+
- spec/date_picker/controller_additions_spec.rb
|
125
|
+
- spec/spec_helper.rb
|