This is a simple one, maybe even something that might be nice to see in ActiveSupport::String. It simply limits the length of any string to a specified amount, and adds "..." if the string exceeds the length. I''d like to add whitespace detection to make it cleaner, but I haven''t had the pressing need :) class String def abbreviate(abbreviate_length) return self[0..abbreviate_length] + ''...'' if abbreviate_length < self.length return self end end I usually put this (and methods like it) in a file called "support.rb" in the lib folder of my Rails application. Then add "require ''support.rb''" to the bottom of my config/environment.rb file. Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
François Beausoleil
2005-Jun-14 15:28 UTC
Re: [REUSE] An ''abbreviate'' method for the String class
Have you seen ActionView::truncate ? Does the same thing. Of course, this could be moved in String itself. Bye ! François Duane Johnson said the following on 2005-06-14 11:25:> This is a simple one, maybe even something that might be nice to see in > ActiveSupport::String. It simply limits the length of any string to a > specified amount, and adds "..." if the string exceeds the length. I''d > like to add whitespace detection to make it cleaner, but I haven''t had > the pressing need :) > > class String > def abbreviate(abbreviate_length) > return self[0..abbreviate_length] + ''...'' if abbreviate_length < > self.length > return self > end > end > > I usually put this (and methods like it) in a file called "support.rb" > in the lib folder of my Rails application. Then add "require > ''support.rb''" to the bottom of my config/environment.rb file.
John Wilger
2005-Jun-14 15:29 UTC
Re: [REUSE] An ''abbreviate'' method for the String class
On 6/14/05, Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is a simple one, maybe even something that might be nice to see in > ActiveSupport::String. It simply limits the length of any string to a > specified amount, and adds "..." if the string exceeds the length. I''d like > to add whitespace detection to make it cleaner, but I haven''t had the > pressing need :)What''s the difference between this and ActionView::Helpers::TextHelper#excerpt ? -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
Duane Johnson
2005-Jun-14 15:35 UTC
Re: [REUSE] An ''abbreviate'' method for the String class
On Jun 14, 2005, at 9:29 AM, John Wilger wrote:> > What''s the difference between this and > ActionView::Helpers::TextHelper#excerpt ? > -- > Regards, > John WilgerThe difference is probably that I didn''t know about TextHelper :) Thanks for pointing that out. Actually, upon inspection it appears that TextHelper#truncate does what my ''abbreviate'' method does. It appears that #excerpt matches on a particular string sequence and adds ellipses (...) before and after the excerpt. Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Scott Barron
2005-Jun-14 15:39 UTC
Re: [REUSE] An ''abbreviate'' method for the String class
On Jun 14, 2005, at 11:35 AM, Duane Johnson wrote:> > On Jun 14, 2005, at 9:29 AM, John Wilger wrote: >> What''s the difference between this and >> ActionView::Helpers::TextHelper#excerpt ? >> -- >> Regards, >> John Wilger > The difference is probably that I didn''t know about TextHelper :) > Thanks for pointing that out. > > Actually, upon inspection it appears that TextHelper#truncate does > what my ''abbreviate'' method does. It appears that #excerpt matches on > a particular string sequence and adds ellipses (...) before and after > the excerpt. > > Duane Johnson > (canadaduane) >I wonder if these might be better suited to activesupport. Also, I just noticed a pluralize in TextHelper that should probably be removed since it is in AS::String. Thoughts? Want to do a patch? -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Michael Schuerig
2005-Jun-14 16:18 UTC
Re: [REUSE] An ''abbreviate'' method for the String class
On Tuesday 14 June 2005 17:39, Scott Barron wrote:> On Jun 14, 2005, at 11:35 AM, Duane Johnson wrote:> > Actually, upon inspection it appears that TextHelper#truncate does > > what my ''abbreviate'' method does. It appears that #excerpt matches > > on a particular string sequence and adds ellipses (...) before and > > after the excerpt.> I wonder if these might be better suited to activesupport. Also, I > just noticed a pluralize in TextHelper that should probably be > removed since it is in AS::String. Thoughts? Want to do a patch?It would be easier for me if these methods were methods on String. Detour: In BoilerPlate I have column meta data at the controller class level that defines which attributes are available and which defaults for ordering and some other things apply. Also, a closure (lambda) can be set for formatting the values in the column. Unfortunately, in the closure I can''t refer to any helper methods as they''re instance methods. A method on String wouldn''t be a problem. But then I''m not entirely happy with the meta data approach as it is, either. It''s just the best I''ve come up with. I''d prefer something that is evaluated in the context of the view template. The code I''m talking about looks like this class TasksController set_default_columns Task, ''description'', { :column => ''due_date'', :formatter => BoilerPlate::TemplateUtils::DATE_FORMATTER }, { :column => ''status_id'', :title => ''Status'', :path => ''status.name'', :type => :string, :include => :status } ... end This defines the default columns generated by the list action. The user can reorder and filter these columns and the user-defined changes are mixed and matched with the defaults. Michael -- Michael Schuerig The Fifth Rider of the Apocalypse mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org is a programmer. http://www.schuerig.de/michael/