diff options
| author | Zog | 2018-04-25 11:15:31 +0200 |
|---|---|---|
| committer | Johan Van Ryseghem | 2018-04-25 15:46:33 +0200 |
| commit | 0f0be7d01a87c64f30539205379e4d7588f6d73e (patch) | |
| tree | 11e5af8797ee3cbc3807b5f62877323333a5f029 | |
| parent | a0567f40c0ad57abcf630d97816e38f73750138e (diff) | |
| download | chouette-core-0f0be7d01a87c64f30539205379e4d7588f6d73e.tar.bz2 | |
Refs #6669; delay the custom fields initialization until the workgroup is loaded
| -rw-r--r-- | app/models/concerns/custom_fields_support.rb | 24 | ||||
| -rw-r--r-- | app/models/custom_field.rb | 8 | ||||
| -rw-r--r-- | spec/models/custom_field_spec.rb | 5 |
3 files changed, 33 insertions, 4 deletions
diff --git a/app/models/concerns/custom_fields_support.rb b/app/models/concerns/custom_fields_support.rb index fd872cc62..6e744d550 100644 --- a/app/models/concerns/custom_fields_support.rb +++ b/app/models/concerns/custom_fields_support.rb @@ -17,7 +17,7 @@ module CustomFieldsSupport end def method_missing method_name, *args - if method_name =~ /custom_field_*/ && method_name.to_sym != :custom_field_values && !@custom_fields_initialized + if !@custom_fields_initialized && method_name =~ /custom_field_*/ && method_name.to_sym != :custom_field_values initialize_custom_fields send method_name, *args else @@ -35,21 +35,37 @@ module CustomFieldsSupport end def custom_field_values= vals - out = {} - custom_fields.each do |code, field| - out[code] = field.preprocess_value_for_assignment(vals.symbolize_keys[code.to_sym]) + if custom_fields_initialized? + out = {} + custom_fields.each do |code, field| + out[code] = field.preprocess_value_for_assignment(vals.symbolize_keys[code.to_sym]) + end + @custom_fields_values_initialized = true + else + out = vals end write_attribute :custom_field_values, out end + def custom_fields_initialized? + !!@custom_fields_initialized + end + + def custom_fields_values_initialized? + !!@custom_fields_values_initialized + end + def initialize_custom_fields + return if custom_fields_initialized? return unless self.attributes.has_key?("custom_field_values") + return unless self.workgroup.present? self.custom_field_values ||= {} custom_fields.values.each &:initialize_custom_field custom_fields.each do |k, v| custom_field_values[k] ||= v.default_value end @custom_fields_initialized = true + self.custom_field_values = self.custom_field_values unless custom_fields_values_initialized? end def custom_field_value key diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index b5c08465d..15aee9a41 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -147,6 +147,10 @@ class CustomField < ApplicationModel @raw_value&.to_i end + def preprocess_value_for_assignment val + val&.to_i + end + def validate @valid = true return if @raw_value.is_a?(Fixnum) || @raw_value.is_a?(Float) @@ -167,6 +171,10 @@ class CustomField < ApplicationModel end end + def preprocess_value_for_assignment val + val + end + def display_value return unless value k = options["list_values"].is_a?(Hash) ? value.to_s : value.to_i diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb index 2b561e739..54fd4f9d8 100644 --- a/spec/models/custom_field_spec.rb +++ b/spec/models/custom_field_spec.rb @@ -52,12 +52,17 @@ RSpec.describe CustomField, type: :model do let(:ref2){ create :workbench_referential } before do create :custom_field, field_type: :integer, code: :ref1_energy, name: :energy, workgroup: ref1.workgroup, options: {default: 12} + create :custom_field, field_type: :integer, code: :ref1_energy, name: :energy, workgroup: ref1.workgroup, options: {default: 12}, resource_type: "Company" create :custom_field, field_type: :integer, code: :ref2_energy, name: :energy, workgroup: ref2.workgroup end it "should only initialize fields from the right workgroup" do ref1.switch expect(Chouette::VehicleJourney.new.custom_fields.keys).to eq ["ref1_energy"] expect(Chouette::VehicleJourney.new.custom_field_values["ref1_energy"]).to eq 12 + expect(Chouette::VehicleJourney.new(custom_field_values: {ref1_energy: 13}).custom_field_values["ref1_energy"]).to eq 13 + line_referential = create(:line_referential, workgroup: ref1.workgroup) + expect(line_referential.companies.build(custom_field_values: {ref1_energy: "13"}).custom_field_values["ref1_energy"]).to eq 13 + ref2.switch expect(Chouette::VehicleJourney.new.custom_fields.keys).to eq ["ref2_energy"] expect(Chouette::VehicleJourney.new.custom_field_values).to_not have_key "ref1_energy" |
