diff --git a/app/models/dialog_field_serializer.rb b/app/models/dialog_field_serializer.rb index 69dec8a62cc..8de130d70c6 100644 --- a/app/models/dialog_field_serializer.rb +++ b/app/models/dialog_field_serializer.rb @@ -23,7 +23,7 @@ def serialize(dialog_field, all_attributes = false) if dialog_field.type == "DialogFieldTagControl" category = Category.find_by(:id => dialog_field.category) - dialog_field.options.merge!(:category_name => category.name, :category_description => category.description) + dialog_field.options.merge!(:category_name => category.name, :category_description => category.description) if category end included_attributes(dialog_field.as_json(:methods => [:type, :values]), all_attributes).merge(extra_attributes) diff --git a/spec/models/dialog_field_serializer_spec.rb b/spec/models/dialog_field_serializer_spec.rb index c090045e885..49e7a5ce90b 100644 --- a/spec/models/dialog_field_serializer_spec.rb +++ b/spec/models/dialog_field_serializer_spec.rb @@ -116,21 +116,40 @@ let(:dynamic) { false } let(:category) { double("Category", :name => "best category ever", :description => "best category ever") } - before do - allow(Category).to receive(:find_by).with(:id => "123").and_return(category) - allow(dialog_field).to receive(:values).and_return("values") + context "with category" do + before do + allow(Category).to receive(:find_by).with(:id => "123").and_return(category) + allow(dialog_field).to receive(:values).and_return("values") + end + + it "serializes the category name and description" do + expect(dialog_field_serializer.serialize(dialog_field)) + .to eq(expected_serialized_values.merge( + "resource_action" => "serialized resource action", + "options" => { + :category_id => "123", + :category_name => "best category ever", + :category_description => "best category ever" + } + )) + end end - it "serializes the category name and description" do - expect(dialog_field_serializer.serialize(dialog_field)) - .to eq(expected_serialized_values.merge( - "resource_action" => "serialized resource action", - "options" => { - :category_id => "123", - :category_name => "best category ever", - :category_description => "best category ever" - } - )) + context "without category" do + before do + allow(Category).to receive(:find_by).with(:id => "123").and_return(nil) + allow(dialog_field).to receive(:values).and_return("values") + end + + it "serializes the category name" do + expect(dialog_field_serializer.serialize(dialog_field)) + .to eq(expected_serialized_values.merge( + "resource_action" => "serialized resource action", + "options" => { + :category_id => "123", + } + )) + end end end end