diff --git a/app/models/classification.rb b/app/models/classification.rb index 9804a35805f4..f30cb4449535 100644 --- a/app/models/classification.rb +++ b/app/models/classification.rb @@ -35,6 +35,8 @@ class Classification < ApplicationRecord scope :is_category, -> { where(:parent_id => 0) } scope :is_entry, -> { where.not(:parent_id => 0) } + scope :with_writable_parents, -> { includes(:parent).where(:parents_classifications => { :read_only => false}) } + DEFAULT_NAMESPACE = "/managed" default_value_for :read_only, false diff --git a/lib/extensions/ar_taggable.rb b/lib/extensions/ar_taggable.rb index 7c1573343c66..3f1d01afcfa2 100644 --- a/lib/extensions/ar_taggable.rb +++ b/lib/extensions/ar_taggable.rb @@ -25,6 +25,10 @@ def self.split_tag_names(tags, separator) end.map(&:strip).uniq end + def writable_classification_tags + tags.merge(Classification.with_writable_parents) + end + module ClassMethods # @option options :cat [String|nil] optional category for the tags # @option options :ns [String|nil] optional namespace for the tags diff --git a/spec/lib/extensions/ar_taggable_spec.rb b/spec/lib/extensions/ar_taggable_spec.rb index 164816c782c7..f1cd5c52a93b 100644 --- a/spec/lib/extensions/ar_taggable_spec.rb +++ b/spec/lib/extensions/ar_taggable_spec.rb @@ -16,6 +16,25 @@ @vm4.tag_add("bos phi blt") end + describe '#writable_classification_tags' do + let(:parent_classification) { FactoryGirl.create(:classification, :description => "Environment", :name => "environment", :read_only => false) } + let(:classification) { FactoryGirl.create(:classification, :name => "prod", :description => "Production", :parent => parent_classification, :read_only => true) } + + before do + classification.assign_entry_to(@vm1) + end + + it "returns only tags as they would be entered in UI by user(edit tags screen)" do + expect(@vm1.tags.count).to eq(4) + expect(@vm1.writable_classification_tags.count).to eq(1) + expect(@vm1.writable_classification_tags.first.name).to eq('/managed/environment/prod') + expect(@vm1.writable_classification_tags.first).to be_kind_of(Tag) + + expect(@vm3.writable_classification_tags.count).to eq(0) + expect(@vm3.tags.count).to eq(3) + end + end + context ".find_tagged_with" do it ":any" do found = Host.find_tagged_with(:any => "red black", :ns => "/test/tags")