Skip to content

Commit

Permalink
reference classification instead of category
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Mar 28, 2018
1 parent 5a3dc82 commit 5fdb200
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
1 change: 1 addition & 0 deletions app/models/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
4 changes: 2 additions & 2 deletions app/models/dialog_field_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/dialog_field_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions spec/models/dialog_field_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions spec/models/dialog_field_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
},
Expand Down

0 comments on commit 5fdb200

Please sign in to comment.