Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

to_url and acts_as_url limit and tests

  • Loading branch information...
commit f501d7a55857a960aa2152ec9fe3fa006b19ab7d 1 parent f449a25
@ehoch ehoch authored
View
1  README.rdoc
@@ -23,6 +23,7 @@ which will populate the <tt>url</tt> attribute on the object with the converted
Default is false. <em>NOTE: This is strongly not recommended
if you are routing solely on the generated slug as you will no longer
be guaranteed to lookup the expected record based on a duplicate slug.</em>
+<tt>:limit</tt>:: If set, will limit length of url generated. Default is nil.
In order to use the generated url attribute, you will probably want to override <tt>to_param</tt> like so, in your Model:
View
4 lib/stringex/acts_as_url.rb
@@ -34,6 +34,7 @@ def acts_as_url(attribute, options = {})
cattr_accessor :duplicate_count_separator
cattr_accessor :allow_slash
cattr_accessor :allow_duplicates
+ cattr_accessor :url_limit
if options[:sync_url]
before_validation(:ensure_unique_url)
@@ -52,6 +53,7 @@ def acts_as_url(attribute, options = {})
self.duplicate_count_separator = options[:duplicate_count_separator] || "-"
self.allow_slash = options[:allow_slash] || false
self.allow_duplicates = options[:allow_duplicates] || false
+ self.url_limit = options[:limit] || nil
class_eval <<-"END"
def #{url_attribute}
@@ -85,7 +87,7 @@ def ensure_unique_url
url_attribute = self.class.url_attribute
separator = self.class.duplicate_count_separator
base_url = self.send(url_attribute)
- base_url = self.send(self.class.attribute_to_urlify).to_s.to_url(:allow_slash => self.allow_slash) if base_url.blank? || !self.only_when_blank
+ base_url = self.send(self.class.attribute_to_urlify).to_s.to_url(:allow_slash => self.allow_slash, :limit => self.url_limit) if base_url.blank? || !self.only_when_blank
conditions = ["#{url_attribute} LIKE ?", base_url+'%']
unless new_record?
conditions.first << " and id != ?"
View
6 lib/stringex/string_extensions.rb
@@ -36,7 +36,11 @@ def to_html(lite_mode = false)
# acts_as_url[link:classes/Stringex/ActsAsUrl/ClassMethods.html#M000012]
# but can be called manually in order to generate an URI-friendly version of any string.
def to_url(options = {})
- remove_formatting(options).downcase.replace_whitespace("-").collapse("-")
+ remove_formatting(options).downcase.replace_whitespace("-").collapse("-").limit(options[:limit])
+ end
+
+ def limit(lim = nil)
+ lim.is_a?(Integer) ? self[0...lim] : self
end
# Performs multiple text manipulations. Essentially a shortcut for typing them all. View source
View
13 test/acts_as_url_test.rb
@@ -53,6 +53,10 @@
create_table :ununiquments, :force => true do |t|
t.string :title, :url, :other
end
+
+ create_table :limituments, :force => true do |t|
+ t.string :title, :url, :other
+ end
end
ActiveRecord::Migration.verbose = true
@@ -97,6 +101,10 @@ class Ununiqument < ActiveRecord::Base
acts_as_url :title, :allow_duplicates => true
end
+class Limitument < ActiveRecord::Base
+ acts_as_url :title, :limit => 13
+end
+
class ActsAsUrlTest < Test::Unit::TestCase
def test_should_create_url
@doc = Document.create(:title => "Let's Make a Test Title, <em>Okay</em>?")
@@ -234,4 +242,9 @@ def test_should_only_update_url_if_model_is_valid
assert !@doc.valid?
assert_equal "initial", @doc.url
end
+
+ def test_should_allow_url_limit
+ @doc = Limitument.create(:title => "I am much too long")
+ assert_equal "i-am-much-too", @doc.url
+ end
end
View
4 test/string_extensions_test.rb
@@ -213,4 +213,8 @@ def test_collapse
assert_equal "now-with-hyphens", "----now---------with-hyphens--------".collapse("-")
end
+
+ def test_to_url_limit
+ assert_equal "I am much too long".to_url(:limit => 13), "i-am-much-too"
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.