acts-as-taggable-on icon indicating copy to clipboard operation
acts-as-taggable-on copied to clipboard

Provide prefix option in tagged_with method in tag.rb for prefix match.

Open maygupta opened this issue 9 years ago • 26 comments

Currently :wild supports substring match only and not specifically prefix match.

maygupta avatar Jul 25 '14 12:07 maygupta

I will need specs for this feature. Update the changelog too to document this and squash the commits to have the code and the doc in the same one.

Thank you

seuros avatar Jul 25 '14 12:07 seuros

@ProGM , i think you wanted this feature.

seuros avatar Aug 28 '14 19:08 seuros

@seuros yep, it's that one! I think it could make sense to add also suffix, not only prefix :)

ProGM avatar Aug 28 '14 19:08 ProGM

suffix and prefix are not good names IMO , i would prefer starting and ending. 'dog' is not a prefix of 'doggy', WDYT ?

seuros avatar Aug 28 '14 19:08 seuros

@seuros I agree!

And what about something like: wild: :full, wild: :starting, wild: :ending or something like that? Maybe it's not very elegant, but all both are wildcards, aren't they?

ProGM avatar Aug 28 '14 19:08 ProGM

I don't like it. I want that coders understand the code without having to dig into the library to find it significance. Wild is ambiguous.

seuros avatar Aug 28 '14 20:08 seuros

Uhm... and what about starting_with and ending_with?

ProGM avatar Aug 28 '14 20:08 ProGM

as scope , not in the wild option.

That way we could have .starting_with('c').ending_with('t')

seuros avatar Aug 28 '14 20:08 seuros

Uhm... I don't think I got how you'd like to implement it.

ProGM avatar Aug 28 '14 20:08 ProGM

Like the current PR .

not with wild: :full, wild: :starting_with, wild: :ending_with

seuros avatar Aug 28 '14 20:08 seuros

Oooh yes, of curse :D I left that syntax after your last message ;) What I don't understand is: how do you want to implement starting_with with parameters?

User.tagged_with("my, tag, list", starting_with: ????) # => what is the parameter?

ProGM avatar Aug 28 '14 20:08 ProGM

We can't do that with tagged_with maybe

User.with_tags_starting_with('dog')

or

User.tagged.starting_with('cat')

seuros avatar Aug 28 '14 20:08 seuros

I don't like it very much... :
In that way you can't do something like this:

User.tagged_with("my, list, of, tags", any: true, starting: true, order_by_matching_tag_count: true)

ProGM avatar Aug 28 '14 21:08 ProGM

User.with_tags_starting_with(%w(my list of tags)).order_by_matching_tag_count

seuros avatar Aug 28 '14 21:08 seuros

Mh, in that way it's pretty cool. For coherency should we change also tagged_with? Isn't it a breaking change?

ProGM avatar Aug 28 '14 21:08 ProGM

We can't change it. We will just have to leave it as it and implement the new syntax.

We have to think about the context too.

User.tagged(['tags','skills']).starting_with('drive')

seuros avatar Aug 28 '14 21:08 seuros

oh, interesting, I like it! Do you have an idea on how to implement this feature?

ProGM avatar Aug 28 '14 21:08 ProGM

I will see how i can implement it using only arel nodes.

seuros avatar Aug 28 '14 22:08 seuros

If we want to keep the actual structure we can have an internal QueryBuilder class:

class QueryBuilder
  def initialize(parent)
    @parent = parent
    ...
  end

  def order_by_matching_tag_count
    @order << 'something_good'
    self
  end

  def starting_with(string)
    @where << 'something_good'
    self
  end

   ...

  def to_a
    query = @parent
    query = @parent.select(select_clause.join(',')) unless @select_clause.empty?
    query.joins(@joins.join(' '))
             .where(@conditions.join(' AND '))
             .group(@group)
             .having(@having)
             .order(@order_by.join(', '))
             .readonly(false)
  end
end

So you can do, in core.rb:

def tagged
    QueryBuilder.new(self)
end

I don't know if it's a good idea, just sharing something I though. WDYT?

ProGM avatar Aug 29 '14 06:08 ProGM

It should return a activerecord relation.

User.tagged.limit(3) User.tagged.where('xxx')

seuros avatar Aug 29 '14 07:08 seuros

aww you're right.

ProGM avatar Aug 29 '14 07:08 ProGM

@seuros do you ever tried to extend an active record relation? Me not. Should it include this module?

https://github.com/rails/rails/blob/83e42d52e37a33682fcac856330fd5d06e5a529c/activerecord/lib/active_record/relation/query_methods.rb

ProGM avatar Sep 01 '14 06:09 ProGM

I think you want to use scopes .

seuros avatar Sep 01 '14 06:09 seuros

@seuros Yes, I think so... I confess you I don't have a clear idea on how you like this to be done... Maybe I'm just taking this the wrong way.

Anyway, do you like to include those changes in this version or move everything in 4.0+?

ProGM avatar Sep 01 '14 07:09 ProGM

Hi

Provide prefix option in tagged_with method Did we push the change for prefix match ?

Thanks

On Mon, Sep 1, 2014 at 12:44 PM, Piero Dotti [email protected] wrote:

@seuros https://github.com/seuros Yes, I think so... I confess you I don't have a clear idea on how you like this to be done... Maybe I'm just taking this the wrong way.

Anyway, do you like to include those changes in this version or move everything in 4.0+?

— Reply to this email directly or view it on GitHub https://github.com/mbleigh/acts-as-taggable-on/pull/568#issuecomment-54027798 .

maygupta avatar Sep 12 '14 11:09 maygupta

@maygupta no.

seuros avatar Sep 12 '14 11:09 seuros