Skip to content

Commit

Permalink
Merge pull request ManageIQ#13766 from cfcosta/fix-Tag-tags
Browse files Browse the repository at this point in the history
Fix SQL error on Tag.tags
  • Loading branch information
kbrock authored Feb 8, 2017
2 parents 557aee0 + 0bd1a75 commit 60eeff4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
13 changes: 10 additions & 3 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ def self.to_tag(name, options = {})
end

def self.tags(options = {})
query = Tag.includes(:taggings)
query = query.where(Tagging.arel_table[:taggable_type].eq options[:taggable_type])
query = query.where(Tag.arel_table[:name].matches "#{options[:ns]}%") if options[:ns]
query = Tag.joins(:taggings)

if options[:taggable_type].present?
query = query.where(Tagging.arel_table[:taggable_type].eq(options[:taggable_type]))
end

if options[:ns].present?
query = query.where(Tag.arel_table[:name].matches("#{options[:ns]}%"))
end

Tag.filter_ns(query, options[:ns])
end

Expand Down
31 changes: 31 additions & 0 deletions spec/models/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@
end
end

describe ".tags" do
let!(:account) { FactoryGirl.create(:account) }
let!(:tag) { FactoryGirl.create(:tag) }

it "returns tags with tagged items" do
Tagging.create(:taggable => account, :tag => tag)

expect(Tag.tags).to eq [tag]
end

it "does not return tags without tagged items" do
expect(Tag.tags).to eq []
end

it "can be filtered by taggable type" do
Tagging.create(:taggable => account, :tag => tag)

expect(Tag.tags(:taggable_type => 'Account')).to eq [tag]
expect(Tag.tags(:taggable_type => 'User')).to eq []
end

context "when filtered by tag namespaces" do
it "returns tag names w" do
Tagging.create(:taggable => account, :tag => tag)

expect(Tag.tags(:ns => '/namespace/cat')).to eq [tag.name.split('/').last]
expect(Tag.tags(:ns => '/foo/bar')).to eq []
end
end
end

context ".filter_ns" do
it "normal case" do
tag1 = double
Expand Down

0 comments on commit 60eeff4

Please sign in to comment.