You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This gem also provides an Active Record adapter for persisting and retrieving measurements with their units, and model validations.
164
+
165
+
Columns are expected to have the `_value` and `_unit` suffix, and be `DECIMAL` and `VARCHAR`, and defaults are accepted. Customizing the column used to hold units is supported, see below for details.
This will validate that the unit is defined on the measurement, and that there is a value.
249
+
250
+
Rather than `true` the validation can accept a hash with the following options:
251
+
252
+
*`message`: Override the default "is invalid" message.
253
+
*`units`: A subset of units available for this measurement. Units must be in existing measurement.
254
+
*`greater_than`
255
+
*`greater_than_or_equal_to`
256
+
*`equal_to`
257
+
*`less_than`
258
+
*`less_than_or_equal_to`
259
+
260
+
All comparison validations require `Measured::Measurable` values, not scalars. Most of these options replace the `numericality` validator which compares the measurement/method name/proc to the column's value. Validations can also be combined with `presence` validator.
261
+
262
+
**Note:** Validations are strongly recommended since assigning an invalid unit will cause the measurement to return `nil`, even if there is a value:
263
+
264
+
```ruby
265
+
thing =Thing.new
266
+
thing.total_length_value =1
267
+
thing.total_length_unit ="invalid"
268
+
thing.total_length # nil
269
+
```
270
+
161
271
## Units and conversions
162
272
163
273
### SI units support
@@ -269,7 +379,7 @@ Existing alternatives which were considered:
269
379
***Cons**
270
380
* Opens up and modifies `Array`, `Date`, `Fixnum`, `Math`, `Numeric`, `String`, `Time`, and `Object`, then depends on those changes internally.
271
381
* Lots of code to solve a relatively simple problem.
0 commit comments