-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from Shopify/uk-merge-measured-rails
Merge `measured-rails` into this gem
- Loading branch information
Showing
26 changed files
with
1,584 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec | ||
|
||
gem "activerecord" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ source 'https://rubygems.org' | |
gemspec path: '..' | ||
|
||
gem 'activesupport', '~> 6.0' | ||
gem "activerecord", '~> 6.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ source 'https://rubygems.org' | |
gemspec path: '..' | ||
|
||
gem 'activesupport', '~> 6.1' | ||
gem "activerecord", '~> 6.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ source 'https://rubygems.org' | |
gemspec path: '..' | ||
|
||
gem 'activesupport', '~> 7.0' | ||
gem 'activerecord', '~> 7.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec path: '..' | ||
|
||
gem 'activesupport', github: 'rails/rails', branch: 'main' | ||
gem 'activerecord', github: 'rails/rails', branch: 'main' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# frozen_string_literal: true | ||
|
||
module Measured::Rails::ActiveRecord | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def measured(measured_class, *fields) | ||
options = fields.extract_options! | ||
options = {}.merge(options) | ||
|
||
measured_class = measured_class.constantize if measured_class.is_a?(String) | ||
unless measured_class.is_a?(Class) && measured_class.ancestors.include?(Measured::Measurable) | ||
raise Measured::Rails::Error, "Expecting #{ measured_class } to be a subclass of Measured::Measurable" | ||
end | ||
|
||
options[:class] = measured_class | ||
|
||
fields.map(&:to_sym).each do |field| | ||
raise Measured::Rails::Error, "The field #{ field } has already been measured" if measured_fields.key?(field) | ||
|
||
measured_fields[field] = options | ||
|
||
unit_field_name = if options[:unit_field_name] | ||
measured_fields[field][:unit_field_name] = options[:unit_field_name].to_s | ||
else | ||
"#{ field }_unit" | ||
end | ||
|
||
value_field_name = if options[:value_field_name] | ||
measured_fields[field][:value_field_name] = options[:value_field_name].to_s | ||
else | ||
"#{ field }_value" | ||
end | ||
|
||
# Reader to retrieve measured object | ||
define_method(field) do | ||
value = public_send(value_field_name) | ||
unit = public_send(unit_field_name) | ||
|
||
return nil unless value && unit | ||
|
||
instance = instance_variable_get("@measured_#{ field }") if instance_variable_defined?("@measured_#{ field }") | ||
new_instance = begin | ||
measured_class.new(value, unit) | ||
rescue Measured::UnitError | ||
nil | ||
end | ||
|
||
if instance == new_instance | ||
instance | ||
else | ||
instance_variable_set("@measured_#{ field }", new_instance) | ||
end | ||
end | ||
|
||
# Writer to assign measured object | ||
define_method("#{ field }=") do |incoming| | ||
if incoming.is_a?(measured_class) | ||
instance_variable_set("@measured_#{ field }", incoming) | ||
precision = self.column_for_attribute(value_field_name).precision | ||
scale = self.column_for_attribute(value_field_name).scale | ||
rounded_to_scale_value = incoming.value.round(scale) | ||
|
||
max = self.class.measured_fields[field][:max_on_assignment] | ||
if max && rounded_to_scale_value > max | ||
rounded_to_scale_value = max | ||
elsif rounded_to_scale_value.to_i.to_s.length > (precision - scale) | ||
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 - scale} significant digits." | ||
end | ||
|
||
public_send("#{ value_field_name }=", rounded_to_scale_value) | ||
public_send("#{ unit_field_name }=", incoming.unit.name) | ||
else | ||
instance_variable_set("@measured_#{ field }", nil) | ||
public_send("#{ value_field_name}=", nil) | ||
public_send("#{ unit_field_name }=", nil) | ||
end | ||
end | ||
|
||
# Writer to override unit assignment | ||
redefine_method("#{ unit_field_name }=") do |incoming| | ||
unit_name = measured_class.unit_system.unit_for(incoming).try!(:name) | ||
write_attribute(unit_field_name, unit_name || incoming) | ||
end | ||
end | ||
end | ||
|
||
def measured_fields | ||
@measured_fields ||= {} | ||
end | ||
|
||
end | ||
|
||
module Length | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def measured_length(*fields) | ||
measured(Measured::Length, *fields) | ||
end | ||
end | ||
end | ||
|
||
module Volume | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def measured_volume(*fields) | ||
measured(Measured::Volume, *fields) | ||
end | ||
end | ||
end | ||
|
||
module Weight | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def measured_weight(*fields) | ||
measured(Measured::Weight, *fields) | ||
end | ||
end | ||
end | ||
end | ||
|
||
::ActiveRecord::Base.include( | ||
Measured::Rails::ActiveRecord, | ||
Measured::Rails::ActiveRecord::Length, | ||
Measured::Rails::ActiveRecord::Volume, | ||
Measured::Rails::ActiveRecord::Weight, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_model/validations" | ||
|
||
class MeasuredValidator < ActiveModel::EachValidator | ||
CHECKS = { | ||
greater_than: :>, | ||
greater_than_or_equal_to: :>=, | ||
equal_to: :==, | ||
less_than: :<, | ||
less_than_or_equal_to: :<=, | ||
}.freeze | ||
|
||
def validate_each(record, attribute, measurable) | ||
measured_config = record.class.measured_fields[attribute] | ||
unit_field_name = measured_config[:unit_field_name] || "#{ attribute }_unit" | ||
value_field_name = measured_config[:value_field_name] || "#{ attribute }_value" | ||
|
||
measured_class = measured_config[:class] | ||
|
||
measurable_unit_name = record.public_send(unit_field_name) | ||
measurable_value = record.public_send(value_field_name) | ||
|
||
return unless measurable_unit_name.present? || measurable_value.present? | ||
|
||
measurable_unit = measured_class.unit_system.unit_for(measurable_unit_name) | ||
record.errors.add(attribute, message(record, "is not a valid unit")) unless measurable_unit | ||
|
||
if options[:units] && measurable_unit.present? | ||
valid_units = Array(options[:units]).map { |unit| measured_class.unit_system.unit_for(unit) } | ||
record.errors.add(attribute, message(record, "is not a valid unit")) unless valid_units.include?(measurable_unit) | ||
end | ||
|
||
if measurable_unit && measurable_value.present? | ||
options.slice(*CHECKS.keys).each do |option, value| | ||
comparable_value = value_for(value, record) | ||
comparable_value = measured_class.new(comparable_value, measurable_unit) unless comparable_value.is_a?(Measured::Measurable) | ||
unless measurable.public_send(CHECKS[option], comparable_value) | ||
record.errors.add(attribute, message(record, "#{measurable.to_s} must be #{CHECKS[option]} #{comparable_value}")) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def message(record, default_message) | ||
if options[:message].respond_to?(:call) | ||
options[:message].call(record) | ||
else | ||
options[:message] || default_message | ||
end | ||
end | ||
|
||
def value_for(key, record) | ||
value = case key | ||
when Proc | ||
key.call(record) | ||
when Symbol | ||
record.send(key) | ||
else | ||
key | ||
end | ||
|
||
raise ArgumentError, ":#{ value } must be a number or a Measurable object" unless (value.is_a?(Numeric) || value.is_a?(Measured::Measurable)) | ||
value | ||
end | ||
end |
Oops, something went wrong.