Replace twitter-like usernames in a model's attribute automatically in Ruby on Rails -


i'm having hard time trying figure out how mvc way. have comment model holds body attribute. attribute may contain mentions such following:

hi! me mentioning @someone.

everytime posts comment, accessor method in model converts @mention #user:231# 231 user's id. way, if mentioned user changes username, can still link & mention him without problems on older comments.

now, want able access body attribute , mentions converted links. appears doing mvc way, within model not possible have investigated.

is there easy way this? don't wanna have convert mentions on controller because think lead repeated code , non-testable code.

could give me advice on this?

thanks!

parsing message particular format , re-saving in database can edited @ later date silly. i'm sorry blunt, doing way fundamentally broken 1 major reason: when user goes edit message later on, they'll see formatted text unless format back. want responsible doing this?

i hope not. because you're programmer, you're naturally lazy , things in few steps possible.

what recommend doing solve problem parse message when display on page. before go screaming @ me computationally intensive if you've got large amount of hits, hear me out. when it's displayed on page, can cache this:

<% cache comment %>   # code goes here <% end %> 

this store the final output in whatever cache you've set rails, possibly memcached or redis, using cache key includes comment's updated_at timestamp. pay attention this, it'll useful later.

retrieval cache faster parsing it, , easier convert message , forth between versions.

when comment updated, updated_at timestamp different , new comment rendered first, cached. in memcached (so i'm told) clear oldest cache key hasn't been referenced if needs more memory, thereby cleaning out older comments.


Comments