slug 4.1.0 → 4.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d86b48722b18d45cf288be1a320bff1550270cd61a0ab2c03dba7cbb67ab9ef
4
- data.tar.gz: 1ffb50fcca8eb72cbedb0f002104ce472fc35abf6aaf9635229605b14cb88580
3
+ metadata.gz: 65d6b738a1e306b5803f1b92e8328ed20632eb0d7706eb2417a198bd1acf8bfc
4
+ data.tar.gz: b1833f858513babd75d2f94ef776831fab3cc2e105ed9df33bf2912c9d7feb5c
5
5
  SHA512:
6
- metadata.gz: 49a6637967aae4804dfe30f1829bbd7cd3dd2d9075337e053d93a23489af88b894275f63787596243b01905eb27b90c7ca0a88ffe60146c780a7eead7aedd451
7
- data.tar.gz: 3693e899c785498624a302f92c3bedc6a9432c236c98f3eee924d9aba63c108191b3ab0decb44d1852ecb4093e34c187a5584d5bd082cc9125086c3fec51af8d
6
+ metadata.gz: 9b46a5b3226a1b4adbdf027fe9c7692d2a33431a5aa0b67ef99edd6297974008789407eee80cd614b8d65a5bfdcbadd6c2112c554e7d79e7198571ccee967a52
7
+ data.tar.gz: d9e07b0b4b55810ab4eaff7f828755bf0bdf0d5e6744cc8ba866e50e96642db707ee459d0c8b31288ef9978cf82677a386163a646e7eff570662587395996b02
data/README.md CHANGED
@@ -110,6 +110,10 @@ slug and move on.
110
110
  * If a slug already exists, Slug will automatically append a '-n' suffix to your slug to make it unique. The second instance of a slug is '-1'.
111
111
  * If you don't like the slug formatting or the accented character stripping doesn't work for you, it's easy to override Slug's formatting functions. Check the source for details.
112
112
 
113
+ ## Development
114
+
115
+ `rake test` will run the included unit tests.
116
+
113
117
  ## Authors
114
118
 
115
119
  Ben Koski, ben.koski@gmail.com
@@ -36,63 +36,66 @@ module Slug
36
36
  with: /\A[a-z0-9-]+\z/,
37
37
  message: "contains invalid characters. Only downcase letters, numbers, and '-' are allowed."
38
38
  before_validation :set_slug, :on => :create
39
+
40
+ include SlugInstanceMethods
39
41
  end
40
42
  end
41
43
 
42
- # Sets the slug. Called before create.
43
- # By default, set_slug won't change slug if one already exists. Pass :force => true to overwrite.
44
- def set_slug(opts={})
45
- validate_slug_columns
46
- return if self[self.slug_column].present? && !opts[:force]
44
+ module SlugInstanceMethods
45
+ # Sets the slug. Called before create.
46
+ # By default, set_slug won't change slug if one already exists. Pass :force => true to overwrite.
47
+ def set_slug(opts={})
48
+ validate_slug_columns
49
+ return if self[self.slug_column].present? && !opts[:force]
47
50
 
48
- self[self.slug_column] = normalize_slug(self.send(self.slug_source))
51
+ self[self.slug_column] = normalize_slug(self.send(self.slug_source))
49
52
 
50
- # if normalize_slug returned a blank string, try the generic_default handling
51
- if generic_default && self[self.slug_column].blank?
52
- self[self.slug_column] = self.class.to_s.demodulize.underscore.dasherize
53
- end
53
+ # if normalize_slug returned a blank string, try the generic_default handling
54
+ if generic_default && self[self.slug_column].blank?
55
+ self[self.slug_column] = self.class.to_s.demodulize.underscore.dasherize
56
+ end
54
57
 
55
- assign_slug_sequence if self.changed_attributes.include?(self.slug_column)
56
- end
58
+ assign_slug_sequence if self.changed_attributes.include?(self.slug_column)
59
+ end
57
60
 
58
- # Overwrite existing slug based on current contents of source column.
59
- def reset_slug
60
- set_slug(:force => true)
61
- end
61
+ # Overwrite existing slug based on current contents of source column.
62
+ def reset_slug
63
+ set_slug(:force => true)
64
+ end
62
65
 
63
- # Overrides to_param to return the model's slug.
64
- def to_param
65
- self[self.slug_column]
66
- end
66
+ # Overrides to_param to return the model's slug.
67
+ def to_param
68
+ self[self.slug_column]
69
+ end
67
70
 
