-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding additional configurable filters to tags #579
Conversation
I personally find the syntax very hard to understand, i don't mind adding this feature if the more clear. @bf4, i need your opinion on this. |
@seuros mmh... you're right. What about something like:
And... what you mean for "query based filters"? |
I want to do something like tag_list = ActsAsTaggableOn::TagList.new('dogs', 'friendships', parser: MyParser)
tag_list = ActsAsTaggableOn::TagList.new('dogs', 'friendships', parser: MyOtherParser) or tag_list = ActsAsTaggableOn::TagList.new('dogs', 'friendships')
tag_list.parser #=> ActsAsTaggableOn::TagListParser
tag_list.to_s #=> ['dogs', 'friendships']
tag_list.parser = DogReplacerParser
tag_list.to_s #=> ['cats', 'friendships'] I started few month ago by extracting the parser in https://github.com/mbleigh/acts-as-taggable-on/blob/master/lib/acts_as_taggable_on/tag_list_parser.rb |
Uhm... I understand. And the idea is to make it configurable?
|
Correct. ActsAsTaggableOn.default_parser = DogReplacerParser # default: ActsAsTaggableOn::TagListParser |
I can manage it if you want :) What is the common way to do? Should I work on this pull request or create a new one? |
Sure thing. I was think we should have an GenericParser class which we use to inherit all other parsers module ActsAsTaggableOn
##
# Returns a new TagList using the given tag string.
#
# Example:
# tag_list = ActsAsTaggableOn::GenericParser.new.parse("One , Two, Three")
# tag_list # ["One", "Two", "Three"]
class GenericParser
def initialize(tag_list)
@tag_list = tag_list
end
def parse
@tag_list.split(',')
end
end
The default Default parser should keep the current behavior for now, we can drop to GenericParse in version 4.x You can use this PR if you want. |
For So shound I turn |
Yes. That module is too complex IMO. I think that users who have complex tags requirement, should implement the complex logic in their app instead of providing multi delimiter/multi quote parser by default. If you feel that we can't do it without breaking existing applications. We could get start the version 4.0.0 next week with this feature. Wdyt ? |
I'm taking a look where the default parser is used right now... I think we can mantain compatibility. Take a look to my last commit and tell me if it was what you intended to do. P.S. I've not finished yet, of course :) |
string = string.join(ActsAsTaggableOn.glue) if string.respond_to?(:join) | ||
TagList.new.tap do |tag_list| | ||
string = string.to_s.dup | ||
class TagListParser < GenericParser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can leave the TagListParser as module and delegate the parsing to the DefaultParser and change our code to use DefaultParser instead.
Code using TagListParser will have the deprecation warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right.
One thing: should I leave the internal methods like delimiter
, double_quote_pattern
, single_quote_pattern
from TagListParser
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DefaultParser will have those.
TagListParser will have just the self.parse(string) method.
I agree with you, the delimiter will be deprecated then. |
@seuros Maybe tests needs to be reviewed a bit. |
@@ -85,7 +85,7 @@ def grouped_column_names_for(object) | |||
# User.tagged_with(["awesome", "cool"], :owned_by => foo ) # Users that are tagged with just awesome and cool by 'foo' | |||
# User.tagged_with(["awesome", "cool"], :owned_by => foo, :start_at => Date.today ) # Users that are tagged with just awesome, cool by 'foo' and starting today | |||
def tagged_with(tags, options = {}) | |||
tag_list = ActsAsTaggableOn::TagListParser.parse(tags) | |||
tag_list = ActsAsTaggableOn.default_parser.new(tags).parse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use ActsAsTaggableOn::DefaultParser
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean only here or everywhere?
So the tagged_with
feature will always use the default parser?
I was quite uncertain about this while committing...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, you are right 👍 . I got confused after reading the spec file
@ProGM , seem we are good to merge 💚 . Could you squash your commits and update the Readme with a small example ? |
d | ||
end | ||
|
||
# ( # Tag start delimiter ($1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, this is a little weird.. commenting it out and then re-implementing it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean? It's the same implementation taken from TagListParser
, moved here. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bf4, It explain the regex used in the method below.
Great PR and tests, but I don't think the new classes improve the design or understandability of the code.. at the end of the day, @seuros do you want to maintain it? I think the first commit, which was must simpler, is a better first implementation. |
@seuros @user = User.new(:name => "Bobby")
ActsAsTaggable.default_parser = MyParser
@user.tag_list = "east, south"
ActsAsTaggable.default_parser = MyParser2
@user.tag_list.add("north, west")
# => This will use MyParser instead of MyParser2! I try to fix. |
@bf4 @ProGM @user = User.new(:name => "Bobby")
ActsAsTaggable.default_parser = MyParser
@user.tag_list = "east, south"
other_tags = MyParser2.new("north; west")
@user.tag_list.add(other_tags) #=> Add should not parse if the object is a taglist. i think that changing the default parser will cause another thread to use it and we will have unexpected results. WDYT ? |
@seuros Oh, you are right! |
Can we change it to return a Taglist by default ?
i think you did it in line 252-262
LGTM. |
@seuros @user = User.new(:name => "Bobby")
@user.tag_list = "east, south"
@user.tag_list.add("north; west", parser: MyParser) #=> This will do nothing, because the `parse: true` is not specified
|
👍 Sound good |
@seuros |
💚 💚 💚 💚 Nice. Can you squash the commits ? Do you need help with that ? |
@seuros Yes, if you can help me with that... I don't want to make a mess! :) |
Ok git rebase d2144bb6e0aa75467eb7edf1655183cd752b9ff7^1 -i The sha1 is the one of the first commit , ^1 tells git to use that commit also. you will be presented with a list of commit , change all the You will be presented to edit the new commit message (by default it will contact all the commits message). Delete everything and write a descriptive comment. Save and exit.
|
8af759d
to
0608e41
Compare
@seuros I think it worked. Thank you! |
Yep. I will merge once travis go green. |
Adding additional configurable filters to tags
👍 |
Congrats!! |
Adding additional configurable filters to tags
Adding a configurable option to apply additional filters to the tags before inserting them.
It can be useful to apply regular expressions or other simple string modifications to tags.
Example:
If you configure it like this:
Your tags will look like this: