rsl / stringex

Some [hopefully] useful extensions to Ruby’s String class. It is made up of three libraries: ActsAsUrl [permalink solution with better character translation], Unidecoder [Unicode to Ascii transliteration], and StringExtensions [miscellaneous helper methods for the String class].
MIT License
984 stars 158 forks source link

Add url_taken_method option for overriding url collision logic #171

Closed joekur closed 8 years ago

joekur commented 9 years ago

I want to supply my own method that decides whether a given url candidate is unique or not. Our use-case is that we are also storing histories of urls, for which we do redirects, and we don't want collisions with those as well.

Example:

class Document
  acts_as_url :title, url_taken_method: :url_taken?

  def url_taken?(url)
    Document.where(url: url).where("id != ?, id).any? ||
      OtherThingy.where(url: url).any?
  end
end

Let me know your thoughts :)

rsl commented 9 years ago

i like idea. not sure about naming. thoughts? sorry about slow reply as well.

joekur commented 9 years ago

Hey, thanks for responding. Some other naming ideas: url_taken_if, validate_uniqueness (would probably switch the meaning of true/false response)

rsl commented 9 years ago

what about uniqueness_test or uniqueness_check? perhaps the latter.

joekur commented 9 years ago

Yeah I'm ok with that. Returns false if it is not unique?

rsl commented 9 years ago

should be optional and default to existing, no?

joekur commented 9 years ago

Yes I agree with that. I was commenting on how in my implementation the behavior of a "url_taken_method" made sense to return true if it the url is not unique. To me, if the name were "uniqueness_check", it seems it may instead be more intuitive that your custom method should return false if the url is not unique.

rsl commented 9 years ago

perhaps custom_uniqueness_check then? i see what you meant. ;)

rsl commented 8 years ago

merged and released as 2.6.0.