acts_as_permalink 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +5 -0
- data/lib/acts_as_permalink/version.rb +1 -1
- data/lib/acts_as_permalink.rb +24 -22
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 209c71d964e2e6ed7b91f5c76b5a2fbebe6eaba8
|
4
|
+
data.tar.gz: a95cc44e91a7845302db14f9d1fc5af160c9eb60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01a035c4489b03e36a9191dc369908ce5c2fc8b100fd3a7d511d6c447abfbfd7a844efbbe0d29680d577ed4d4814af295af082900bf04ff49bff89ab41dc19f8
|
7
|
+
data.tar.gz: a8ad3501d84e3262737e657050e73d2255e207e0c08a5af36ca1513c1fe99c4247b582d28c74c3b294f84950610b4bcf934e945c0a436d78ffe7fd53a41908d0
|
data/README.markdown
CHANGED
@@ -67,6 +67,11 @@ $ bundle exec rspec
|
|
67
67
|
|
68
68
|
## Changelog
|
69
69
|
|
70
|
+
* 0.5.0 -- Fix bugs in `max_length` property which would sometimes allow the permalink to be longer than the value
|
71
|
+
Use `where().first` over `send("find_by_#{ column }")`
|
72
|
+
|
73
|
+
* 0.4.2 -- Update rspec to new expect() syntax
|
74
|
+
|
70
75
|
* 0.4.1 -- Documentation improvements.
|
71
76
|
|
72
77
|
* 0.4.0 -- Rails 4 support.
|
data/lib/acts_as_permalink.rb
CHANGED
@@ -8,10 +8,10 @@ module Acts #:nodoc:
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def acts_as_permalink(options={})
|
11
|
-
# Read and scrub option for the column which will save the permalink
|
11
|
+
# Read and scrub option for the column which will save the permalink
|
12
12
|
self.base_class.instance_variable_set('@permalink_column_name', options[:to].try(:to_sym) || :permalink)
|
13
13
|
|
14
|
-
# Read and scrub the option for the column or function which will generate the permalink
|
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
17
|
# Read and validate the maximum length of the permalink
|
@@ -20,24 +20,24 @@ module Acts #:nodoc:
|
|
20
20
|
self.base_class.instance_variable_set('@permalink_length', max_length)
|
21
21
|
|
22
22
|
if Rails.version >= "3"
|
23
|
-
before_validation :update_permalink, :
|
23
|
+
before_validation :update_permalink, on: :create
|
24
24
|
else
|
25
25
|
before_validation_on_create :update_permalink
|
26
26
|
end
|
27
27
|
|
28
28
|
validates_uniqueness_of @permalink_column_name
|
29
29
|
attr_readonly @permalink_column_name
|
30
|
-
|
30
|
+
|
31
31
|
include Acts::Permalink::InstanceMethods
|
32
32
|
extend Acts::Permalink::SingletonMethods
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
# Returns the unique permalink string for the passed in object.
|
36
36
|
def generate_permalink_for(obj)
|
37
|
-
|
38
|
-
# Find the source for the permalink
|
37
|
+
column_name = obj.class.base_class.instance_variable_get('@permalink_column_name')
|
39
38
|
text = obj.send(obj.class.base_class.instance_variable_get('@permalink_source'))
|
40
|
-
|
39
|
+
max_length = obj.class.base_class.instance_variable_get('@permalink_length')
|
40
|
+
|
41
41
|
# If it is blank then generate a random link
|
42
42
|
if text.blank?
|
43
43
|
text = obj.class.base_class.to_s.downcase + rand(10000).to_s
|
@@ -47,37 +47,39 @@ module Acts #:nodoc:
|
|
47
47
|
text = text.downcase.strip # make the string lowercase and scrub white space on either side
|
48
48
|
text = text.gsub(/[^a-z0-9\w]/, "_") # make any character that is not nupermic or alphabetic into an underscore
|
49
49
|
text = text.sub(/_+$/, "").sub(/^_+/, "") # remove underscores on either end, caused by non-simplified characters
|
50
|
-
text = text[0...
|
50
|
+
text = text[0...max_length] # trim to length
|
51
51
|
end
|
52
|
-
|
53
|
-
# Attempt to find the object by the permalink
|
54
|
-
if obj.class.base_class.
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
52
|
+
|
53
|
+
# Attempt to find the object by the permalink, and if so there is a collision and we need to de-collision it
|
54
|
+
if obj.class.base_class.where(column_name => text).first
|
55
|
+
candidate_text = nil
|
56
|
+
|
57
|
+
(1..999999).each do |num|
|
58
|
+
suffix = "-#{ num }"
|
59
|
+
candidate_text = [text[0...(max_length - suffix.length)], suffix].join("")
|
60
|
+
break unless obj.class.base_class.where(column_name => candidate_text).first
|
60
61
|
end
|
61
62
|
|
62
|
-
text =
|
63
|
+
text = candidate_text
|
63
64
|
end
|
65
|
+
|
64
66
|
text
|
65
67
|
end
|
66
68
|
end
|
67
|
-
|
69
|
+
|
68
70
|
module SingletonMethods
|
69
71
|
end
|
70
|
-
|
72
|
+
|
71
73
|
module InstanceMethods
|
72
74
|
|
73
75
|
# Override this method so that find searches by permalink and not by id
|
74
76
|
def to_param
|
75
77
|
self.send(self.class.base_class.instance_variable_get('@permalink_column_name'))
|
76
78
|
end
|
77
|
-
|
79
|
+
|
78
80
|
# Generate the permalink and assign it directly via callback
|
79
81
|
def update_permalink
|
80
|
-
self.send("#{self.class.base_class.instance_variable_get('@permalink_column_name')}=", self.class.base_class.generate_permalink_for(self))
|
82
|
+
self.send("#{ self.class.base_class.instance_variable_get('@permalink_column_name') }=", self.class.base_class.generate_permalink_for(self))
|
81
83
|
true
|
82
84
|
end
|
83
85
|
end
|