68
- private
69
- # Validates that source and destination methods exist. Invoked at runtime to allow definition
70
- # of source/slug methods after <tt>slug</tt> setup in class.
71
- def validate_slug_columns
72
- raise ArgumentError, "Source column '#{self.slug_source}' does not exist!" if !self.respond_to?(self.slug_source)
73
- raise ArgumentError, "Slug column '#{self.slug_column}' does not exist!" if !self.respond_to?("#{self.slug_column}=")
74
- end
71
+ private
72
+ # Validates that source and destination methods exist. Invoked at runtime to allow definition
73
+ # of source/slug methods after <tt>slug</tt> setup in class.
74
+ def validate_slug_columns
75
+ raise ArgumentError, "Source column '#{self.slug_source}' does not exist!" if !self.respond_to?(self.slug_source)
76
+ raise ArgumentError, "Slug column '#{self.slug_column}' does not exist!" if !self.respond_to?("#{self.slug_column}=")
77
+ end
75
78
 
76
- # Takes the slug, downcases it and replaces non-word characters with a -.
77
- # Feel free to override this method if you'd like different slug formatting.
78
- def normalize_slug(str)
79
- return if str.blank?
80
- str.gsub!(/[\p{Pc}\p{Ps}\p{Pe}\p{Pi}\p{Pf}\p{Po}]/, '') # Remove punctuation
81
- str.parameterize
82
- end
79
+ # Takes the slug, downcases it and replaces non-word characters with a -.
80
+ # Feel free to override this method if you'd like different slug formatting.
81
+ def normalize_slug(str)
82
+ return if str.blank?
83
+ str.gsub!(/[\p{Pc}\p{Ps}\p{Pe}\p{Pi}\p{Pf}\p{Po}]/, '') # Remove punctuation
84
+ str.parameterize
85
+ end
83
86
 
84
- # If a slug of the same name already exists, this will append '-n' to the end of the slug to
85
- # make it unique. The second instance gets a '-1' suffix.
86
- def assign_slug_sequence
87
- return if self[self.slug_column].blank?
88
- assoc = self.class.base_class
89
- base_slug = self[self.slug_column]
90
- seq = 0
91
-
92
- while assoc.where(self.slug_column => self[self.slug_column]).exists? do
93
- seq += 1
94
- self[self.slug_column] = "#{base_slug}-#{seq}"
87
+ # If a slug of the same name already exists, this will append '-n' to the end of the slug to
88
+ # make it unique. The second instance gets a '-1' suffix.
89
+ def assign_slug_sequence
90
+ return if self[self.slug_column].blank?
91
+ assoc = self.class.base_class
92
+ base_slug = self[self.slug_column]
93
+ seq = 0
94
+
95
+ while assoc.where(self.slug_column => self[self.slug_column]).exists? do
96
+ seq += 1
97
+ self[self.slug_column] = "#{base_slug}-#{seq}"
98
+ end
95
99
  end
96
100
  end
97
-
98
- end
101
+ end
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "slug"
6
- s.version = "4.1.0"
6
+ s.version = "4.1.1"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
@@ -34,3 +34,6 @@ class Generation < ActiveRecord::Base
34
34
  slug :title, generic_default: true
35
35
  end
36
36
 
37
+ # Test model with no slug column
38
+ class Orphan < ActiveRecord::Base
39
+ end
@@ -32,4 +32,10 @@ ActiveRecord::Schema.define(:version => 1) do
32
32
  t.column "title", "string"
33
33
  t.column "slug", "string", null: false
34
34
  end
35
+
36
+ # table with no slug column
37
+ create_table "orphans", :force => true do |t|
38
+ t.column "name", "string"
39
+ t.column "location", "string"
40
+ end
35
41
  end
@@ -17,6 +17,17 @@ describe Slug do
17
17
  assert_equal 'test-event-portland', article.slug
18
18
  end
19
19
 
20
+ it "bases to_param on slug" do
21
+ article = Article.create!(:headline => 'Test Headline')
22
+ assert_equal(article.slug, article.to_param)
23
+ end
24
+
25
+ it "does not impact lookup of model with no slug column" do
26
+ orphan = Orphan.create!(:name => 'Oliver')
27
+ query = orphan.to_param
28
+ assert_equal(orphan.id.to_s, query)
29
+ end
30
+
20
31
  describe "slug column" do
21
32
  it "saves slug to 'slug' column by default" do
22
33
  article = Article.create!(:headline => 'Test Headline')
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: 4.1.0
4
+ version: 4.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Koski