aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZog2018-04-06 10:09:10 +0200
committercedricnjanga2018-04-10 13:42:14 -0700
commit7f7f93caf090aee755b23b408d5da2374088217b (patch)
tree967e5bb385449db3acd3e42762ce68de865ebbf5
parent3ad0c0d06a821d1d5632b64c7c6b51f8cd43c538 (diff)
downloadchouette-core-7f7f93caf090aee755b23b408d5da2374088217b.tar.bz2
Refs #6461; Don't initilialize CustomFields when they are not loaded by the model
-rw-r--r--app/models/concerns/custom_fields_support.rb1
-rw-r--r--spec/models/custom_field_spec.rb5
2 files changed, 5 insertions, 1 deletions
diff --git a/app/models/concerns/custom_fields_support.rb b/app/models/concerns/custom_fields_support.rb
index 017f496a8..ba4ea9912 100644
--- a/app/models/concerns/custom_fields_support.rb
+++ b/app/models/concerns/custom_fields_support.rb
@@ -33,6 +33,7 @@ module CustomFieldsSupport
end
def initialize_custom_fields
+ return unless self.attributes.has_key?("custom_field_values")
self.custom_field_values ||= {}
custom_fields(:all).values.each &:initialize_custom_field
custom_fields(:all).each do |k, v|
diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb
index 1dfe6d33c..4c1b65a15 100644
--- a/spec/models/custom_field_spec.rb
+++ b/spec/models/custom_field_spec.rb
@@ -47,11 +47,14 @@ RSpec.describe CustomField, type: :model do
let!(:field){ [create(:custom_field, code: :energy, field_type: 'list', options: {list_values: %w(foo bar baz)})] }
let!( :vj ){ create :vehicle_journey, custom_field_values: {energy: "1"} }
it "should cast the value" do
- p vj.custom_fields
expect(vj.custom_fields[:energy].value).to eq 1
expect(vj.custom_fields[:energy].display_value).to eq "bar"
end
+ it "should not break initailizartion if the model does not have the :custom_field_values attribute" do
+ expect{Chouette::VehicleJourney.where(id: vj.id).select(:id).last}.to_not raise_error(ActiveModel::MissingAttributeError)
+ end
+
it "should validate the value" do
{
"1" => true,