measured-rails 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/measured/rails/active_record.rb +4 -3
- data/lib/measured/rails/validations.rb +9 -6
- data/lib/measured/rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68355e20283509779816298a639059a8cbf389ee
|
4
|
+
data.tar.gz: 93c43698257e4831d50da35d2525ced2cb1dae7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0f42793e76e6cacae038d8843d91089163ae57501db98f939e3b0fe39012882412936b0a52def1c485f2f9fe4d3d1ba3b9b7e8322bc502a7ab1c4962dca5061
|
7
|
+
data.tar.gz: fb820be61b40641dd5c16b40c243bc7a859b13d7dda91a4a95e1833fde9ea0077f306c124e4bd1e0265a4b4f94ae57d0c2d3aae0c155a0f8df2472ed6577172e
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Measured Rails [![Build Status](https://travis-ci.org/Shopify/measured-rails.svg)](https://travis-ci.org/Shopify/measured-rails)
|
1
|
+
# Measured Rails [![Build Status](https://travis-ci.org/Shopify/measured-rails.svg)](https://travis-ci.org/Shopify/measured-rails) [![Gem Version](https://badge.fury.io/rb/measured-rails.svg)](http://badge.fury.io/rb/measured-rails)
|
2
2
|
|
3
3
|
This gem is the Rails integration for the [measured](https://github.com/Shopify/measured) gem.
|
4
4
|
|
@@ -133,3 +133,4 @@ $ bundle exec rake test
|
|
133
133
|
|
134
134
|
* [Kevin McPhillips](https://github.com/kmcphillips) at [Shopify](http://shopify.com/careers)
|
135
135
|
* [Sai Warang](https://github.com/cyprusad) at [Shopify](http://shopify.com/careers)
|
136
|
+
* [Gareth du Plooy](https://github.com/garethson) at [Shopify](http://shopify.com/careers)
|
@@ -44,11 +44,12 @@ module Measured::Rails::ActiveRecord
|
|
44
44
|
value_field_name = "#{ field }_value"
|
45
45
|
precision = self.column_for_attribute(value_field_name).precision
|
46
46
|
scale = self.column_for_attribute(value_field_name).scale
|
47
|
+
rounded_to_scale_value = incoming.value.round(scale)
|
47
48
|
## For BigDecimal#split syntax, refer http://ruby-doc.org/stdlib-2.1.1/libdoc/bigdecimal/rdoc/BigDecimal.html#method-i-split
|
48
|
-
if
|
49
|
-
raise Measured::Rails::Error, "The value #{
|
49
|
+
if rounded_to_scale_value.split[1].size > precision
|
50
|
+
raise Measured::Rails::Error, "The value #{rounded_to_scale_value} being set for column '#{value_field_name}' has too many significant digits. Please ensure it has no more than #{precision} significant digits."
|
50
51
|
end
|
51
|
-
public_send("#{ value_field_name }=",
|
52
|
+
public_send("#{ value_field_name }=", rounded_to_scale_value)
|
52
53
|
public_send("#{ field }_unit=", incoming.unit)
|
53
54
|
else
|
54
55
|
instance_variable_set("@measured_#{ field }", nil)
|
@@ -16,24 +16,27 @@ class MeasuredValidator < ActiveModel::EachValidator
|
|
16
16
|
|
17
17
|
return unless measurable_unit.present? || measurable_value.present?
|
18
18
|
|
19
|
-
record.errors.add(attribute, message) if [measurable_unit.blank?, measurable_value.blank?].any?
|
19
|
+
record.errors.add(attribute, message("cannot be blank")) if [measurable_unit.blank?, measurable_value.blank?].any?
|
20
20
|
|
21
|
-
record.errors.add(attribute, message) unless measured_class.valid_unit?(measurable_unit)
|
21
|
+
record.errors.add(attribute, message("is not a valid unit")) unless measured_class.valid_unit?(measurable_unit)
|
22
22
|
|
23
23
|
if options[:units]
|
24
24
|
valid_units = [options[:units]].flatten.map{|u| measured_class.conversion.to_unit_name(u) }
|
25
|
-
record.errors.add(attribute, message) unless valid_units.include?(measured_class.conversion.to_unit_name(measurable_unit))
|
25
|
+
record.errors.add(attribute, message("is not a valid unit")) unless valid_units.include?(measured_class.conversion.to_unit_name(measurable_unit))
|
26
26
|
end
|
27
27
|
|
28
28
|
options.slice(*CHECKS.keys).each do |option, value|
|
29
|
-
|
29
|
+
comparable_value = value_for(value, record)
|
30
|
+
unless measurable.public_send(CHECKS[option], comparable_value)
|
31
|
+
record.errors.add(attribute, message("#{measurable.to_s} must be #{CHECKS[option]} #{comparable_value}"))
|
32
|
+
end
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
33
36
|
private
|
34
37
|
|
35
|
-
def message
|
36
|
-
options[:message] ||
|
38
|
+
def message(default_message)
|
39
|
+
options[:message] || default_message
|
37
40
|
end
|
38
41
|
|
39
42
|
def value_for(key, record)
|