has_permalink 0.0.8 → 0.1.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.
data/lib/friendly_url.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module FriendlyUrl
3
- $KCODE = "U"
3
+ $KCODE = "U" unless RUBY_VERSION > "1.9.0"
4
4
  def normalize(str)
5
5
  unless str.blank?
6
6
  n = str.mb_chars.downcase.to_s.strip
data/lib/has_permalink.rb CHANGED
@@ -3,19 +3,27 @@ require 'friendly_url'
3
3
  module HasPermalink
4
4
  require 'railtie' if defined?(Rails) && Rails.version >= "3"
5
5
 
6
- def has_permalink(generate_from = :title)
6
+ def has_permalink(generate_from = :title, auto_fix_duplication = false)
7
7
  unless included_modules.include? InstanceMethods
8
- class_inheritable_accessor :generate_from
8
+ class_attribute :generate_from
9
+ class_attribute :auto_fix_duplication
9
10
  extend ClassMethods
10
11
  include InstanceMethods
11
12
  end
12
13
 
13
- self.generate_from = generate_from
14
+ self.generate_from = generate_from
15
+ self.auto_fix_duplication = auto_fix_duplication
14
16
  before_validation :generate_permalink
15
17
  end
16
18
 
17
19
  module ClassMethods
18
20
 
21
+ # find(params[:id]) is quering the permalink field if it's a string
22
+ #
23
+ def find(*args)
24
+ find_by_permalink!(args)
25
+ end
26
+
19
27
  # Makes it possible to generate permalinks for
20
28
  # all instances of self:
21
29
  #
@@ -34,19 +42,40 @@ module HasPermalink
34
42
 
35
43
  # Generate permalink for the instance if the permalink is empty or nil.
36
44
  def generate_permalink
37
- self.permalink = normalize(self.send(generate_from)) if permalink.blank?
45
+ self.permalink = fix_duplication(normalize(self.send(generate_from))) if permalink.blank?
38
46
  end
39
47
 
40
48
  # Generate permalink for the instance and overwrite any existing value.
41
49
  def generate_permalink!
42
- self.permalink = normalize(self.send(generate_from))
50
+ self.permalink = fix_duplication(normalize(self.send(generate_from)))
43
51
  end
44
52
 
45
53
  # Override to send permalink as params[:id]
46
54
  def to_param
47
55
  permalink
48
56
  end
57
+
58
+ private
59
+
60
+ # Autofix duplication of permalinks
61
+ def fix_duplication(permalink)
62
+ if auto_fix_duplication
63
+ n = self.class.where(:permalink => permalink).length
64
+
65
+ if n > 0
66
+ resolve_duplication(permalink, n)
67
+ else
68
+ permalink
69
+ end
70
+ else
71
+ permalink
72
+ end
73
+ end
74
+
75
+ def resolve_duplication(permalink, number)
76
+ "#{permalink}-#{number}"
77
+ end
49
78
  end
50
79
  end
51
80
 
52
- ActiveRecord::Base.send :extend, HasPermalink
81
+ ActiveRecord::Base.send :extend, HasPermalink
@@ -1,6 +1,6 @@
1
1
  namespace :has_permalink do
2
2
  desc 'Generate permalinks for [MODEL]'
3
- task :generate_permalinks, :model_name, :needs => :environment do |t, args|
3
+ task :generate_permalinks, [:model_name] => [:environment] do |t, args|
4
4
  begin
5
5
  model_name = args.first[1]
6
6
  rescue
@@ -22,4 +22,4 @@ def generate_permalinks(model_name)
22
22
  rescue
23
23
  puts "Can't find model '#{model_name}'. Does it exist?"
24
24
  end
25
- end
25
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_permalink
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 8
10
- version: 0.0.8
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ola Karlsson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-12 00:00:00 +01:00
18
+ date: 2011-06-20 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  requirements: []
67
67
 
68
68
  rubyforge_project:
69
- rubygems_version: 1.6.2
69
+ rubygems_version: 1.4.2
70
70
  signing_key:
71
71
  specification_version: 3
72
72
  summary: SEO Permalink Plugin for Ruby on Rails.