Skip to content

Instantly share code, notes, and snippets.

@notch8-old
Created October 12, 2011 18:31
Show Gist options
  • Save notch8-old/1282103 to your computer and use it in GitHub Desktop.
Save notch8-old/1282103 to your computer and use it in GitHub Desktop.
263 def create_links
264 old_links = self.links.clone
265 new_links = self.content.scan(LINK_REGEXP)
266 new_links.each do |link_result|
267 if current_link = create_or_update_link link_result.first
268 old_links.delete(current_link)
269 end
270 end
271
272 old_links.each do |old_link|
273 old_link.destroy
274 end
275 end
276
277 def create_or_update_link raw_link_text
278 link_body = Link.safe_link_text(raw_link_text)
279 url_attr = Link.is_short_url?(raw_link_text) ? "short_url" : "long_url"
280 current_link = Link.find(:first, :conditions => ["? LIKE BINARY ? AND message_id = ?", url_attr, link_body, self.id])
281 if current_link
282 return current_link
283 else
284 new_link = self.links.build(url_attr.intern => link_body)
285 new_link.save
286 return nil
287 end
288 end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment