# Patching auto\_link for @username link conversion

**URL:** <https://www.ruby-forum.com/t/patching-auto-link-for-username-link-conversion/150791>\
**Category:** Rails\
**Created:** [October 13, 2008, 11:14am UTC](https://www.ruby-forum.com/t/patching-auto-link-for-username-link-conversion/150791 "2008-10-13T11:14:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 13, 2008, 11:14am UTC](https://www.ruby-forum.com/t/patching-auto-link-for-username-link-conversion/150791/1 "2008-10-13T11:14:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 13, 2008, 11:23am UTC](https://www.ruby-forum.com/t/patching-auto-link-for-username-link-conversion/150791/2 "2008-10-13T11:23:23Z")

</div>

On Oct 13, 10:14 am, Neil C. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [October 13, 2008, 1:06pm UTC](https://www.ruby-forum.com/t/patching-auto-link-for-username-link-conversion/150791/3 "2008-10-13T13:06:01Z")

</div>

Frederick C. wrote:

> On Oct 13, 10:14ï¿½am, Neil C. [removed\_email\_address@domain.invalid](mailto: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.
