slug 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 5
2
+ :patch: 6
3
3
  :major: 0
4
4
  :minor: 5
data/lib/slug/slug.rb CHANGED
@@ -36,13 +36,22 @@ module Slug
36
36
  module InstanceMethods
37
37
 
38
38
  # Sets the slug. Called before create.
39
- def set_slug
40
- validate_slug_columns
41
- self[self.slug_column] = self.send(self.slug_source) if self[self.slug_column].blank?
39
+ # By default, set_slug won't change slug if one already exists. Pass :force => true to overwrite.
40
+ def set_slug(opts={})
41
+ validate_slug_columns
42
+ return unless self[self.slug_column].blank? || opts[:force] == true
43
+
44
+ original_slug = self[self.slug_column]
45
+ self[self.slug_column] = self.send(self.slug_source)
42
46
 
43
47
  strip_diacritics_from_slug
44
48
  normalize_slug
45
- assign_slug_sequence
49
+ assign_slug_sequence unless self[self.slug_column] == original_slug # don't try to increment seq if slug hasn't changed
50
+ end
51
+
52
+ # Overwrite existing slug based on current contents of source column.
53
+ def reset_slug
54
+ set_slug(:force => true)
46
55
  end
47
56
 
48
57
  # Overrides to_param to return the model's slug.
data/slug.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{slug}
8
- s.version = "0.5.5"
8
+ s.version = "0.5.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Koski"]
12
- s.date = %q{2010-04-29}
12
+ s.date = %q{2010-06-24}
13
13
  s.description = %q{Simple, straightforward slugs for your ActiveRecord models.}
14
14
  s.email = %q{ben.koski@gmail.com}
15
15
  s.extra_rdoc_files = [
data/test/test_slug.rb CHANGED
@@ -90,6 +90,33 @@ class TestSlug < Test::Unit::TestCase
90
90
  assert_equal 'slug1', a.slug
91
91
  end
92
92
 
93
+ context "resetting a slug" do
94
+
95
+ setup do
96
+ @article = Article.create(:headline => 'test headline')
97
+ @original_slug = @article.slug
98
+ end
99
+
100
+ should "maintain the same slug if slug column hasn't changed" do
101
+ @article.reset_slug
102
+ assert_equal @original_slug, @article.slug
103
+ end
104
+
105
+ should "change slug if slug column has updated" do
106
+ @article.headline = "donkey"
107
+ @article.reset_slug
108
+ assert_not_equal @original_slug, @article.slug
109
+ end
110
+
111
+ should "maintain sequence" do
112
+ @existing_article = Article.create!(:headline => 'world cup')
113
+ @article.headline = "world cup"
114
+ @article.reset_slug
115
+ assert_equal 'world-cup-1', @article.slug
116
+ end
117
+
118
+ end
119
+
93
120
 
94
121
  context "slug normalization" do
95
122
  setup do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Koski
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-04-29 00:00:00 -04:00
12
+ date: 2010-06-24 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency