Skip to content

Commit

Permalink
Add writable_clasification_tags
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Dec 12, 2017
1 parent 5549221 commit d3af28d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/extensions/ar_taggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/extensions/ar_taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit d3af28d

Please sign in to comment.