mbleigh / acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on
MIT License
4.96k stars 1.2k forks source link

Added option +taggable_model_type+ to +tagged_with+ scope #994

Open sequielo opened 4 years ago

sequielo commented 4 years ago

Context

When searching for multiple tags on different contexts on a model which is actually a table view, tagged_with will be translated based on taggable model.

On my particular case, this won't work as expected. The tags were created on the original model, not on the "table view" (this view is used for several calculations). I can't duplicate the tags on this table view as there will be a duplication of data.

Issue

ActsAsTaggableOn will translate the query based on taggable_model.base_class.name which is the name of the table view, but not the table which actually is being referenced by that table view.

Solution

I propose adding an optional parameter taggable_model_type to tagged_with method:

ProviderView.tagged_with(tags_list, on: :context, taggable_model_type: 'Provider')

By allowing to specify the taggable_model_type, the query will search tags on the correct table (and not on the view), and make the calculations on the table view as expected.