diff --git a/app/models/classification.rb b/app/models/classification.rb index f30cb4449535..484f4b83248d 100644 --- a/app/models/classification.rb +++ b/app/models/classification.rb @@ -32,6 +32,7 @@ class Classification < ApplicationRecord scope :read_only, -> { where(:read_only => true) } scope :writeable, -> { where(:read_only => false) } + # NOTE: roots does not work (that checks :parent_id => nil) scope :is_category, -> { where(:parent_id => 0) } scope :is_entry, -> { where.not(:parent_id => 0) } diff --git a/app/models/dialog_field_importer.rb b/app/models/dialog_field_importer.rb index 1641d7144e37..1dec6f049226 100644 --- a/app/models/dialog_field_importer.rb +++ b/app/models/dialog_field_importer.rb @@ -37,9 +37,9 @@ def set_category_for_tag_control(dialog_field, dialog_field_attributes) def adjust_category(opts) return nil if opts[:category_description].nil? category = if opts[:category_id] - Category.find(opts[:category_id]) + Classification.is_category.find(opts[:category_id]) elsif opts[:category_name] - Category.find_by_name(opts[:category_name]) + Classification.is_category.find_by_name(opts[:category_name]) end category.try(:description) == opts[:category_description] ? category.try(:id).to_s : nil end diff --git a/app/models/dialog_field_serializer.rb b/app/models/dialog_field_serializer.rb index 6374a7a9b8ba..72d2e6adf25a 100644 --- a/app/models/dialog_field_serializer.rb +++ b/app/models/dialog_field_serializer.rb @@ -22,7 +22,7 @@ def serialize(dialog_field, all_attributes = false) end if dialog_field.type == "DialogFieldTagControl" - category = Category.find_by(:id => dialog_field.category) + category = Classification.is_category.find_by(:id => dialog_field.category) if category dialog_field.options.merge!(:category_name => category.name, :category_description => category.description) dialog_field.options[:force_single_value] = dialog_field.options[:force_single_value] || category.single_value diff --git a/spec/models/dialog_field_importer_spec.rb b/spec/models/dialog_field_importer_spec.rb index 933ca603bd38..411e162edc83 100644 --- a/spec/models/dialog_field_importer_spec.rb +++ b/spec/models/dialog_field_importer_spec.rb @@ -88,8 +88,8 @@ let(:category_description) { "best_category" } context "when the category exists by name and description" do - before do - @existing_category = Category.create!(:name => "best_category", :description => "best_category") + let!(:category) do + Classification.is_category.create!(:name => "best_category", :description => "best_category") end context "when the category description does not match" do @@ -104,7 +104,7 @@ context "when the category description matches" do it "uses the correct category, ignoring id" do dialog_field_importer.import_field(dialog_field) - expect(DialogFieldTagControl.first.category).to eq(@existing_category.id.to_s) + expect(DialogFieldTagControl.first.category).to eq(category.id.to_s) end end end @@ -123,30 +123,30 @@ end context "when the import file contains a category id a category name and a description" do - before do - @existing_category = Category.create!(:name => "best_category", :description => "best_category") + let!(:category) do + Classification.is_category.create!(:name => "best_category", :description => "best_category") end let(:options) do { - :category_id => @existing_category.id, - :category_name => @existing_category.name, - :category_description => @existing_category.description + :category_id => category.id, + :category_name => category.name, + :category_description => category.description } end it "uses the category id provided" do dialog_field_importer.import_field(dialog_field) - expect(DialogFieldTagControl.first.category).to eq(@existing_category.id.to_s) + expect(DialogFieldTagControl.first.category).to eq(category.id.to_s) end end context "when the import file contains a category id with a different description" do - before do - @existing_category = Category.create!(:name => "best_category", :description => "best_category") + let!(:category) do + Classification.is_category.create!(:name => "best_category", :description => "best_category") end let(:options) do { - :category_id => @existing_category.id, + :category_id => category.id, :category_description => "bad description" } end diff --git a/spec/models/dialog_field_serializer_spec.rb b/spec/models/dialog_field_serializer_spec.rb index 3ac470fcecb1..a567871b149f 100644 --- a/spec/models/dialog_field_serializer_spec.rb +++ b/spec/models/dialog_field_serializer_spec.rb @@ -132,14 +132,13 @@ end let(:type) { "DialogFieldTagControl" } - let(:options) { {:category_id => "123", :force_single_value => false} } + let(:options) { {:category_id => category.id.to_s, :force_single_value => false} } let(:dynamic) { false } let(:category) do - double("Category", :name => "best category ever", :description => "best category ever", :single_value => true) + Classification.create!(:name => "best_category_ever", :description => "best category ever", :single_value => true) end before do - allow(Category).to receive(:find_by).with(:id => "123").and_return(category) allow(dialog_field).to receive(:values).and_return("values") end @@ -152,8 +151,8 @@ "resource_action" => "serialized resource action", "dialog_field_responders" => dialog_field_responders, "options" => { - :category_id => "123", - :category_name => "best category ever", + :category_id => category.id.to_s, + :category_name => "best_category_ever", :category_description => "best category ever", :force_single_value => true },