pat / gutentag

A good, simple, solid tagging extension for ActiveRecord.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tag.names_for_scope when scope is empty

opened this issue · comments

I'm using Tag.names_for_scope on a multi-tenant project, and getting results that I didn't expect when a given tenant has no tags. Here's a spec showing the issue:

it "returns an empty array for an empty scope" do
  Article.create :title => "mammals", :tag_names => %w[ koala wombat ]
  Article.create :title => "birds",   :tag_names => %w[ cassowary ]

  expect(Gutentag::Tag.names_for_scope(Article.where(:title => "reptiles"))).
    to match_array([])
end

When run, this produces:

  1) Tag names for scopes returns an empty array for an empty scope
     Failure/Error:
       expect(Gutentag::Tag.names_for_scope(Article.where(:title => "reptiles"))).
         to match_array([])

       expected collection contained:  []
       actual collection contained:    ["cassowary", "koala", "wombat"]
       the extra elements were:        ["cassowary", "koala", "wombat"]
     # ./spec/lib/tag_names_for_scope_spec.rb:27:in `block (2 levels) in <top (required)>'

Before I work up a patch, I wanted to check whether this makes sense to you as a matter of design.

Hi Mike - definitely sounds like a bug to me. You're very welcome to write a patch, but also if you've got other things keeping you busy let me know, I should be able to look into a fix in the next few days if needed.

I'm not blocked at the moment, I just dropped a quick & brutal patch into my copy:

      if scope.is_a?(ActiveRecord::Relation)
        if scope.current_scope.present?
          join_conditions[:taggable_id] = scope.select(:id)
        else
          return Gutentag::Tag.none
        end
      end

Holding off on a PR till I have time to think about whether there's a more elegant way to write that.

Hi Mike - just following up on this - if you'd like to submit a PR, even if it's just the above code, that'd be great. We can always look at cleaning it up later.