acts_as_permalink 0.4.2 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 791fbabd504bcc2ae08b4ef1c5299188166dbd1b
4
- data.tar.gz: 85d87b94ae581e492e63a757c29ceb537a16e5ab
3
+ metadata.gz: 209c71d964e2e6ed7b91f5c76b5a2fbebe6eaba8
4
+ data.tar.gz: a95cc44e91a7845302db14f9d1fc5af160c9eb60
5
5
  SHA512:
6
- metadata.gz: 6e4306529165b0afcaebb21b3eecc5c6da3d0ce42989d36059c5c4f415283f2c5f6efd3fa8f80ff04cba749d47e18c2cef17543424069b0dbabcfe6354ae42d6
7
- data.tar.gz: 171c21e79bcb9fd46f1018580026c43635f51f1715520d2e9e424237b5596a9210d66bf27040935180dd9a4eadb16d77bf39a0e4c06733f16debe10d857ebaab
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.
@@ -1,5 +1,5 @@
1
1
  module Acts
2
2
  module Permalink
3
- VERSION = "0.4.2"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
@@ -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, :on => :create
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...obj.class.base_class.instance_variable_get('@permalink_length')] # trim to length
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.send("find_by_#{obj.class.base_class.instance_variable_get('@permalink_column_name')}", text)
55
- num = 1
56
-
57
- # If we find the object we know there is a collision, so just add a number to the end until there is no collision
58
- while obj.class.base_class.send("find_by_#{obj.class.base_class.instance_variable_get('@permalink_column_name')}", text + num.to_s)
59
- num += 1
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 = text + num.to_s
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_permalink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips