punctual_date_select 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/punctual_date_select/helper.rb +24 -0
- data/lib/punctual_date_select/model.rb +73 -0
- data/lib/punctual_date_select/version.rb +3 -0
- data/lib/punctual_date_select.rb +7 -0
- data/punctual_date_select.gemspec +27 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b765c2778fc3c29414085d70e820aca5cb9b52d7
|
4
|
+
data.tar.gz: da5143798dbac1fea8539d3aa890e3e7e7b23750
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 611e03070b23e79a7e905d3c4d3e5d2a813a233dfb0fe265564d36604da8539a5804cd57fa14498758b4808332474360d47d57b4a461369a2b6d741dad039e52
|
7
|
+
data.tar.gz: cc63473242cb5ee84b7ba701288cb0a6bb955111d4306d2ff28a97ad71d6c9e65d7d16e27e18db0ceda00c5b2e5682d98abb5ca0a179dceba547b8d1dde839b1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 nay3
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# PunctualDateSelect
|
2
|
+
|
3
|
+
This gem provides yet another date_select-like fields which can hold invalid date data.
|
4
|
+
|
5
|
+
When using Rails's date_select, an invalid date like 2016/9/31 would be casted into 2016/10/1 with no warning.
|
6
|
+
This may cause a human error in some use cases.
|
7
|
+
|
8
|
+
With PunctualDateSelect, you can hold invalid date as they are specified by an user and show the invalid error message.
|
9
|
+
Therefore users can notice the mistakes they've made.
|
10
|
+
|
11
|
+
This gem is for Rails 4 currently.
|
12
|
+
|
13
|
+
## Key concept
|
14
|
+
|
15
|
+
Using PunctualDateSelect, your date column value won't have Date object while it is non-existing date.
|
16
|
+
It holds parameters like {year: '2016', month: '9', day: '31'} instead.
|
17
|
+
This gem extends that parameters to respond year, month, day, to_date, to_s so that inner select_date field use it like Date object.
|
18
|
+
You can use to_date or to_s in your code to get the valid date information.
|
19
|
+
If the specified date from punctual_date_select fields is collect, it will be casted into Date object before validation.
|
20
|
+
|
21
|
+
Note that you need to run validation before you save it not to send those parameters holding invalid date data to DB.
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'punctual_date_select'
|
29
|
+
```
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle
|
34
|
+
|
35
|
+
Or install it yourself as:
|
36
|
+
|
37
|
+
$ gem install punctual_date_select
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
You need to write code in 2 places; Model and View.
|
42
|
+
|
43
|
+
### Model (ActiveRecord)
|
44
|
+
|
45
|
+
It your model class which has the target date column, call punctual_date_select method.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
punctual_date_column :started_on # You have started_on column which represents a date.
|
49
|
+
```
|
50
|
+
|
51
|
+
### View
|
52
|
+
|
53
|
+
On the view side, you can use punctual_date_select instead of date_select. This helper holds invalid date values so that the user can see what is wrong.
|
54
|
+
This plugin does not support non-builder type helper.
|
55
|
+
|
56
|
+
```
|
57
|
+
<% form_for :your_model do |f| %>
|
58
|
+
<%= f.punctual_date_select :started_on %>
|
59
|
+
<% end %>
|
60
|
+
```
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
65
|
+
|
66
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nay/punctual_date_select.
|
71
|
+
|
72
|
+
## License
|
73
|
+
|
74
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
75
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "punctual_date_select"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module PunctualDateSelect::Helper
|
2
|
+
|
3
|
+
module Builder
|
4
|
+
def punctual_date_select(method, options={}, html_options={})
|
5
|
+
value = object.send(method)
|
6
|
+
value ||= Date.current if options[:prompt].nil?
|
7
|
+
@template.select_date(value, {:prefix => "#{@object_name}[#{method}]"}.merge(options), html_options)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module DateHelper
|
12
|
+
def select_year
|
13
|
+
old_datetime = @datetime
|
14
|
+
@datetime = nil if @datetime.respond_to?(:year) && !@datetime.year
|
15
|
+
ret = super
|
16
|
+
@datetime = old_datetime
|
17
|
+
ret
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
ActionView::Helpers::FormBuilder.send(:include, PunctualDateSelect::Helper::Builder)
|
24
|
+
ActionView::Helpers::DateHelper.send(:prepend, PunctualDateSelect::Helper::DateHelper)
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module PunctualDateSelect
|
2
|
+
module DateHash
|
3
|
+
def year
|
4
|
+
get_integer_of :year
|
5
|
+
end
|
6
|
+
def month
|
7
|
+
get_integer_of :month
|
8
|
+
end
|
9
|
+
def day
|
10
|
+
get_integer_of :day
|
11
|
+
end
|
12
|
+
def to_date
|
13
|
+
if year.nil? || month.nil? || day.nil?
|
14
|
+
nil
|
15
|
+
else
|
16
|
+
begin
|
17
|
+
Date.new(year, month, day)
|
18
|
+
rescue
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
def to_s(*args)
|
24
|
+
date = to_date
|
25
|
+
date ? date.to_s(*args) : ""
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def get_integer_of(key)
|
30
|
+
self[key].blank? ? nil : self[key].to_i # This plugin also extends select_year that if this method returns nil it goes well.
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module Model
|
35
|
+
module ClassMethods
|
36
|
+
def punctual_date_column(*args)
|
37
|
+
args.each do |column_name|
|
38
|
+
cast_method = :"cast_#{column_name}_if_possible"
|
39
|
+
before_validation cast_method
|
40
|
+
|
41
|
+
define_method cast_method do
|
42
|
+
casted_date = send(column_name).try(:to_date)
|
43
|
+
send("#{column_name}=", casted_date) if casted_date
|
44
|
+
end
|
45
|
+
|
46
|
+
validation_method = :"validate_#{column_name}_is_casted"
|
47
|
+
validate validation_method
|
48
|
+
|
49
|
+
define_method validation_method do
|
50
|
+
errors.add(column_name, :invalid) if send(column_name) && !send(column_name).kind_of?(Date) && !send(column_name).kind_of?(Time)
|
51
|
+
end
|
52
|
+
|
53
|
+
define_method "#{column_name}=" do |value|
|
54
|
+
self[column_name] = (value.kind_of?(Hash) && !value.values.any?{|t| !t.blank?}) ? nil : value
|
55
|
+
if value.kind_of?(Hash) && !value.kind_of?(PunctualDateSelect::DateHash)
|
56
|
+
class << value
|
57
|
+
include PunctualDateSelect::DateHash
|
58
|
+
end
|
59
|
+
end
|
60
|
+
self[column_name]
|
61
|
+
end
|
62
|
+
|
63
|
+
private cast_method, validation_method
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.included(base)
|
69
|
+
base.extend(ClassMethods)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
ActiveRecord::Base.send(:include, PunctualDateSelect::Model)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'punctual_date_select/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "punctual_date_select"
|
8
|
+
spec.version = PunctualDateSelect::VERSION
|
9
|
+
spec.authors = ["nay3"]
|
10
|
+
spec.email = ["y.ohba@everyleaf.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{It provides similar feature like date_select but it won't cast invalid dates.}
|
13
|
+
spec.description = %q{It provides similar feature like date_select but it won't cast invalid dates.}
|
14
|
+
spec.homepage = "https://github.com/nay/punctual_date_select"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: punctual_date_select
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nay3
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: It provides similar feature like date_select but it won't cast invalid
|
56
|
+
dates.
|
57
|
+
email:
|
58
|
+
- y.ohba@everyleaf.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/punctual_date_select.rb
|
73
|
+
- lib/punctual_date_select/helper.rb
|
74
|
+
- lib/punctual_date_select/model.rb
|
75
|
+
- lib/punctual_date_select/version.rb
|
76
|
+
- punctual_date_select.gemspec
|
77
|
+
homepage: https://github.com/nay/punctual_date_select
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: It provides similar feature like date_select but it won't cast invalid dates.
|
101
|
+
test_files: []
|