I am working on an app that has user generated content. What I''d like to do, is display X characters of, say, a title and put "..." for the rest. This sounds like something you see in soc networking type app and wondering if there is some code out there to do this. thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
<%= h truncate( model.property, X ) %> On Mon, Jun 9, 2008 at 5:16 AM, nahabed <roupenn-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I am working on an app that has user generated content. What I''d like > to do, is display X characters of, say, a title and put "..." for the > rest. This sounds like something you see in soc networking type app > and wondering if there is some code out there to do this. thanks. > > >-- xucros.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jun-09 15:28 UTC
Re: display "..." for text LONER than a certain length...
Nahabed, Just in case you didn''t follow Lucas''s explanation the ''X'' is the length you wish to display on the form. Kathleen On Jun 9, 2:28 am, Lucas <lucas.uy...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> <%= h truncate( model.property, X ) %> > > On Mon, Jun 9, 2008 at 5:16 AM, nahabed <roup...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am working on an app that has user generated content. What I''d like > > to do, is display X characters of, say, a title and put "..." for the > > rest. This sounds like something you see in soc networking type app > > and wondering if there is some code out there to do this. thanks. > > -- > xucros.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Lucas wrote:> <%= h truncate( model.property, X ) %>Hey, I''ve just got a quick question on this (I''ve run into needing this same thing, but I''ve done it a different way). Say I want to limit something to 50 characters before cutting it off and adding the "...". If it turns out that whatever I want to limit ends up only being 30 characters, will it still add the "..."? I''d assume it wouldn''t, because Rails is usually smarter than that, but its good to check before I go changing my app to use this method (which is way better than how I''ve been doing it) :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
DyingToLearn
2008-Jun-09 16:43 UTC
Re: display "..." for text LONER than a certain length...
@Dan, in short, rails is smart enough. See http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M001049 On Jun 9, 8:34 am, Dan __ <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Lucas wrote: > > <%= h truncate( model.property, X ) %> > > Hey, I''ve just got a quick question on this (I''ve run into needing this > same thing, but I''ve done it a different way). Say I want to limit > something to 50 characters before cutting it off and adding the "...". > If it turns out that whatever I want to limit ends up only being 30 > characters, will it still add the "..."? I''d assume it wouldn''t, > because Rails is usually smarter than that, but its good to check before > I go changing my app to use this method (which is way better than how > I''ve been doing it) :) > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dan __ wrote:> Lucas wrote: >> <%= h truncate( model.property, X ) %> > > Hey, I''ve just got a quick question on this (I''ve run into needing this > same thing, but I''ve done it a different way). Say I want to limit > something to 50 characters before cutting it off and adding the "...". > If it turns out that whatever I want to limit ends up only being 30 > characters, will it still add the "..."? I''d assume it wouldn''t, > because Rails is usually smarter than that, but its good to check before > I go changing my app to use this method (which is way better than how > I''ve been doing it) :)(model.property.length > X ? truncate(model.property, X-3)+''...'' : model.property) or something like that... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
DyingToLearn wrote:> @Dan, in short, rails is smart enough. See > http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M001049Beautiful! Simplifies my code a decent amount (gets rid of the if statements I had before to do the same job). Thanks a bunch :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> (model.property.length > X ? truncate(model.property, X-3)+''...'' : > model.property)Doh! Ignore that... truncate... Nice. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeffrey L. Taylor
2008-Jun-13 00:14 UTC
Re: display "..." for text LONER than a certain length...
Quoting nahabed <roupenn-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > I am working on an app that has user generated content. What I''d like > to do, is display X characters of, say, a title and put "..." for the > rest. This sounds like something you see in soc networking type app > and wondering if there is some code out there to do this. thanks.# truncate a string to N characters, add elipsis if over def short_desc(s, n=80) if s.nil? '''' elsif s.length <= n s else i = n - 4 # 4 characters in ellipsis while i > 0 && " \t".include?(s[i]) # is whitespace? i = i - 1 end if i <= 0 s else s.slice(0, i) + '' ...'' end end end HTH, Jeffrey --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-Jun-13 01:05 UTC
Re: display "..." for text LONER than a certain length...
I was under the impression that''s what truncate did, Jeffrey. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dhaval Parikh
2008-Jun-13 06:51 UTC
Re: display "..." for text LONER than a certain length...
hi I guess u want to do something like this some text upto say 30 char and after that ... so u can restrict just by doing <%= t.content[0..30]%>... Thanks Dhaval Parikh Software Engineer www.railshouse.com sales(AT)railshouse(DOT)com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeffrey L. Taylor
2008-Jun-13 16:34 UTC
Re: display "..." for text LONER than a certain length...
My bad. Made an improvement without verifying the result. Corrected line below uncommented. Sorry, Jeffrey Quoting Jeffrey L. Taylor <ror-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org>:> Quoting nahabed <roupenn-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > I am working on an app that has user generated content. What I''d like > > to do, is display X characters of, say, a title and put "..." for the > > rest. This sounds like something you see in soc networking type app > > and wondering if there is some code out there to do this. thanks. > > # truncate a string to N characters, add elipsis if over > def short_desc(s, n=80) > if s.nil? > '''' > elsif s.length <= n > s > else > i = n - 4 # 4 characters in ellipsis > while i > 0 && " \t".include?(s[i]) # is whitespace?while i > 0 && !" \t".include?(s[i]) # is not whitespace?> i = i - 1 > end > if i <= 0 > s > else > s.slice(0, i) + '' ...'' > end > end > end--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeffrey L. Taylor
2008-Jun-13 16:36 UTC
Re: display "..." for text LONER than a certain length...
Quoting Ryan Bigg (Radar) <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> I was under the impression that''s what truncate did, Jeffrey. >No, truncate just chops it off at N characters, possibly in mid-word. My code truncates at whitespace, i.e., between words. (Or at least it does after the correction I just posted.) Jeffrey --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2008-Jun-13 17:08 UTC
Re: display "..." for text LONER than a certain length...
On Jun 13, 2008, at 12:36 PM, Jeffrey L. Taylor wrote:> Quoting Ryan Bigg (Radar) <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> I was under the impression that''s what truncate did, Jeffrey. > > No, truncate just chops it off at N characters, possibly in mid- > word. My code > truncates at whitespace, i.e., between words. (Or at least it does > after the > correction I just posted.) > > JeffreyYou really, REALLY ought to have tests for this: class TruncateTest < Test::Unit::TestCase def test_fits str = "short" assert_equal str, truncate(str, 10) end def test_backs_up_to_space str = "short filibuster" exp = "short ..." assert_equal exp, truncate(str, 10) end def test_breaks_word_if_no_space str = "antidisestablishmentarianism" exp = "antidis..." assert_equal exp, truncate(str, 10) end end I didn''t look back at your code so these tests might not actually be the results that you want, but this ought to give you the right idea. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeffrey L. Taylor
2008-Jun-14 12:03 UTC
Re: display "..." for text LONER than a certain length...
Shorter, faster version # truncate a string to N characters, add elipsis if over def short_desc(s, n=80) if s.nil? '''' elsif s.length <= n s else # find last break character (space, tab, hyphen, slash, underscore) i = s.rindex(/[ \t-\/_]/, n) i = n - 4 if i.nil? # none, just truncate() s.slice(0, i+1) + ''...'' end end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeffrey L. Taylor
2008-Jun-14 12:05 UTC
Re: display "..." for text LONER than a certain length...
Quoting Rob Biedenharn <Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org>:> > On Jun 13, 2008, at 12:36 PM, Jeffrey L. Taylor wrote: > > Quoting Ryan Bigg (Radar) <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > >> I was under the impression that''s what truncate did, Jeffrey. > > > > No, truncate just chops it off at N characters, possibly in mid- > > word. My code > > truncates at whitespace, i.e., between words. (Or at least it does > > after the > > correction I just posted.) > > > > Jeffrey > > > You really, REALLY ought to have tests for this: > > class TruncateTest < Test::Unit::TestCase > def test_fits > str = "short" > assert_equal str, truncate(str, 10) > end > > def test_backs_up_to_space > str = "short filibuster" > exp = "short ..." > assert_equal exp, truncate(str, 10) > end > > def test_breaks_word_if_no_space > str = "antidisestablishmentarianism" > exp = "antidis..." > assert_equal exp, truncate(str, 10) > end > > end > > I didn''t look back at your code so these tests might not actually be > the results that you want, but this ought to give you the right idea. >It''s added to the TODO list. I''ll start with your examples. Thanks, Jeffrey --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeffrey L. Taylor
2008-Jun-14 12:39 UTC
Re: display "..." for text LONER than a certain length...
Quoting Jeffrey L. Taylor <ror-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org>: [snip]> It''s added to the TODO list. I''ll start with your examples. >Done. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---