measured-rails 0.0.7 → 0.0.8

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
  SHA1:
3
- metadata.gz: 5a228c3feac7e0810823eb7a838318ef24b1b459
4
- data.tar.gz: 46d35f6ce7e1d68f65fe23620b21eba1e8358e40
3
+ metadata.gz: 68355e20283509779816298a639059a8cbf389ee
4
+ data.tar.gz: 93c43698257e4831d50da35d2525ced2cb1dae7b
5
5
  SHA512:
6
- metadata.gz: cc6967a2287ac7cff1beb58a2af703bd1a512eafffd5f3791a6fd7f3027cb24771b3a8679cae7b5861bec4a36c35da3c6a9068a4e62476fe26c2ee0878f4c068
7
- data.tar.gz: 0c9041617dc8ac13d9c06092adb946daddae25c370d2415e2ada1bb41ed9424ce0280c1d8160285b448bc2f79f55d1711890462ae95cb3df388ead85e7f649f7
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 incoming.value.split[1].size > precision
49
- raise Measured::Rails::Error, "The value #{incoming.value} being set for column '#{value_field_name}' has too many significant digits. Please ensure it has no more than #{precision} significant digits."
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 }=", incoming.value.round(scale))
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
- record.errors.add(attribute, message) unless measurable.public_send(CHECKS[option], value_for(value, record))
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] || "is not a valid unit"
38
+ def message(default_message)
39
+ options[:message] || default_message
37
40
  end
38
41
 
39
42
  def value_for(key, record)
@@ -1,5 +1,5 @@
1
1
  module Measured
2
2
  module Rails
3
- VERSION = "0.0.7"
3
+ VERSION = "0.0.8"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measured-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips