engine/lib/core_ext.rb

32 lines
978 B
Ruby
Raw Normal View History

2010-04-25 00:33:38 +00:00
## String
class String
# def perma_string(sep = '_')
# ActiveSupport::Inflector.parameterize(self, sep).to_s
# end
def slugify(options = {})
2010-05-11 21:38:52 +00:00
options = { :sep => '_', :without_extension => false, :downcase => false, :underscore => false }.merge(options)
2010-04-25 00:33:38 +00:00
# replace accented chars with ther ascii equivalents
s = ActiveSupport::Inflector.transliterate(self).to_s
# No more than one slash in a row
s.gsub!(/(\/[\/]+)/, '/')
# Remove leading or trailing space
s.strip!
# Remove leading or trailing slash
s.gsub! /(^[\/]+)|([\/]+$)/, ''
# Remove extensions
s.gsub! /(\.[a-zA-Z]{2,})/, '' if options[:without_extension]
# Downcase
s.downcase! if options[:downcase]
2010-04-25 00:33:38 +00:00
# Turn unwanted chars into the seperator
s.gsub!(/[^a-zA-Z0-9\-_\+\/]+/i, options[:sep])
2010-05-11 21:38:52 +00:00
# Underscore
2010-05-12 00:16:39 +00:00
s.gsub!(/[\-]/i, '_') if options[:underscore]
2010-04-25 00:33:38 +00:00
s
end
def slugify!(options = {})
replace(self.slugify(options))
end
2010-04-25 00:33:38 +00:00
end