active_date_range 0.5.2 → 0.5.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0b596b6208103ad08949d24191796250a8df396ed0f2536b35578bb250781b3
4
- data.tar.gz: 9ade9b1b706a11f9892353a63f9acb61c5e537602de67954c37434f24e0c23bb
3
+ metadata.gz: fa7f0092dab70e955754434aa37df4f7c0c11c5461d54b377c68d935ad115330
4
+ data.tar.gz: 44658f76ff5dca0336804ce7b7dc44fee1b96699365bd04e8bbf93741ff77f75
5
5
  SHA512:
6
- metadata.gz: 1a5ff4a0c6b173751d60f2cc959b614e59216e2c17085556ffe34674596ed74bb776882fcc576f7da8ac20e749d8a1aa9a740210e6da3dad85efb4f93876b086
7
- data.tar.gz: 5960b5d080937c6ef690aefb4b6f758035c93a2d483ded6370307253526c69ccf72bfd0398e8f441610a8a2ced1520e860f331b3782e87441b3b05c5c10ffa72
6
+ metadata.gz: e7363eeba6c6e8747afd3e3f576fe2d3b24fbe03b17b30de980458fe4e65b9a13a31344a31207d9ae09fe8d0f7a7e97589e6c697f9564fa7977bbf227e80ac01
7
+ data.tar.gz: 5a3d15754ce33007ae78e56a0790ac5ede08f34a043d72e0e9866f61f912b2ebec9081af7df285927f83b3d6fd762c20bb4cbb11ae0bf411b7499966f7306e6e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## 0.5.3
2
+
3
+ * Add `safe_parse`, which returns `nil` instead of raising for input that isn't a valid date range. Use it for input you don't control, like a query parameter or a form field:
4
+
5
+ ```ruby
6
+ DateRange.safe_parse("202101..202112") # => DateRange(2021-01-01..2021-12-31)
7
+ DateRange.safe_parse("test") # => nil
8
+ DateRange.safe_parse("202112..202101") # => nil
9
+ ```
10
+
11
+ *wesselmoneybird*
12
+
13
+ * Add a `safe: true` option to the `:date_range` ActiveModel type, which casts to `nil` instead of raising. Use it for attributes fed by input you don't control, so a validation can report the problem:
14
+
15
+ ```ruby
16
+ attribute :period, :date_range, safe: true
17
+ ```
18
+
19
+ *wesselmoneybird*
20
+
1
21
  ## 0.5.2
2
22
 
3
23
  * Add calendar-aware `DateRangeValidator` for ActiveModel validations. Unlike `validates_length_of` which compares day counts (causing `1.month` to use ~30.44 day averages), this validator uses date arithmetic so that February (28 or 29 days) correctly satisfies `minimum_duration: 1.month`:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_date_range (0.5.2)
4
+ active_date_range (0.5.3)
5
5
  activesupport (~> 8.0)
6
6
  i18n (~> 1.6)
7
7
 
@@ -45,9 +45,9 @@ GEM
45
45
  bigdecimal (4.0.1)
46
46
  builder (3.3.0)
47
47
  coderay (1.1.3)
48
- concurrent-ruby (1.3.6)
48
+ concurrent-ruby (1.3.7)
49
49
  connection_pool (3.0.2)
50
- crass (1.0.6)
50
+ crass (1.0.7)
51
51
  date (3.5.1)
52
52
  drb (2.2.3)
53
53
  erb (6.0.1)
@@ -86,7 +86,7 @@ GEM
86
86
  rb-fsevent (~> 0.10, >= 0.10.3)
87
87
  rb-inotify (~> 0.9, >= 0.9.10)
88
88
  logger (1.7.0)
89
- loofah (2.25.0)
89
+ loofah (2.25.2)
90
90
  crass (~> 1.0.2)
91
91
  nokogiri (>= 1.12.0)
92
92
  lumberjack (1.4.2)
@@ -95,10 +95,10 @@ GEM
95
95
  mini_portile2 (2.8.9)
96
96
  minitest (5.27.0)
97
97
  nenv (0.3.0)
98
- nokogiri (1.19.1)
98
+ nokogiri (1.19.4)
99
99
  mini_portile2 (~> 2.8.2)
100
100
  racc (~> 1.4)
101
- nokogiri (1.19.1-x86_64-linux-gnu)
101
+ nokogiri (1.19.4-x86_64-linux-gnu)
102
102
  racc (~> 1.4)
103
103
  notiffany (0.1.3)
104
104
  nenv (~> 0.1)
@@ -131,8 +131,8 @@ GEM
131
131
  activesupport (>= 5.0.0)
132
132
  minitest
133
133
  nokogiri (>= 1.6)
134
- rails-html-sanitizer (1.6.2)
135
- loofah (~> 2.21)
134
+ rails-html-sanitizer (1.7.1)
135
+ loofah (~> 2.25, >= 2.25.2)
136
136
  nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
137
137
  railties (8.1.2)
138
138
  actionpack (= 8.1.2)
@@ -218,4 +218,4 @@ DEPENDENCIES
218
218
  rubocop-rails (~> 2.33)
219
219
 
220
220
  BUNDLED WITH
221
- 2.3.5
221
+ 2.6.9
data/README.md CHANGED
@@ -55,6 +55,14 @@ ActiveDateRange::DateRange.parse('20210101..20210115')
55
55
 
56
56
  Parse accepts three formats: `YYYYMM..YYYYMM`, `YYYYMMDD..YYYYMMDD` and any short hand like `this_month`.
57
57
 
58
+ `parse` raises `InvalidDateRangeFormat` or `InvalidDateRange` on input it can't handle. For input you don't control, like a query parameter or a form field, use `safe_parse` instead. It returns `nil` in those cases, the same way `safe_constantize` relates to `constantize`:
59
+
60
+ ```ruby
61
+ ActiveDateRange::DateRange.safe_parse('202101..202112') # => DateRange(2021-01-01..2021-12-31)
62
+ ActiveDateRange::DateRange.safe_parse('test') # => nil
63
+ ActiveDateRange::DateRange.safe_parse('202112..202101') # => nil
64
+ ```
65
+
58
66
  ### The date range instance
59
67
 
60
68
  A `DateRange` object is an extension of a regular `Range` object. You can use [all methods available on `Range`](https://ruby-doc.org/core-3.0.0/Range.html):
@@ -126,6 +134,21 @@ class Report
126
134
  end
127
135
  ```
128
136
 
137
+ The attribute raises on a value it can't parse. For an attribute fed by input you don't control, like a query parameter or a form field, pass `safe: true`. The attribute then casts to `nil` instead, so a validation can report the problem:
138
+
139
+ ```ruby
140
+ class ReportFilter
141
+ include ActiveModel::Attributes
142
+ include ActiveModel::Validations
143
+
144
+ attribute :period, :date_range, safe: true
145
+
146
+ validates :period, date_range: { bounded: true }
147
+ end
148
+
149
+ ReportFilter.new(period: "test").period # => nil
150
+ ```
151
+
129
152
  ### Usage example
130
153
 
131
154
  Use the shorthands to link to a specific period:
@@ -2,8 +2,17 @@
2
2
 
3
3
  module ActiveDateRange
4
4
  class DateRangeType < ActiveModel::Type::String
5
+ # Pass <tt>safe: true</tt> for attributes fed by input you don't control, like a query
6
+ # parameter. The attribute then casts to nil instead of raising:
7
+ #
8
+ # attribute :period, :date_range, safe: true
9
+ def initialize(safe: false)
10
+ @safe = safe
11
+ super()
12
+ end
13
+
5
14
  def cast(value)
6
- ActiveDateRange::DateRange.parse(value)
15
+ @safe ? ActiveDateRange::DateRange.safe_parse(value) : ActiveDateRange::DateRange.parse(value)
7
16
  end
8
17
  end
9
18
  end
@@ -41,6 +41,19 @@ module ActiveDateRange
41
41
  DateRange.new(parse_date(begin_date), parse_date(end_date, last: true))
42
42
  end
43
43
 
44
+ # Parses a date range string like #parse, but returns nil when the input isn't a valid date
45
+ # range, the same way <tt>safe_constantize</tt> relates to <tt>constantize</tt>. Use this for
46
+ # input you don't control, like a query parameter or a form field.
47
+ #
48
+ # DateRange.safe_parse("202101..202112") # => DateRange(2021-01-01..2021-12-31)
49
+ # DateRange.safe_parse("test") # => nil
50
+ # DateRange.safe_parse("202112..202101") # => nil
51
+ def self.safe_parse(input)
52
+ parse(input)
53
+ rescue InvalidDateRangeFormat, InvalidDateRange
54
+ nil
55
+ end
56
+
44
57
  def self.parse_date(input, last: false)
45
58
  return if input.blank?
46
59
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveDateRange
4
- VERSION = "0.5.2"
4
+ VERSION = "0.5.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_date_range
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edwin Vlieg