active_date_range 0.5.2 → 0.6.0

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: 8f3c8036025bdc70d1d72d08a4d46d5f72ccefc6e072782b15f266fb783b6397
4
+ data.tar.gz: 8b5dd01d16009fbd7d2df94c3bf65dd71de81f31c610288277bd1dba96cdb610
5
5
  SHA512:
6
- metadata.gz: 1a5ff4a0c6b173751d60f2cc959b614e59216e2c17085556ffe34674596ed74bb776882fcc576f7da8ac20e749d8a1aa9a740210e6da3dad85efb4f93876b086
7
- data.tar.gz: 5960b5d080937c6ef690aefb4b6f758035c93a2d483ded6370307253526c69ccf72bfd0398e8f441610a8a2ced1520e860f331b3782e87441b3b05c5c10ffa72
6
+ metadata.gz: bf8dfe1ba846c42abf2c544819b2938ab477c611d3b8a38775f692fe69e02fdaaa098b5c3634a7369c18cc6ebb4e41cb76b7591acd49736ea2543e146c9764a3
7
+ data.tar.gz: 4f10617d476efd7275a37e6a3594a2d193bd59aa95c86f25aadc1ae2cc6200e28a0081f55b69b64ed2d1cefc2a7d9a3a938a09b5199c3798c5de0c53c2900d4d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,33 @@
1
+ ## 0.6.0
2
+
3
+ Rename the `safe:` option on the `:date_range` ActiveModel type to `safe_parse:`. On an attribute, `safe: true` reads as a claim that the value is trusted, while it actually describes how the value is parsed. The option was introduced in 0.5.3 and `safe:` now raises an `ArgumentError`:
4
+
5
+ ```ruby
6
+ attribute :period, :date_range, safe_parse: true
7
+ ```
8
+
9
+ *wesselmoneybird*
10
+
11
+ ## 0.5.3
12
+
13
+ * 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:
14
+
15
+ ```ruby
16
+ DateRange.safe_parse("202101..202112") # => DateRange(2021-01-01..2021-12-31)
17
+ DateRange.safe_parse("test") # => nil
18
+ DateRange.safe_parse("202112..202101") # => nil
19
+ ```
20
+
21
+ *wesselmoneybird*
22
+
23
+ * 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:
24
+
25
+ ```ruby
26
+ attribute :period, :date_range, safe: true
27
+ ```
28
+
29
+ *wesselmoneybird*
30
+
1
31
  ## 0.5.2
2
32
 
3
33
  * 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.6.0)
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_parse: true`. The attribute then casts through `safe_parse`, so it returns `nil` instead and 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_parse: 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,7 +2,19 @@
2
2
 
3
3
  module ActiveDateRange
4
4
  class DateRangeType < ActiveModel::Type::String
5
+ # Pass <tt>safe_parse: true</tt> for attributes fed by input you don't control, like a query
6
+ # parameter. The attribute then casts through .safe_parse, so it returns nil instead of
7
+ # raising:
8
+ #
9
+ # attribute :period, :date_range, safe_parse: true
10
+ def initialize(safe_parse: false)
11
+ @safe_parse = safe_parse
12
+ super()
13
+ end
14
+
5
15
  def cast(value)
16
+ return ActiveDateRange::DateRange.safe_parse(value) if @safe_parse
17
+
6
18
  ActiveDateRange::DateRange.parse(value)
7
19
  end
8
20
  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.6.0"
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.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edwin Vlieg