rails_slugs 1.0.8 → 1.0.9

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.
@@ -1,16 +1,60 @@
1
1
  module RailsSlugs
2
2
  module ActiveRecord
3
3
  module Base
4
- module NonSluggable
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ def to_param
10
+ self.class.sluggable? ? slug : super
11
+ end
12
+
13
+ protected
14
+
15
+ def assign_slug
16
+ if slug.nil? or not slug_changed?
17
+ options = self.class.slug
18
+ case options
19
+ when Symbol
20
+ value = send(options)
21
+ when Array
22
+ value = options.each.map{|p|send(p)}.join(' ')
23
+ when Proc
24
+ value = options.call(self)
25
+ end
26
+ if value.present?
27
+ value = value.parameterize
28
+ previous_value = previous_slug?(value)
29
+ if previous_value != false
30
+ if previous_value.present?
31
+ index = Regexp.new(value + '-(\d+)$').match(previous_value)
32
+ if index.present?
33
+ value << "-#{index[1].to_i + 1}"
34
+ else
35
+ value << '-1'
36
+ end
37
+ end
38
+ self.slug = value
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ module ClassMethods
45
+
46
+ def inherited(subclass)
47
+ subclass.instance_variable_set(:@slug, @slug)
48
+ super
49
+ end
5
50
 
6
51
  def sluggable?
7
- not defined?(@slug).nil?
52
+ @slug.present?
8
53
  end
9
54
 
10
55
  def slug(*args)
11
56
  if args.any?
12
57
  unless sluggable?
13
- include RailsSlugs::ActiveRecord::Base::Sluggable
14
58
  if respond_to? :translatable? and translatable?
15
59
  include RailsSlugs::ActiveRecord::Base::Translatable
16
60
  attr_translatable :slug
@@ -23,93 +67,42 @@ module RailsSlugs
23
67
  end
24
68
  end
25
69
  @slug
26
- end
27
-
28
- end
29
- module Sluggable
30
-
31
- def to_param
32
- slug
33
70
  end
34
-
35
- protected
36
-
37
- def assign_slug
38
- if slug.nil? or not slug_changed?
39
- options = self.class.slug
40
- case options
41
- when Symbol
42
- value = send(options)
43
- when Array
44
- value = options.each.map{|p|send(p)}.join(' ')
45
- when Proc
46
- value = options.call(self)
47
- end
48
- if value.present?
49
- value = value.parameterize
50
- previous_value = previous_slug?(value)
51
- if previous_value.present?
52
- index = Regexp.new(value + '-(\d+)$').match(previous_value)
53
- if index.present?
54
- value << "-#{index[1].to_i + 1}"
55
- else
56
- value << '-1'
57
- end
58
- end
59
- self.slug = value
60
- end
61
- end
62
- end
63
-
71
+
64
72
  end
65
73
  module NonTranslatable
66
74
 
67
75
  def self.included(base)
68
- base.instance_eval do
69
-
70
- def exists_by_slug(id)
71
- exists? :slug => id
72
- end
73
-
74
- end
76
+ base.extend ClassMethods
75
77
  end
76
78
 
77
79
  protected
78
80
 
79
81
  def previous_slug?(slug)
80
82
  r = self.class.where(
81
- (new_record? ? '' : "id != #{id} AND ") + "(slug = '#{slug}' OR slug LIKE '#{slug}-_')"
83
+ "(slug = '#{slug}' OR slug LIKE '#{slug}-_')"
82
84
  ).order(
83
85
  'slug DESC'
84
86
  ).first
85
- r.respond_to?(:slug) ? r.slug : nil
87
+ r.respond_to?(:slug) ? (r == self ? false : r.slug) : nil
86
88
  end
87
89
 
88
90
  def generate_slug
89
91
  assign_slug
90
92
  end
91
93
 
94
+ module ClassMethods
95
+
96
+ def exists_by_slug(id)
97
+ exists? :slug => id
98
+ end
99
+
100
+ end
92
101
  end
93
102
  module Translatable
94
103
 
95
104
  def self.included(base)
96
- base.instance_eval do
97
-
98
- def find_by_slug(id)
99
- t = reflect_on_association(:translations)
100
- joins(
101
- "INNER JOIN #{t.table_name} t ON t.#{t.foreign_key} = #{table_name}.#{t.active_record_primary_key}"
102
- ).where(
103
- "t.slug = '#{id}' AND t.locale = '#{I18n.locale.to_s}'"
104
- ).readonly(false).first
105
- end
106
-
107
- def exists_by_slug(id)
108
- t = reflect_on_association(:translations)
109
- joins(:translations).exists?(t.table_name.to_sym => {:slug => id, :locale => I18n.locale})
110
- end
111
-
112
- end
105
+ base.extend ClassMethods
113
106
  end
114
107
 
115
108
  protected
@@ -124,11 +117,11 @@ module RailsSlugs
124
117
  r = self.class.joins(
125
118
  "INNER JOIN #{t.table_name} t ON t.#{t.foreign_key} = #{self.class.table_name}.#{t.active_record_primary_key}"
126
119
  ).where(
127
- (new_record? ? '' : "#{self.class.table_name}.id != #{id} AND ") + "(t.slug = '#{slug}' OR t.slug LIKE '#{slug}-_') AND t.locale = '#{I18n.locale.to_s}'"
120
+ "(t.slug = '#{slug}' OR t.slug LIKE '#{slug}-_') AND t.locale = '#{I18n.locale.to_s}'"
128
121
  ).order(
129
122
  't.slug DESC'
130
123
  ).first
131
- r.respond_to?(:slug) ? r.slug : nil
124
+ r.respond_to?(:slug) ? (r == self ? false : r.slug) : nil
132
125
  end
133
126
 
134
127
  def generate_slugs
@@ -139,7 +132,24 @@ module RailsSlugs
139
132
  end
140
133
  change_locale locale
141
134
  end
142
-
135
+
136
+ module ClassMethods
137
+
138
+ def find_by_slug(id)
139
+ t = reflect_on_association(:translations)
140
+ joins(
141
+ "INNER JOIN #{t.table_name} t ON t.#{t.foreign_key} = #{table_name}.#{t.active_record_primary_key}"
142
+ ).where(
143
+ "t.slug = '#{id}' AND t.locale = '#{I18n.locale.to_s}'"
144
+ ).readonly(false).first
145
+ end
146
+
147
+ def exists_by_slug(id)
148
+ t = reflect_on_association(:translations)
149
+ joins(:translations).exists?(t.table_name.to_sym => {:slug => id, :locale => I18n.locale})
150
+ end
151
+
152
+ end
143
153
  end
144
154
  end
145
155
  end
@@ -3,7 +3,7 @@ module RailsSlugs
3
3
 
4
4
  initializer 'rails_slugs' do
5
5
  ::ActiveRecord::Relation.send :include, RailsSlugs::ActiveRecord::Relation
6
- ::ActiveRecord::Base.send :extend, RailsSlugs::ActiveRecord::Base::NonSluggable
6
+ ::ActiveRecord::Base.send :include, RailsSlugs::ActiveRecord::Base
7
7
  end
8
8
 
9
9
  end
@@ -1,5 +1,5 @@
1
1
  module RailsSlugs
2
2
 
3
- VERSION = '1.0.8'
3
+ VERSION = '1.0.9'
4
4
 
5
5
  end