pretty_slugs 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Guardfile CHANGED
@@ -6,6 +6,7 @@ guard 'minitest' do
6
6
  watch(%r|^test/(.*)\/?test_(.*)\.rb|)
7
7
  watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
8
8
  watch(%r|^test/test_helper\.rb|) { "test" }
9
+ watch(%r|^lib/pretty_slugs.rb|) { "test/lib/pretty_slugs/test_pretty_slugs.rb" }
9
10
 
10
11
  # with Minitest::Spec
11
12
  # watch(%r|^spec/(.*)_spec\.rb|)
data/lib/pretty_slugs.rb CHANGED
@@ -54,16 +54,31 @@ module PrettySlugs
54
54
  def slug=(val)
55
55
  # the function of to_slug(val) means that if I try to store a
56
56
  # custom slug, it will make sure it is slug-worthy, lowercase, underscored, etc...
57
- @slugstorage = to_slug(val) and return if self.new_record?
57
+
58
+ if self.new_record? && val != '' && !val.nil?
59
+ @slugstorage = to_slug(val) and return
60
+ end
61
+
62
+ if matches_existing_slug?(to_slug(val))
63
+ val = nil
64
+ end
58
65
  check_slug_existence
59
66
  if val != nil && val != ""
60
67
  slug = val
61
- Slug.find_by_sluggable_id_and_sluggable_class(self.id, self.class.to_s).update_attribute(:slug, val)
68
+ slug_record.update_attribute(:slug, val)
62
69
  update_menu_elements(val)
63
70
  else
64
71
  # do nothing
65
72
  end
66
73
  end
74
+
75
+ def slug_record
76
+ Slug.find_by_sluggable_id_and_sluggable_class(self.id, self.class.to_s)
77
+ end
78
+
79
+ def matches_existing_slug?(val=nil)
80
+ !Slug.find_by_slug_and_sluggable_class(val, self.class.to_s).nil?
81
+ end
67
82
 
68
83
  def check_slug_existence
69
84
  if self.slug.nil?
@@ -73,30 +88,34 @@ module PrettySlugs
73
88
 
74
89
  def generate_slug
75
90
  inc = 1
76
- slug = self.to_slug
77
- while !Slug.find_by_slug_and_sluggable_class(slug, self.class.to_s).nil?
78
- slug += "-#{inc}"
91
+ s_slug = self.to_slug
92
+ while matches_existing_slug?(s_slug)
93
+ s_slug += "-#{inc}"
79
94
  inc += 1
80
95
  end
81
96
 
82
97
  obj = Slug.create({
83
- slug: slug,
98
+ slug: s_slug,
84
99
  sluggable_id: self.id,
85
100
  sluggable_class: self.class.to_s
86
101
  })
87
-
88
- update_menu_elements(slug)
89
- slug
102
+ @slugstorage = nil
103
+ update_menu_elements(s_slug)
104
+ s_slug
90
105
  end
91
106
 
92
107
  def remove_slug
93
- Slug.find_by_sluggable_id_and_sluggable_class(self.id, self.class.to_s).destroy rescue true
108
+ slug_record.destroy rescue true
94
109
  end
95
110
 
96
111
  def update_slug
97
- Slug.find_by_sluggable_id_and_sluggable_class(self.id, self.class.to_s).update_attribute(slug: self.slug)
112
+ begin
113
+ slug_record.update_attribute(slug: self.slug)
114
+ rescue
115
+ generate_slug
116
+ end
98
117
  end
99
-
118
+
100
119
  def update_menu_elements(url=nil)
101
120
  if self.respond_to?("menu_elements") && self.class.to_s == "Page"
102
121
  self.menu_elements.each{|me| me.update_attribute(:url, ("/pages/" + (url.nil? || url.empty? ? self.slug : url)))}
@@ -104,7 +123,7 @@ module PrettySlugs
104
123
  end
105
124
 
106
125
  def to_slug(val=nil)
107
- return @slugstorage if @slugstorage
126
+ return @slugstorage if @slugstorage
108
127
  word = self[self.respond_to?("name") ? "name" : "title"] rescue self["id"]
109
128
  word = self["id"].to_s if word.nil?
110
129
 
@@ -1,3 +1,3 @@
1
1
  module PrettySlugs
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -88,4 +88,27 @@ describe PrettySlugs do
88
88
  @jont.slug.must_equal "jon-1"
89
89
  end
90
90
 
91
+
92
+ it "must not create Blank Slug entries" do
93
+ @rocky = Page.create(:name => "Rocky and Bulwinkle", :slug => '')
94
+ PrettySlugs::Slug.first.slug.must_equal 'rocky_and_bulwinkle'
95
+ end
96
+
97
+ it "must not allow duplicate slugs even after explicitly assigning the same slug" do
98
+ @g = Page.create(:name => "About")
99
+ @h = Page.create(:name => "About Us", :slug => "about")
100
+ PrettySlugs::Slug.first.slug.must_equal("about")
101
+ PrettySlugs::Slug.last.slug.must_equal("about-1")
102
+ end
103
+
104
+ it "must not allow duplicate slugs even after explicitly editing and assigning the same slug" do
105
+ @g = Page.create(:name => "About")
106
+ @h = Page.create(:name => "About")
107
+ PrettySlugs::Slug.first.slug.must_equal("about")
108
+ PrettySlugs::Slug.last.slug.must_equal("about-1")
109
+ @h.slug = "about"
110
+ @h.save
111
+ PrettySlugs::Slug.last.slug.must_equal("about-1")
112
+ end
113
+
91
114
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_slugs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-04 00:00:00.000000000 Z
13
+ date: 2013-03-06 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Pretty Slugs for Rails models
16
16
  email: