slugged 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -65,6 +65,7 @@ is\_sluggable accepts the source method name as a symbol, and an optional has of
65
65
  * _:slug\_column_ - the column in which to store the slug. Defaults to _:cached\_slug_
66
66
  * _:to\_param_ - if true (by default), overrides to_param to use the slug
67
67
  * _:use\_cache_ - uses Slugged.cache if available to cache any lookups e.g. in memcache.
68
+ * _:editable_ - if true (false is the default), allow the users to edit _cached\_slug_ column.
68
69
 
69
70
  Once installed, it provides the following methods:
70
71
 
@@ -1,6 +1,6 @@
1
1
  module Slugged
2
2
  module ActiveRecordMethods
3
- AR_CLASS_ATTRIBUTE_NAMES = %w(cached_slug_column slug_source slug_convertor_proc default_uuid_slug use_slug_history sync_slugs slug_scope use_slug_cache use_slug_to_param).map(&:to_sym)
3
+ AR_CLASS_ATTRIBUTE_NAMES = %w(cached_slug_column slug_source slug_convertor_proc default_uuid_slug use_slug_history sync_slugs slug_scope use_slug_cache use_slug_to_param editable).map(&:to_sym)
4
4
 
5
5
  def is_sluggable(source = :name, options = {})
6
6
  options.symbolize_keys!
@@ -25,10 +25,9 @@ module Slugged
25
25
  cached_value = send cached_slug_column
26
26
  cached_value.present? ? cached_value : id.to_s
27
27
  end
28
-
28
+
29
29
  def generate_slug
30
- slug_value = send(self.slug_source)
31
- slug_value = self.slug_convertor_proc.call(slug_value) if slug_value.present?
30
+ slug_value = self.class.slug_value_for(send(self.slug_source))
32
31
  if slug_value.present?
33
32
  scope = self.class.other_than(self).slug_scope_relation(self)
34
33
  slug_value = Slugged.next_value(scope, slug_value)
@@ -39,6 +38,10 @@ module Slugged
39
38
  write_attribute self.cached_slug_column, nil
40
39
  end
41
40
  end
41
+
42
+ def convert_cached_slug
43
+ write_attribute self.cached_slug_column, self.class.slug_value_for(send(self.cached_slug_column))
44
+ end
42
45
 
43
46
  def generate_slug!
44
47
  generate_slug
@@ -46,11 +49,19 @@ module Slugged
46
49
  end
47
50
 
48
51
  def autogenerate_slug
49
- generate_slug if should_generate_slug?
52
+ if should_convert_cached_slug?
53
+ convert_cached_slug
54
+ else
55
+ generate_slug if should_generate_slug?
56
+ end
50
57
  end
51
-
58
+
59
+ def should_convert_cached_slug?
60
+ send(:"#{self.cached_slug_column}_changed?") && !send(self.cached_slug_column).blank? && self.editable?
61
+ end
62
+
52
63
  def should_generate_slug?
53
- send(self.cached_slug_column).blank? || (self.sync_slugs && send(:"#{self.slug_source}_changed?"))
64
+ send(self.cached_slug_column).blank? || (self.sync_slugs && send(:"#{self.slug_source}_changed?")) && !should_convert_cached_slug?
54
65
  end
55
66
 
56
67
  def has_better_slug?
@@ -77,6 +88,10 @@ module Slugged
77
88
  has_slug_scope? ? where(slug_scope => record.send(slug_scope)) : scoped
78
89
  end
79
90
 
91
+ def slug_value_for(value)
92
+ value.present? ? self.slug_convertor_proc.call(value) : value
93
+ end
94
+
80
95
  protected
81
96
 
82
97
  def has_slug_scope?
@@ -92,6 +107,7 @@ module Slugged
92
107
  self.use_slug_cache = !!options.fetch(:use_cache, true)
93
108
  self.use_slug_to_param = !!options.fetch(:to_param, true)
94
109
  self.use_slug_history = !!options.fetch(:history, Slugged::Slug.usable?)
110
+ self.editable = !!options.fetch(:editable, false)
95
111
  end
96
112
 
97
113
  def set_slug_convertor(convertor)
@@ -107,7 +123,6 @@ module Slugged
107
123
  end
108
124
  end
109
125
  end
110
-
111
126
  end
112
127
  end
113
128
  end
@@ -1,8 +1,8 @@
1
1
  module Slugged
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 3
5
- PATCH = 3
4
+ MINOR = 4
5
+ PATCH = 0
6
6
  STRING = [MAJOR, MINOR, PATCH].join(".")
