Patching auto_link for @username link conversion


#1

Does anyone know of an easy way to convert a unique ‘@username’ (i.e.
@user.username) using an auto_link-esque helper? I have a method for
doing it on the way in to the database but, I realise there’s a few deal
breakers with that, such as changing the original character length of
whatever was posted (by inserting a href).

Would it be possible to patch auto_link (which I’m already using) to
work with an @username conversion? I was thinking something along these
lines;

 # File actionpack/lib/action_view/helpers/text_helper.rb, line 307

307: def auto_link(text, link = :all, href_options = {}, &block)
308: return ‘’ if text.blank?
309: case link
310: when :all then
auto_link_email_addresses(auto_link_urls(text, href_options, &block),
&block)
311: when :email_addresses then
auto_link_email_addresses(text, &block)
312: when :urls then auto_link_urls(text,
href_options, &block)
###: when :usernames then auto_link_usernames(text,
href_options, &block)
313: end
314: end

I haven’t done any patching or delving in to Rails internals before so I
don’t how tricky the auto_link_usernames method would be, or, if there’s
a better way. Any suggestions?


#2

On Oct 13, 10:14 am, Neil C. removed_email_address@domain.invalid
wrote:

Does anyone know of an easy way to convert a unique ‘@username’ (i.e.
@user.username) using an auto_link-esque helper? I have a method for
doing it on the way in to the database but, I realise there’s a few deal
breakers with that, such as changing the original character length of
whatever was posted (by inserting a href).

Would it be possible to patch auto_link (which I’m already using) to
work with an @username conversion? I was thinking something along these
lines;

You certainly could (personally I’d probably alias_method_chain it
just to add your auto_link_usernames behaviour.
I’d also worry about getting the scanning text for usernames bit right
first before wiring it into auto_link

Fred


#3

Frederick C. wrote:

On Oct 13, 10:14�am, Neil C. removed_email_address@domain.invalid
wrote:

Does anyone know of an easy way to convert a unique ‘@username’ (i.e.
@user.username) using an auto_link-esque helper? I have a method for
doing it on the way in to the database but, I realise there’s a few deal
breakers with that, such as changing the original character length of
whatever was posted (by inserting a href).

Would it be possible to patch auto_link (which I’m already using) to
work with an @username conversion? I was thinking something along these
lines;

You certainly could (personally I’d probably alias_method_chain it
just to add your auto_link_usernames behaviour.
I’d also worry about getting the scanning text for usernames bit right
first before wiring it into auto_link

Fred

Thanks Fred. I do have the scanning working just fine on the way in to
the database but, the problem is that auto_link doesn’t ‘auto link’
links preceded with @ - if it did, I would already have a working
solution (at least, if I was happy with the conversion being done on the
way in to the database). Either way, considering what needs to happen in
this particular application, the conversion is better-off being done in
the view and probably in tandem with auto_link (seeing as auto_link
already does the job just fine on ‘@comment.body’ - with everything but
@usernames).

I know little about alias_method_chain - am I right in presuming that it
allows behavior to be added without ‘overwriting’ a method? Where would
the alias_method_chain live?

Also, a little OT; I came across a link to your company site a little
while back and realised you’re based in Cambridge (IIRC). Do you get to
(or know of?) many Rails/web app meet-ups? I’m in lovely
Stratford-Upon-Avon. There doesn’t appear to be much in the way of
meet-ups around here.