i76-has_slug 0.1.1 → 0.1.2
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.
@@ -22,7 +22,8 @@ module HasSlug::SluggableInstanceMethods
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def new_slug_needed?
|
25
|
-
self.
|
25
|
+
!self.send("#{self.class.has_slug_options[:slug_column]}_changed?") &&
|
26
|
+
(self.new_record? || self.send("#{self.class.has_slug_options[:attribute]}_changed?"))
|
26
27
|
end
|
27
28
|
|
28
29
|
def to_param
|
data/test/unit/has_slug_test.rb
CHANGED
@@ -60,7 +60,24 @@ 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
|
+
end
|
65
|
+
|
66
|
+
context 'and a custom slug' do
|
67
|
+
setup do
|
68
|
+
@custom = Factory(:city, :name => 'Las Vegas', :slug => 'lv')
|
69
|
+
end
|
70
|
+
|
71
|
+
should 'not update the slug unless sluggable_column changed' do
|
72
|
+
@custom.save
|
73
|
+
assert_equal 'lv', @custom.slug
|
74
|
+
end
|
75
|
+
|
76
|
+
should 'update the slug if sluggable_column changed' do
|
77
|
+
@custom.update_attributes(:name => 'Los Angeles')
|
78
|
+
|
79
|
+
assert_equal 'los-angeles', @custom.slug
|
80
|
+
end
|
64
81
|
end
|
65
82
|
|
66
83
|
context 'and a scope' do
|
@@ -71,7 +88,7 @@ class HasSlugTest < Test::Unit::TestCase
|
|
71
88
|
|
72
89
|
should 'create the same slug in a different scope' do
|
73
90
|
@da_marco_2 = Factory(:restaurant, :name => 'Da Marco',
|
74
|
-
:city => @
|
91
|
+
:city => @san_Francisco)
|
75
92
|
|
76
93
|
assert_equal @da_marco_2.slug, @da_marco.slug
|
77
94
|
end
|
@@ -79,12 +96,12 @@ class HasSlugTest < Test::Unit::TestCase
|
|
79
96
|
|
80
97
|
should 'set the slug' do
|
81
98
|
assert_equal 'new-york', @new_york.slug
|
82
|
-
assert_equal 'san-
|
99
|
+
assert_equal 'san-francisco', @san_Francisco.slug
|
83
100
|
end
|
84
101
|
|
85
102
|
should 'return the slug on call to #to_param' do
|
86
103
|
assert_equal @new_york.slug, @new_york.to_param
|
87
|
-
assert_equal @
|
104
|
+
assert_equal @san_Francisco.slug, @san_Francisco.to_param
|
88
105
|
end
|
89
106
|
|
90
107
|
should 'not create duplicate slugs' do
|
@@ -108,21 +125,21 @@ class HasSlugTest < Test::Unit::TestCase
|
|
108
125
|
end
|
109
126
|
|
110
127
|
should 'still find some by id' do
|
111
|
-
cities = City.find([@new_york.id, @
|
128
|
+
cities = City.find([@new_york.id, @san_Francisco.id])
|
112
129
|
|
113
130
|
assert_equal 2, cities.length
|
114
131
|
assert !cities.any?(&:found_by_slug?)
|
115
132
|
end
|
116
133
|
|
117
134
|
should 'find some by slug' do
|
118
|
-
cities = City.find([@new_york.slug, @
|
135
|
+
cities = City.find([@new_york.slug, @san_Francisco.slug])
|
119
136
|
|
120
137
|
assert_equal 2, cities.length
|
121
138
|
assert cities.all?(&:found_by_slug?)
|
122
139
|
end
|
123
140
|
|
124
141
|
should 'find some by id or slug' do
|
125
|
-
cities = City.find([@new_york.id, @
|
142
|
+
cities = City.find([@new_york.id, @san_Francisco.slug])
|
126
143
|
|
127
144
|
assert_equal 2, cities.length
|
128
145
|
assert !cities[0].found_by_slug?
|