7
7
  end
8
8
  end
data/slugged.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{slugged}
8
- s.version = "0.3.3"
8
+ s.version = "0.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 = ["Darcy Laycock"]
12
- s.date = %q{2010-12-30}
12
+ s.date = %q{2011-01-01}
13
13
  s.description = %q{Super simple slugs for ActiveRecord 3.0 and higher, with support for slug history}
14
14
  s.email = %q{sutto@sutto.net}
15
15
  s.extra_rdoc_files = [
@@ -42,6 +42,7 @@ Gem::Specification.new do |s|
42
42
  "metrics/.gitignore",
43
43
  "slugged.gemspec",
44
44
  "test/caching_test.rb",
45
+ "test/editable_test.rb",
45
46
  "test/helper.rb",
46
47
  "test/is_sluggable_test.rb",
47
48
  "test/model_definitions.rb",
@@ -55,6 +56,7 @@ Gem::Specification.new do |s|
55
56
  s.summary = %q{Super simple slugs for ActiveRecord 3.0 and higher, with support for slug history}
56
57
  s.test_files = [
57
58
  "test/caching_test.rb",
59
+ "test/editable_test.rb",
58
60
  "test/helper.rb",
59
61
  "test/is_sluggable_test.rb",
60
62
  "test/model_definitions.rb",
@@ -0,0 +1,51 @@
1
+ require 'helper'
2
+ require 'digest/md5'
3
+
4
+ class EditableTest < Test::Unit::TestCase
5
+ with_tables :slugs, :users do
6
+ context 'with the editable option' do
7
+
8
+ setup { setup_slugs! :editable => true }
9
+
10
+ should 'correctly sluggify a value when cached_slug is blank on create' do
11
+ user = User.create(:name => "Bob")
12
+ assert_equal "bob", user.cached_slug
13
+ end
14
+
15
+ should 'not sluggify a value when cached_slug is not blank on create' do
16
+ user = User.create(:name => "Bob", :cached_slug => "Mob")
17
+ assert_equal "mob", user.cached_slug
18
+ end
19
+
20
+ should 'sluggify a value when cached_slug is not changed && name is changed on update' do
21
+ user = User.create(:name => "Bob")
22
+ user.update_attributes(:name => "Noob")
23
+ assert_equal "noob", user.cached_slug
24
+ end
25
+
26
+ should 'not sluggify a value when cached_slug is changed && name is changed on update' do
27
+ user = User.create(:name => "Bob")
28
+ user.update_attributes(:name => "Rob", :cached_slug => "noob")
29
+ assert_equal "noob", user.cached_slug
30
+ end
31
+
32
+ should 'sluggify a value when cached_slug set blank on update' do
33
+ user = User.create(:name => "Bob")
34
+ assert_equal "bob", user.cached_slug
35
+ user.update_attributes(:cached_slug => '')
36
+ assert_equal "bob", user.cached_slug
37
+ end
38
+
39
+ should 'sluggify a value when cached_slug set blank && name changed on update' do
40
+ user = User.create(:name => "Bob")
41
+ user.update_attributes(:name => "Rob", :cached_slug => '')
42
+ assert_equal "rob", user.cached_slug
43
+ end
44
+
45
+ should 'parameterize a value of a cached_slug' do
46
+ user = User.create(:name => "Bob", :cached_slug => "f**ck")
47
+ assert_equal user.cached_slug, User.slug_value_for("f**ck")
48
+ end
49
+ end
50
+ end
51
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slugged
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 3
10
- version: 0.3.3
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Darcy Laycock
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-30 00:00:00 +08:00
18
+ date: 2011-01-01 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -127,6 +127,7 @@ files:
127
127
  - metrics/.gitignore
128
128
  - slugged.gemspec
129
129
  - test/caching_test.rb
130
+ - test/editable_test.rb
130
131
  - test/helper.rb
131
132
  - test/is_sluggable_test.rb
132
133
  - test/model_definitions.rb
@@ -168,6 +169,7 @@ specification_version: 3
168
169
  summary: Super simple slugs for ActiveRecord 3.0 and higher, with support for slug history
169
170
  test_files:
170
171
  - test/caching_test.rb
172
+ - test/editable_test.rb
171
173
  - test/helper.rb
172
174
  - test/is_sluggable_test.rb
173
175
  - test/model_definitions.rb