Ruby on Rails Thursday, May 24, 2012

Hi all,

I just installed the Twitter gem and created a partial where I can publish my latest tweets. Because the output
is plain text I created an application helper which should replace the hash tags into some links.

First of all is this a convenient way or is there even a better solution like build in link to helper or something like that (couldn't find anything so far).

My second question is regards my helper function. I had a solution which was working but the code sucked:


def change_hashes
 hashes = text.scan(/#\d*\w*/)
    hashes.each do |hash|
      text = sanitize(text.gsub(hash, link_to(hash, "https://twitter.com/#!/search/#{hash.sub(/#/, "%23")}")))
    end
    return text
end

So I refactored it into this:

def change_hashes(text)
    text.gsub(/#\d*\w*/) { |s| text.sub(s, link_to(s, "https://twitter.com/#!/search/#{s.sub(/#/, "%23")}")) }
  end

The problem is that if I the tweet contains more than one hash the whole tweet is being duplicated. Maybe someone can pinpoint me into the right direction?


Cheers
Greg

No comments:

Post a Comment