punctual_date_select 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be7786f84046998c4c418f1502b479b8c1d8fbff
4
- data.tar.gz: 5c935f40d868f2152b43ad994bb8b7ac7695585c
3
+ metadata.gz: 0deb7f39a8a4b4a36251e069ddc28e1cd9826f4c
4
+ data.tar.gz: abb398567b48a4d67bafde2789bcbf5705b83bba
5
5
  SHA512:
6
- metadata.gz: 761c62adfe4be8f82644561c6685024ab57a3493b881142d2440b422df54a0826de6b8b5d89eccbfee193e547b2045a4cfdeb2116cf89c9c49584185bb54e7a3
7
- data.tar.gz: 04b41241496b48987d8c40dbe270804345fdeeb0d25bb536e44a08516c8075a730bd2aee332407d96de09ee1b19a420e6349707b3aa8c81c57018d370c38efbf
6
+ metadata.gz: c29796d52199eb24c75d825e1cd02cb6fd7e96ced0a8a1e82f8f13690939fba39172d4a4d5df28b3ede0d5bb22221a17e6278366d393c4fbe2e92e5a0566c952
7
+ data.tar.gz: de3a9c14268ec9a4a7179c74f9ea4dbc900fe6dc9dcf13bb0b65040866920eaa8b19ea72cf90c6fe426cb22c9d15566406607e4bc7a0c707794b608318144caa
data/README.md CHANGED
@@ -8,8 +8,6 @@ This may cause a human error in some use cases.
8
8
  With PunctualDateSelect, you can hold invalid date as they are specified by an user and show the invalid error message.
9
9
  Therefore users can notice the mistakes they've made.
10
10
 
11
- This gem is for Rails 4 currently.
12
-
13
11
  ## Key concept
14
12
 
15
13
  Using PunctualDateSelect, your date column value won't have Date object while it is non-existing date.
@@ -20,6 +18,13 @@ If the specified date from punctual_date_select fields is collect, it will be ca
20
18
 
21
19
  Note that you need to run validation before you save it not to send those parameters holding invalid date data to DB.
22
20
 
21
+ ## Supported Versions
22
+
23
+ * Ruby
24
+ * 2.0 or higher
25
+ * Rails
26
+ * 4.2, 5.0, 5.1
27
+
23
28
  ## Installation
24
29
 
25
30
  Add this line to your application's Gemfile:
@@ -36,14 +36,6 @@ module PunctualDateSelect
36
36
  module ClassMethods
37
37
  def punctual_date_column(*args)
38
38
  args.each do |column_name|
39
- cast_method = :"cast_#{column_name}_if_possible"
40
- before_validation cast_method
41
-
42
- define_method cast_method do
43
- casted_date = send(column_name).try(:to_date)
44
- send("#{column_name}=", casted_date) if casted_date
45
- end
46
-
47
39
  validation_method = :"validate_#{column_name}_is_casted"
48
40
  validate validation_method
49
41
 
@@ -52,16 +44,15 @@ module PunctualDateSelect
52
44
  end
53
45
 
54
46
  define_method "#{column_name}=" do |value|
55
- self[column_name] = (value.kind_of?(Hash) && !value.values.any?{|t| !t.blank?}) ? nil : value
56
- if value.kind_of?(Hash) && !value.kind_of?(PunctualDateSelect::DateHash)
47
+ if value.kind_of?(Hash) && !value.kind_of?(PunctualDateSelect::DateHash) && (value.keys & %i[year month day]).any?
57
48
  class << value
58
49
  include PunctualDateSelect::DateHash
59
50
  end
60
51
  end
61
- self[column_name]
52
+ self[column_name] = (value.kind_of?(Hash) && value.values.any?{|t| t.blank?}) ? nil : value
62
53
  end
63
54
 
64
- private cast_method, validation_method
55
+ private validation_method
65
56
  end
66
57
  end
67
58
  end
@@ -70,5 +61,23 @@ module PunctualDateSelect
70
61
  base.extend(ClassMethods)
71
62
  end
72
63
  end
64
+
65
+ module Type
66
+ cast_method = defined?(ActiveModel::Type) ? :value_from_multiparameter_assignment : :cast_value
67
+ define_method cast_method do |value|
68
+ if value.kind_of?(PunctualDateSelect::DateHash)
69
+ value.try(:to_date) || value
70
+ else
71
+ super(value)
72
+ end
73
+ end
74
+ end
73
75
  end
74
76
  ActiveRecord::Base.send(:include, PunctualDateSelect::Model)
77
+ if defined?(ActiveModel::Type)
78
+ ActiveModel::Type::Date.send(:prepend, PunctualDateSelect::Type)
79
+ ActiveModel::Type::DateTime.send(:prepend, PunctualDateSelect::Type)
80
+ else
81
+ ActiveRecord::Type::Date.send(:prepend, PunctualDateSelect::Type)
82
+ ActiveRecord::Type::DateTime.send(:prepend, PunctualDateSelect::Type)
83
+ end
@@ -1,3 +1,3 @@
1
1
  module PunctualDateSelect
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: punctual_date_select
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nay3
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-13 00:00:00.000000000 Z
11
+ date: 2017-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler