acts_as_permalink 0.5.0 → 0.6.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.
- checksums.yaml +4 -4
- data/README.markdown +3 -0
- data/lib/acts_as_permalink.rb +9 -3
- data/lib/acts_as_permalink/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4633aa91665c5b810b32f1c2e748674d74c5d66c
|
4
|
+
data.tar.gz: e06d13bcdb3f1a032b71ff1659b391eb5dedb945
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 663b5f0f6df7dd2527a32b17f090118a0213c8c280db92bc5abfd5c2cce23b06bd1fe73fdf49c830135775117369509cf727710cfea9e09b39cb2f793921f0e7
|
7
|
+
data.tar.gz: 20c6178a41efd0379dff74a4f3151f7116e06593cb9ada693253ef0c91b4a0589431cc4d5ed4f7ee37b8ea3d9c05b6422df553e3fb7c627187672ce1227165c2
|
data/README.markdown
CHANGED
@@ -44,6 +44,7 @@ The `title` and `permalink` fields can be overridden with the following options:
|
|
44
44
|
from: :title # Name of the active record column or function used to generate the permalink
|
45
45
|
to: :permalink # Name of the column where the permalink will be stored
|
46
46
|
max_length: 60 # Maximum number of characters the permalink will be
|
47
|
+
underscore: false # Prefer using the `_` character as a replacement over the default `-`
|
47
48
|
|
48
49
|
So, for example you have want to store your permalink in a column called `path_name` and you want to generate your permalink using first and last name, and you want to restrict it to 40 characters, your model would look like:
|
49
50
|
|
@@ -67,6 +68,8 @@ $ bundle exec rspec
|
|
67
68
|
|
68
69
|
## Changelog
|
69
70
|
|
71
|
+
* 0.6.0 -- Switch default replacement character to a dash, but allow the `underscore: true` property to go back to the old format
|
72
|
+
|
70
73
|
* 0.5.0 -- Fix bugs in `max_length` property which would sometimes allow the permalink to be longer than the value
|
71
74
|
Use `where().first` over `send("find_by_#{ column }")`
|
72
75
|
|
data/lib/acts_as_permalink.rb
CHANGED
@@ -14,6 +14,9 @@ module Acts #:nodoc:
|
|
14
14
|
# Read and scrub the option for the column or function which will generate the permalink
|
15
15
|
self.base_class.instance_variable_set('@permalink_source', (options[:from].try(:to_sym) || :title))
|
16
16
|
|
17
|
+
# Set the underscore character
|
18
|
+
self.base_class.instance_variable_set('@permalink_underscore', options[:underscore])
|
19
|
+
|
17
20
|
# Read and validate the maximum length of the permalink
|
18
21
|
max_length = options[:max_length].to_i rescue 0
|
19
22
|
max_length = 60 unless max_length && max_length > 0
|
@@ -37,6 +40,7 @@ module Acts #:nodoc:
|
|
37
40
|
column_name = obj.class.base_class.instance_variable_get('@permalink_column_name')
|
38
41
|
text = obj.send(obj.class.base_class.instance_variable_get('@permalink_source'))
|
39
42
|
max_length = obj.class.base_class.instance_variable_get('@permalink_length')
|
43
|
+
separator = obj.class.base_class.instance_variable_get('@permalink_underscore') ? "_" : "-"
|
40
44
|
|
41
45
|
# If it is blank then generate a random link
|
42
46
|
if text.blank?
|
@@ -45,9 +49,11 @@ module Acts #:nodoc:
|
|
45
49
|
# If it is not blank, scrub
|
46
50
|
else
|
47
51
|
text = text.downcase.strip # make the string lowercase and scrub white space on either side
|
48
|
-
text = text.gsub(/[^a-z0-9\w]/,
|
49
|
-
text = text.
|
52
|
+
text = text.gsub(/[^a-z0-9\w]/, separator) # make any character that is not nupermic or alphabetic into a special character
|
53
|
+
text = text.squeeze(separator) # removes any consecutive duplicates of the special character
|
50
54
|
text = text[0...max_length] # trim to length
|
55
|
+
text = text.sub(Regexp.new("^#{ separator }+"), "") # remove leading special characters
|
56
|
+
text = text.sub(Regexp.new("#{ separator }+$"), "") # remove trailing special characters
|
51
57
|
end
|
52
58
|
|
53
59
|
# Attempt to find the object by the permalink, and if so there is a collision and we need to de-collision it
|
@@ -55,7 +61,7 @@ module Acts #:nodoc:
|
|
55
61
|
candidate_text = nil
|
56
62
|
|
57
63
|
(1..999999).each do |num|
|
58
|
-
suffix = "
|
64
|
+
suffix = "#{ separator }#{ num }"
|
59
65
|
candidate_text = [text[0...(max_length - suffix.length)], suffix].join("")
|
60
66
|
break unless obj.class.base_class.where(column_name => candidate_text).first
|
61
67
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_permalink
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin McPhillips
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|