stringex 1.3.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -23,6 +23,7 @@ which will populate the <tt>url</tt> attribute on the object with the converted
23
23
  Default is false. <em>NOTE: This is strongly not recommended
24
24
  if you are routing solely on the generated slug as you will no longer
25
25
  be guaranteed to lookup the expected record based on a duplicate slug.</em>
26
+ <tt>:limit</tt>:: If set, will limit length of url generated. Default is nil.
26
27
 
27
28
 
28
29
  In order to use the generated url attribute, you will probably want to override <tt>to_param</tt> like so, in your Model:
data/Rakefile CHANGED
@@ -212,7 +212,7 @@ begin
212
212
  }
213
213
  gem.rdoc_options = %w{--main README.rdoc --charset utf-8 --line-numbers}
214
214
  gem.extra_rdoc_files = %w{MIT-LICENSE README.rdoc}
215
- gem.version = "1.3.3"
215
+ gem.version = "1.4.0"
216
216
  end
217
217
 
218
218
  Jeweler::GemcutterTasks.new
@@ -34,6 +34,7 @@ module Stringex
34
34
  cattr_accessor :duplicate_count_separator
35
35
  cattr_accessor :allow_slash
36
36
  cattr_accessor :allow_duplicates
37
+ cattr_accessor :url_limit
37
38
 
38
39
  if options[:sync_url]
39
40
  before_validation(:ensure_unique_url)
@@ -52,6 +53,7 @@ module Stringex
52
53
  self.duplicate_count_separator = options[:duplicate_count_separator] || "-"
53
54
  self.allow_slash = options[:allow_slash] || false
54
55
  self.allow_duplicates = options[:allow_duplicates] || false
56
+ self.url_limit = options[:limit] || nil
55
57
 
56
58
  class_eval <<-"END"
57
59
  def #{url_attribute}
@@ -85,7 +87,7 @@ module Stringex
85
87
  url_attribute = self.class.url_attribute
86
88
  separator = self.class.duplicate_count_separator
87
89
  base_url = self.send(url_attribute)
88
- 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
90
+ 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
89
91
  conditions = ["#{url_attribute} LIKE ?", base_url+'%']
90
92
  unless new_record?
91
93
  conditions.first << " and id != ?"
@@ -36,7 +36,11 @@ module Stringex
36
36
  # acts_as_url[link:classes/Stringex/ActsAsUrl/ClassMethods.html#M000012]
37
37
  # but can be called manually in order to generate an URI-friendly version of any string.
38
38
  def to_url(options = {})
39
- remove_formatting(options).downcase.replace_whitespace("-").collapse("-")
39
+ remove_formatting(options).downcase.replace_whitespace("-").collapse("-").limit(options[:limit])
40
+ end
41
+
42
+ def limit(lim = nil)
43
+ lim.nil? ? self : self[0...lim]
40
44
  end
41
45
 
42
46
  # Performs multiple text manipulations. Essentially a shortcut for typing them all. View source
data/stringex.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{stringex}
8
- s.version = "1.3.3"
8
+ s.version = "1.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Russell Norris"]
12
- s.date = %q{2012-04-24}
12
+ s.date = %q{2012-05-04}
13
13
  s.description = %q{Some [hopefully] useful extensions to Ruby's String class. Stringex is made up of three libraries: ActsAsUrl [permalink solution with better character translation], Unidecoder [Unicode to ASCII transliteration], and StringExtensions [miscellaneous helper methods for the String class].}
14
14
  s.email = %q{rsl@luckysneaks.com}
15
15
  s.extra_rdoc_files = [
@@ -53,6 +53,10 @@ ActiveRecord::Schema.define(:version => 1) do
53
53
  create_table :ununiquments, :force => true do |t|
54
54
  t.string :title, :url, :other
55
55
  end
56
+
57
+ create_table :limituments, :force => true do |t|
58
+ t.string :title, :url, :other
59
+ end
56
60
  end
57
61
  ActiveRecord::Migration.verbose = true
58
62
 
@@ -97,6 +101,10 @@ class Ununiqument < ActiveRecord::Base
97
101
  acts_as_url :title, :allow_duplicates => true
98
102
  end
99
103
 
104
+ class Limitument < ActiveRecord::Base
105
+ acts_as_url :title, :limit => 13
106
+ end
107
+
100
108
  class ActsAsUrlTest < Test::Unit::TestCase
101
109
  def test_should_create_url
102
110
  @doc = Document.create(:title => "Let's Make a Test Title, <em>Okay</em>?")
@@ -234,4 +242,9 @@ class ActsAsUrlTest < Test::Unit::TestCase
234
242
  assert !@doc.valid?
235
243
  assert_equal "initial", @doc.url
236
244
  end
245
+
246
+ def test_should_allow_url_limit
247
+ @doc = Limitument.create(:title => "I am much too long")
248
+ assert_equal "i-am-much-too", @doc.url
249
+ end
237
250
  end
@@ -213,4 +213,8 @@ class StringExtensionsTest < Test::Unit::TestCase
213
213
 
214
214
  assert_equal "now-with-hyphens", "----now---------with-hyphens--------".collapse("-")
215
215
  end
216
+
217
+ def test_to_url_limit
218
+ assert_equal "I am much too long".to_url(:limit => 13), "i-am-much-too"
219
+ end
216
220
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stringex
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 3
9
- - 3
10
- version: 1.3.3
8
+ - 4
9
+ - 0
10
+ version: 1.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Russell Norris
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-24 00:00:00 -04:00
18
+ date: 2012-05-04 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21