diff options
| author | Zog | 2018-01-12 10:11:51 +0100 |
|---|---|---|
| committer | Zog | 2018-02-26 10:00:34 +0100 |
| commit | 3e2ec765db2835be3772a57b4fc045c7c2e0e4ed (patch) | |
| tree | a387ac9dc478699188de13e5fa9df5d685d7d3dd | |
| parent | 9af498376d51dbd505cf6d276a02e7bfb24e9aa5 (diff) | |
| download | chouette-core-3e2ec765db2835be3772a57b4fc045c7c2e0e4ed.tar.bz2 | |
Add a validation mechanism
| -rw-r--r-- | app/models/concerns/custom_fields_support.rb | 9 | ||||
| -rw-r--r-- | app/models/custom_field.rb | 21 | ||||
| -rw-r--r-- | spec/models/custom_field_spec.rb | 9 |
3 files changed, 35 insertions, 4 deletions
diff --git a/app/models/concerns/custom_fields_support.rb b/app/models/concerns/custom_fields_support.rb index e8ecfcb7c..d562cf227 100644 --- a/app/models/concerns/custom_fields_support.rb +++ b/app/models/concerns/custom_fields_support.rb @@ -2,18 +2,25 @@ module CustomFieldsSupport extend ActiveSupport::Concern included do + validate :custom_fields_values_are_valid + def self.custom_fields CustomField.where(resource_type: self.name.split("::").last) end def custom_fields HashWithIndifferentAccess[*self.class.custom_fields.map do |v| - [v.code, CustomField::Value.new(v, custom_field_value(v.code))] + [v.code, CustomField::Value.new(self, v, custom_field_value(v.code))] end.flatten] end def custom_field_value key (custom_field_values || {})[key.to_s] end + + private + def custom_fields_values_are_valid + custom_fields.values.all?{|cf| cf.valid?} + end end end diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 2572d5c80..5a7c1954c 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -8,17 +8,18 @@ class CustomField < ActiveRecord::Base validates :code, uniqueness: {scope: [:resource_type, :workgroup_id], case_sensitive: false} class Value - def self.new custom_field, value + def self.new owner, custom_field, value field_type = custom_field.options["field_type"] klass_name = field_type && "CustomField::Value::#{field_type.classify}" klass = klass_name && const_defined?(klass_name) ? klass_name.constantize : CustomField::Value::Base - klass.new custom_field, value + klass.new owner, custom_field, value end class Base - def initialize custom_field, value + def initialize owner, custom_field, value @custom_field = custom_field @raw_value = value + @owner = owner end %i(code name field_type options).each do |attr| @@ -27,15 +28,29 @@ class CustomField < ActiveRecord::Base end end + def valid? + true + end + def value @raw_value end + + def errors_key + "custom_fields.#{code}" + end end class Integer < Base def value @raw_value.to_i end + + def valid? + unless ActiveRecord::Base::NumericalityValidator.new(attributes: 42).send(:parse_raw_value_as_an_integer, @raw_value).present? + @owner.errors.add errors_key, "'#{@raw_value}' is not a valid integer" + end + end end class String < Base diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb index d1c6d803a..54a905116 100644 --- a/spec/models/custom_field_spec.rb +++ b/spec/models/custom_field_spec.rb @@ -44,6 +44,15 @@ RSpec.describe CustomField, type: :model do it "should cast the value" do expect(vj.custom_fields[:energy].value).to eq 99 end + + it "should validate the value" do + vj = build :vehicle_journey, custom_field_values: {energy: "99"} + expect(vj.validate).to be_truthy + + vj = build :vehicle_journey, custom_field_values: {energy: "azerty"} + expect(vj.validate).to be_falsy + expect(vj.errors.messages[:"custom_fields.energy"]).to be_present + end end context "with a 'string' field_type" do |
