multi_part_date 0.0.5

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4c1b4bed3a861dbaf61a413584605964d2b5e0d6
4
+ data.tar.gz: 77d5db1b4a68f0a1ccbc290c51fe9b258d3ca5da
5
+ SHA512:
6
+ metadata.gz: c9202315e47052a980cd419c115026a69c211e83b9a75f2806c98d4130d8e17104e76d0f255244cfe3e8252bea8769a15e7dfdc55703db19ee57e6415767905d
7
+ data.tar.gz: 70c192a8642c70ec8100d82e4a8d173744b929fa96eb5184b2432efa1f3624cf4b7a983d547f581c03d0a58c431485c4e47129a4f8a8e4e7402164adc90965d2
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multi_part_date.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Petr Gazarov
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.
@@ -0,0 +1,95 @@
1
+ # MultiPartDate
2
+ [![Build Status](https://semaphoreci.com/api/v1/policygenius/multi_part_date/branches/master/badge.svg)](https://semaphoreci.com/policygenius/multi_part_date)
3
+ [![Gem Version](https://badge.fury.io/rb/multi_part_date.svg)](https://badge.fury.io/rb/multi_part_date)
4
+
5
+ Easy implementation of multiple date part fields on the form object level.
6
+
7
+ Conceptually it works very similar to Rails date_select helper, e.g. breaks up date month, day and year into separate inputs on the front-end, while parsing to one model attribute on the back-end.
8
+
9
+ The difference is 1) It allows you to style date part inputs however you want and 2) This is an extension to [reform gem](https://github.com/apotonick/reform) and works on form object level.
10
+
11
+ ## Installation
12
+
13
+ ```ruby
14
+ gem 'multi_part_date'
15
+ ```
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install multi_part_date
20
+
21
+ ## Usage
22
+
23
+ Form object:
24
+
25
+ ```ruby
26
+ class BasicForm < Reform::Form
27
+ multi_part_date :date_of_birth
28
+ end
29
+ ```
30
+
31
+ Where `date_of_birth` is an actual model attribute.
32
+
33
+ ### Available options
34
+
35
+ ##### Generate inputs with a different name base:
36
+
37
+ ```
38
+ as: :birth
39
+ ```
40
+
41
+ Will generate inputs
42
+
43
+ ```
44
+ f.birth_day
45
+ f.birth_month
46
+ f.birth_year
47
+ ```
48
+
49
+
50
+
51
+ ##### To omit parts, set the appropriate option below to `true`:
52
+
53
+ ```
54
+ :discard_day
55
+ :discard_month
56
+ :discard_year
57
+ ```
58
+
59
+ ##### Validate conditionally:
60
+
61
+ ```
62
+ validate_if: :some_method
63
+ ```
64
+
65
+ The above method needs to be defined in the form object.
66
+ Note that date is validated by default.
67
+ This gem uses `Date.valid_date?` to determine if the date is valid.
68
+
69
+ ##### These options are delegated to reform's `property` method:
70
+
71
+ ```
72
+ :on
73
+ :type
74
+ ```
75
+
76
+ ### View example
77
+ (using simple_form here & written in slim)
78
+
79
+ ```slim
80
+ simple_form_for @basic_form do |f|
81
+
82
+ = f.input :birth_month
83
+
84
+ = f.input :birth_day
85
+
86
+ = f.input :birth_year
87
+ ```
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome! :smile: Fork the repo and submit your PR or issue.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "multi_part_date"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,129 @@
1
+ require 'multi_part_date/version'
2
+ require 'active_support/concern'
3
+ require 'active_support'
4
+ require 'date'
5
+ require 'reform'
6
+ require 'representable/coercion'
7
+
8
+ Reform::Form.reform_2_0!
9
+
10
+ module MultiPartDate
11
+ extend ActiveSupport::Concern
12
+
13
+ class_methods do
14
+ def multi_part_date(field_name, options = {})
15
+ key = options[:as] || field_name
16
+ discard_options = options.slice(:discard_day, :discard_month, :discard_year)
17
+ on_options = options.slice(:on)
18
+ type = options[:type] || Date
19
+
20
+ create_methods_for(field_name, key, discard_options)
21
+
22
+ property :"#{key}_month", { type: Integer, virtual: true }.merge(on_options)
23
+ property :"#{key}_day", { type: Integer, virtual: true }.merge(on_options)
24
+ property :"#{key}_year", { type: Integer, virtual: true }.merge(on_options)
25
+ property field_name, { type: type }.merge(on_options)
26
+
27
+ validate_if_required(field_name, options[:validate_if])
28
+ end
29
+
30
+ def create_methods_for(field_name, key, discard_options)
31
+ create_date_values_methods_for(key)
32
+ create_getters_for(field_name, key, discard_options)
33
+ create_setters_for(field_name, key)
34
+ create_validation_helper_methods_for(field_name, key)
35
+ end
36
+
37
+ def create_getters_for(field_name, key, discard_options)
38
+ %i(day month year).each do |type|
39
+ if discard_options[:"discard_#{type}"]
40
+ define_method(:"#{key}_#{type}") do
41
+ 1
42
+ end
43
+ else
44
+ define_method(:"#{key}_#{type}") do
45
+ if send(field_name)
46
+ send(field_name).send(type) if send(field_name).respond_to?(type)
47
+ else
48
+ super()
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def create_setters_for(field_name, key)
56
+ %i(day month year).each do |type|
57
+ define_method(:"#{key}_#{type}=") do |value|
58
+ super(value)
59
+
60
+ send(:"set_#{field_name}") if send(:"#{field_name}_parts_present?")
61
+ end
62
+ end
63
+
64
+ define_method(:"set_#{field_name}") do
65
+ return nil unless send(:"valid_#{field_name}_date?")
66
+
67
+ date = Date.new(
68
+ send(:"#{key}_year_value"),
69
+ send(:"#{key}_month_value"),
70
+ send(:"#{key}_day_value")
71
+ )
72
+
73
+ send(:"#{field_name}=", date)
74
+ end
75
+ end
76
+
77
+ def validate_if_required(field_name, validate_if_option)
78
+ if validate_if_option
79
+ validate :"validate_#{field_name}_date", if: validate_if_option
80
+ else
81
+ validate :"validate_#{field_name}_date"
82
+ end
83
+ end
84
+
85
+ def create_date_values_methods_for(key)
86
+ %i(day month year).each do |type|
87
+ define_method(:"#{key}_#{type}_value") do
88
+ send(:"#{key}_#{type}").to_i
89
+ end
90
+ end
91
+ end
92
+
93
+ def create_validation_helper_methods_for(field_name, key)
94
+ create_valid_date_method_for(field_name, key)
95
+ create_parts_present_method_for(field_name, key)
96
+ create_validate_date_method_for(field_name)
97
+ end
98
+
99
+ def create_valid_date_method_for(field_name, key)
100
+ define_method(:"valid_#{field_name}_date?") do
101
+ Date.valid_date?(
102
+ send(:"#{key}_year_value"),
103
+ send(:"#{key}_month_value"),
104
+ send(:"#{key}_day_value")
105
+ )
106
+ end
107
+ end
108
+
109
+ def create_parts_present_method_for(field_name, key)
110
+ define_method(:"#{field_name}_parts_present?") do
111
+ send(:"#{field_name}=", nil)
112
+
113
+ [
114
+ send(:"#{key}_year_value"),
115
+ send(:"#{key}_month_value"),
116
+ send(:"#{key}_day_value")
117
+ ].all?(&:present?)
118
+ end
119
+ end
120
+
121
+ def create_validate_date_method_for(field_name)
122
+ define_method(:"validate_#{field_name}_date") do
123
+ return true if send(:"valid_#{field_name}_date?")
124
+
125
+ errors.add(field_name, 'is not a valid date')
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,3 @@
1
+ module MultiPartDate
2
+ VERSION = '0.0.5'
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multi_part_date/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multi_part_date"
8
+ spec.version = MultiPartDate::VERSION
9
+ spec.authors = ["Petr Gazarov"]
10
+ spec.email = ["petrgazarov@gmail.com"]
11
+
12
+ spec.summary = %q{Splitting date field into multiple inputs on the form object level (reform)}
13
+ spec.homepage = "https://github.com/policygenius/multi_part_date"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "reform", "~> 1.2.6"
22
+ spec.add_runtime_dependency "virtus", "~> 1.0.1"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "pry"
28
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_part_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Petr Gazarov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: reform
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: virtus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - petrgazarov@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/multi_part_date.rb
114
+ - lib/multi_part_date/version.rb
115
+ - multi_part_date.gemspec
116
+ homepage: https://github.com/policygenius/multi_part_date
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.6.4
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Splitting date field into multiple inputs on the form object level (reform)
140
+ test_files: []