active_date_range 0.5.3 → 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 +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/lib/active_date_range/active_model_type.rb +9 -6
- data/lib/active_date_range/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8f3c8036025bdc70d1d72d08a4d46d5f72ccefc6e072782b15f266fb783b6397
|
|
4
|
+
data.tar.gz: 8b5dd01d16009fbd7d2df94c3bf65dd71de81f31c610288277bd1dba96cdb610
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bf8dfe1ba846c42abf2c544819b2938ab477c611d3b8a38775f692fe69e02fdaaa098b5c3634a7369c18cc6ebb4e41cb76b7591acd49736ea2543e146c9764a3
|
|
7
|
+
data.tar.gz: 4f10617d476efd7275a37e6a3594a2d193bd59aa95c86f25aadc1ae2cc6200e28a0081f55b69b64ed2d1cefc2a7d9a3a938a09b5199c3798c5de0c53c2900d4d
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
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
|
+
|
|
1
11
|
## 0.5.3
|
|
2
12
|
|
|
3
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:
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -134,14 +134,14 @@ class Report
|
|
|
134
134
|
end
|
|
135
135
|
```
|
|
136
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 `
|
|
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
138
|
|
|
139
139
|
```ruby
|
|
140
140
|
class ReportFilter
|
|
141
141
|
include ActiveModel::Attributes
|
|
142
142
|
include ActiveModel::Validations
|
|
143
143
|
|
|
144
|
-
attribute :period, :date_range,
|
|
144
|
+
attribute :period, :date_range, safe_parse: true
|
|
145
145
|
|
|
146
146
|
validates :period, date_range: { bounded: true }
|
|
147
147
|
end
|
|
@@ -2,17 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
module ActiveDateRange
|
|
4
4
|
class DateRangeType < ActiveModel::Type::String
|
|
5
|
-
# Pass <tt>
|
|
6
|
-
# parameter. The attribute then casts
|
|
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:
|
|
7
8
|
#
|
|
8
|
-
# attribute :period, :date_range,
|
|
9
|
-
def initialize(
|
|
10
|
-
@
|
|
9
|
+
# attribute :period, :date_range, safe_parse: true
|
|
10
|
+
def initialize(safe_parse: false)
|
|
11
|
+
@safe_parse = safe_parse
|
|
11
12
|
super()
|
|
12
13
|
end
|
|
13
14
|
|
|
14
15
|
def cast(value)
|
|
15
|
-
|
|
16
|
+
return ActiveDateRange::DateRange.safe_parse(value) if @safe_parse
|
|
17
|
+
|
|
18
|
+
ActiveDateRange::DateRange.parse(value)
|
|
16
19
|
end
|
|
17
20
|
end
|
|
18
21
|
end
|