simple_slugs 0.0.5 → 0.0.7

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.
@@ -0,0 +1,15 @@
1
+ require 'active_record'
2
+
3
+ module SimpleSlugs
4
+ autoload :Slug, 'simple_slugs/slug'
5
+ autoload :Slugger, 'simple_slugs/slugger'
6
+ autoload :ActMacro, 'simple_slugs/act_macro'
7
+ end
8
+
9
+ class String
10
+ def to_slug
11
+ SimpleSlugs::Slug.new(self)
12
+ end
13
+ end
14
+
15
+ ActiveRecord::Base.extend(SimpleSlugs::ActMacro)
@@ -0,0 +1,12 @@
1
+ module SimpleSlugs
2
+ module ActMacro
3
+ def has_slug(options = {})
4
+ return if respond_to?(:slugger)
5
+
6
+ before_validation lambda { |record| record.slugger.unique_slug!(record) }
7
+
8
+ class_inheritable_accessor :slugger
9
+ self.slugger = Slugger.new(self, options)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,29 @@
1
+ module SimpleSlugs
2
+ class Slug < String
3
+ def initialize(string)
4
+ super
5
+ normalize
6
+ end
7
+
8
+ def normalize
9
+ transliterate
10
+ spacify
11
+ strip!
12
+ downcase!
13
+ dasherize
14
+ end
15
+
16
+ def transliterate
17
+ replace(I18n.transliterate(self))
18
+ end
19
+
20
+ def spacify
21
+ gsub!(/[\W_]/, ' ')
22
+ gsub!(/\s+/, ' ')
23
+ end
24
+
25
+ def dasherize
26
+ gsub!(' ', '-')
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,68 @@
1
+ require 'active_support/ordered_options'
2
+ require 'active_support/core_ext/hash/reverse_merge'
3
+
4
+ module SimpleSlugs
5
+ class Slugger < ActiveSupport::InheritableOptions
6
+ DEFAULTS = {
7
+ :slug_name => :slug,
8
+ :on_blank => true,
9
+ :scope => nil,
10
+ :separator => '-'
11
+ }
12
+
13
+ attr_reader :model
14
+
15
+ def initialize(model, options)
16
+ @model = model
17
+ super(options.reverse_merge(DEFAULTS))
18
+ end
19
+
20
+ def unique_slug!(record)
21
+ if record.send(slug_name).blank? || !on_blank
22
+ source = record.send(self.source)
23
+ record.slug = ensure_unique_slug(record, source.to_slug) if source
24
+ end
25
+ end
26
+
27
+ protected
28
+
29
+ def ensure_unique_slug(record, base)
30
+ slug, taken, n = base, self.taken_slugs(record, base), 0
31
+ slug = [base, separator, n += 1].join while taken.include?(slug)
32
+ slug
33
+ end
34
+
35
+ def taken_slugs(record, slug)
36
+ scope = translated? ? with_translated_slug(record, slug) : with_slug(record, slug)
37
+ scope.map(&:slug)
38
+ end
39
+
40
+ def slug_scope(record)
41
+ scope ? model.where(scope => record.send(scope)) : model.scoped
42
+ end
43
+
44
+ def with_slug(record, slug)
45
+ condition = model.arel_table[:slug].matches("#{slug}%")
46
+ slug_scope(record).where(condition)
47
+ end
48
+
49
+ def with_translated_slug(record, slug)
50
+ condition = model.translation_class.arel_table[:slug].matches("#{slug}%")
51
+ slug_scope(record) & model.with_translations.where(condition)
52
+ end
53
+
54
+ def source
55
+ @source ||= self[:source] || %w(name title).detect { |s| column_names.include?(s) }
56
+ end
57
+
58
+ def column_names
59
+ names = model.column_names
60
+ names = names + model.translation_class.column_names if model.respond_to?(:translation_class)
61
+ names
62
+ end
63
+
64
+ def translated?
65
+ model.respond_to?(:translated?) ? model.translated?(:slug) : false
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleSlugs
2
+ VERSION = "0.0.7"
3
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_slugs
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sven Fuchs
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-31 00:00:00 +02:00
18
+ date: 2010-09-02 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -124,8 +124,12 @@ extensions: []
124
124
 
125
125
  extra_rdoc_files: []
126
126
 
127
- files: []
128
-
127
+ files:
128
+ - lib/simple_slugs.rb
129
+ - lib/simple_slugs/act_macro.rb
130
+ - lib/simple_slugs/version.rb
131
+ - lib/simple_slugs/slug.rb
132
+ - lib/simple_slugs/slugger.rb
129
133
  has_rdoc: true
130
134
  homepage: http://github.com/svenfuchs/simple_slugs
131
135
  licenses: []