Module: WulffeldSlug::SlugInclude

Defined in:
lib/wulffeld_slug/slug_include.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/wulffeld_slug/slug_include.rb', line 3

def self.included(base)
  base.class_eval do
    if self.slug_config[:mongo]
      field :slug
    end
      
    validates_format_of :slug, :with => /\A[[:alnum:]-]+\Z/, :allow_blank => false

    before_validation :on => :create do
      cb_make_slug
    end
  end
end

Instance Method Details

#cb_make_slugObject



34
35
36
37
38
39
40
41
42
# File 'lib/wulffeld_slug/slug_include.rb', line 34

def cb_make_slug
  # NOTE: Uniqueness is not checked if slug is non-nil. You must ensure uniqueness yourself then.
  return unless slug.blank?

  a = [*send(slug_config[:fields])].reject {|f| f.blank? }.map {|f| f.is_a?(String) ? f : send(f) }.flatten
  return if a.blank?

  self.slug = to_unique_slug(a.join('-'))
end

#prepare_string(s, max = 239) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wulffeld_slug/slug_include.rb', line 76

def prepare_string(s, max=239)
  # Blow away apostrophes
  s.gsub! /['`]/,""

  # @ --> at, and & --> and
  s.gsub! /\s*@\s*/, " at "
  s.gsub! /\s*&\s*/, " and "

  s = Babosa::Identifier.new(s).transliterate.to_ascii.to_s
  s.strip!

  # Convert spaces to dashes.
  s.gsub!(/[^a-zA-Z0-9]+/i, '-')

  words = s.split('-').reject {|w| w.blank? }

  new_slug =
    case slug_config[:case]
    when :preserve
      words
    when :downcase
      words.map(&:downcase)
    when :capitalize
      words.map(&:capitalize)
    when :upcase
      words.map(&:upcase)
    end.join('-')
  
  new_slug = new_slug[0..max]
end

#slug_unique?(slug) ⇒ Boolean

You may override this.

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wulffeld_slug/slug_include.rb', line 22

def slug_unique?(slug)
  if new_record?
    !self.class.where(:slug => slug).first
  else
    if self.slug_config[:mongo]
      !self.class.where(:slug => slug).and(:_id.ne => self.id).first
    else
      !self.class.where(["slug = ? AND id <> ?", slug, self.id]).first
    end
  end
end

#to_paramObject



17
18
19
# File 'lib/wulffeld_slug/slug_include.rb', line 17

def to_param
  slug
end

#to_unique_slug(s, n = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wulffeld_slug/slug_include.rb', line 44

def to_unique_slug(s, n=nil)
  str_part = prepare_string(s)
  str_part = self.id.to_s if str_part.blank? && !self.new_record?

  loop do
    slug = [str_part, n].reject {|p| p.blank? }.join('-')
    return slug if !slug.blank? && slug_unique?(slug)

    # First loop we do some optimistic searching.
    if n.nil?
      b = slug_config[:loop_start]
      last_b = nil
      loop do
        slug = [str_part, b].join('-')
        if slug_unique?(slug) || b < 1
          last_b = b
          break if b < 1
        else
          n = last_b
          break
        end
        
        b /= 2
      end
    end
    n ||= 0
    n += 1
  end
end