i76-has_slug 0.1.2 → 0.1.3
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/lib/has_slug.rb
CHANGED
@@ -46,7 +46,8 @@ module HasSlug
|
|
46
46
|
extend SluggableClassMethods
|
47
47
|
include SluggableInstanceMethods
|
48
48
|
|
49
|
-
before_save :set_slug,
|
49
|
+
before_save :set_slug,
|
50
|
+
:if => :new_slug_needed?
|
50
51
|
else
|
51
52
|
require 'has_slug/not_sluggable_class_methods'
|
52
53
|
require 'has_slug/not_sluggable_instance_methods'
|
@@ -7,6 +7,22 @@ module HasSlug::SluggableClassMethods
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
def slug_scope_attribute
|
11
|
+
return @@slug_scope_attribute if defined?(@@slug_scope_attribute)
|
12
|
+
|
13
|
+
if scope = has_slug_options[:scope]
|
14
|
+
if columns.any? { |c| c.name == scope }
|
15
|
+
@@slug_scope_attribute = scope.to_sym
|
16
|
+
elsif columns.any? { |c| c.name == "#{scope}_id" }
|
17
|
+
@@slug_scope_attribute = "#{scope}_id".to_sym
|
18
|
+
else
|
19
|
+
raise Exception, "has_slug's scope '#{scope}' does not exist in the model '#{self.class.to_s}'"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
@@slug_scope_attribute
|
24
|
+
end
|
25
|
+
|
10
26
|
# Find a single record that has the same slug as the given record's slug
|
11
27
|
def find_one_with_same_slug(object)
|
12
28
|
slug_column = has_slug_options[:slug_column]
|
@@ -16,11 +32,8 @@ module HasSlug::SluggableClassMethods
|
|
16
32
|
end
|
17
33
|
|
18
34
|
if scope = has_slug_options[:scope]
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
result = send("find_by_#{slug_column}_and_#{scope_attribute}",
|
23
|
-
object.slug, object.send(scope), options)
|
35
|
+
result = send("find_by_#{slug_column}_and_#{slug_scope_attribute}",
|
36
|
+
object.slug, object.send(scope), options)
|
24
37
|
else
|
25
38
|
result = send("find_by_#{slug_column}", object.slug, options)
|
26
39
|
end
|
@@ -22,8 +22,15 @@ module HasSlug::SluggableInstanceMethods
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def new_slug_needed?
|
25
|
-
|
26
|
-
|
25
|
+
slug_changed = self.send("#{self.class.has_slug_options[:slug_column]}_changed?")
|
26
|
+
sluggable_changed = self.send("#{self.class.has_slug_options[:attribute]}_changed?")
|
27
|
+
|
28
|
+
scope_changed = if self.class.has_slug_options[:scope] then self.send("#{self.class.slug_scope_attribute}_changed?")
|
29
|
+
else false
|
30
|
+
end
|
31
|
+
|
32
|
+
(!slug_changed && (self.new_record? || sluggable_changed)) || scope_changed
|
33
|
+
|
27
34
|
end
|
28
35
|
|
29
36
|
def to_param
|
data/test/unit/has_slug_test.rb
CHANGED
@@ -60,7 +60,7 @@ class HasSlugTest < Test::Unit::TestCase
|
|
60
60
|
context 'with a slug column' do
|
61
61
|
setup do
|
62
62
|
@new_york = Factory(:city, :name => 'New York')
|
63
|
-
@
|
63
|
+
@san_francisco = Factory(:city, :name => 'San Francisco')
|
64
64
|
end
|
65
65
|
|
66
66
|
context 'and a custom slug' do
|
@@ -88,20 +88,29 @@ class HasSlugTest < Test::Unit::TestCase
|
|
88
88
|
|
89
89
|
should 'create the same slug in a different scope' do
|
90
90
|
@da_marco_2 = Factory(:restaurant, :name => 'Da Marco',
|
91
|
-
:city => @
|
91
|
+
:city => @san_francisco)
|
92
92
|
|
93
93
|
assert_equal @da_marco_2.slug, @da_marco.slug
|
94
94
|
end
|
95
|
+
|
96
|
+
should 'not create duplicate slugs' do
|
97
|
+
@da_marco_2 = Factory(:restaurant, :name => 'Da Marco',
|
98
|
+
:city => @new_york)
|
99
|
+
assert_not_equal @da_marco_2.slug, @da_marco.slug
|
100
|
+
|
101
|
+
@da_marco_2.update_attributes(:city => @san_fransisco)
|
102
|
+
assert_equal @da_marco_2.slug, @da_marco.slug
|
103
|
+
end
|
95
104
|
end
|
96
105
|
|
97
106
|
should 'set the slug' do
|
98
107
|
assert_equal 'new-york', @new_york.slug
|
99
|
-
assert_equal 'san-francisco', @
|
108
|
+
assert_equal 'san-francisco', @san_francisco.slug
|
100
109
|
end
|
101
110
|
|
102
111
|
should 'return the slug on call to #to_param' do
|
103
112
|
assert_equal @new_york.slug, @new_york.to_param
|
104
|
-
assert_equal @
|
113
|
+
assert_equal @san_francisco.slug, @san_francisco.to_param
|
105
114
|
end
|
106
115
|
|
107
116
|
should 'not create duplicate slugs' do
|
@@ -125,21 +134,21 @@ class HasSlugTest < Test::Unit::TestCase
|
|
125
134
|
end
|
126
135
|
|
127
136
|
should 'still find some by id' do
|
128
|
-
cities = City.find([@new_york.id, @
|
137
|
+
cities = City.find([@new_york.id, @san_francisco.id])
|
129
138
|
|
130
139
|
assert_equal 2, cities.length
|
131
140
|
assert !cities.any?(&:found_by_slug?)
|
132
141
|
end
|
133
142
|
|
134
143
|
should 'find some by slug' do
|
135
|
-
cities = City.find([@new_york.slug, @
|
144
|
+
cities = City.find([@new_york.slug, @san_francisco.slug])
|
136
145
|
|
137
146
|
assert_equal 2, cities.length
|
138
147
|
assert cities.all?(&:found_by_slug?)
|
139
148
|
end
|
140
149
|
|
141
150
|
should 'find some by id or slug' do
|
142
|
-
cities = City.find([@new_york.id, @
|
151
|
+
cities = City.find([@new_york.id, @san_francisco.slug])
|
143
152
|
|
144
153
|
assert_equal 2, cities.length
|
145
154
|
assert !cities[0].found_by_slug?
|