djanowski-permalink_fu 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/permalink_fu.rb +24 -23
- metadata +1 -1
data/lib/permalink_fu.rb
CHANGED
@@ -18,11 +18,6 @@ module PermalinkFu
|
|
18
18
|
|
19
19
|
def self.included(base)
|
20
20
|
base.extend ClassMethods
|
21
|
-
class << base
|
22
|
-
attr_accessor :permalink_options
|
23
|
-
attr_accessor :permalink_attributes
|
24
|
-
attr_accessor :permalink_field
|
25
|
-
end
|
26
21
|
end
|
27
22
|
|
28
23
|
module ClassMethods
|
@@ -50,9 +45,14 @@ module PermalinkFu
|
|
50
45
|
def has_permalink(*args)
|
51
46
|
options = args.extract_options!
|
52
47
|
attr_names = args.first || :to_s
|
53
|
-
|
54
|
-
|
55
|
-
|
48
|
+
write_inheritable_attribute(:permalink_attributes, Array(attr_names))
|
49
|
+
write_inheritable_attribute(:permalink_field, (options.delete(:as) || 'permalink').to_s)
|
50
|
+
write_inheritable_attribute(:permalink_options, options)
|
51
|
+
|
52
|
+
%w{permalink_attributes permalink_field permalink_options}.each do |v|
|
53
|
+
class_inheritable_reader(v.to_sym)
|
54
|
+
end
|
55
|
+
|
56
56
|
before_validation :create_unique_permalink
|
57
57
|
evaluate_attribute_method permalink_field, "def #{self.permalink_field}=(new_value);write_attribute(:#{self.permalink_field}, PermalinkFu.escape(new_value));end", "#{self.permalink_field}="
|
58
58
|
extend PermalinkFinders
|
@@ -70,13 +70,13 @@ module PermalinkFu
|
|
70
70
|
|
71
71
|
module ToParam
|
72
72
|
def to_param
|
73
|
-
send(self.class.permalink_field)
|
73
|
+
send(self.class.read_inheritable_attribute(:permalink_field))
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
77
|
module ToParamWithID
|
78
78
|
def to_param
|
79
|
-
permalink = send(self.class.permalink_field)
|
79
|
+
permalink = send(self.class.read_inheritable_attribute(:permalink_field))
|
80
80
|
return super if new_record? || permalink.blank?
|
81
81
|
"#{id}-#{permalink}"
|
82
82
|
end
|
@@ -96,26 +96,27 @@ module PermalinkFu
|
|
96
96
|
protected
|
97
97
|
def create_unique_permalink
|
98
98
|
return unless should_create_permalink?
|
99
|
-
if send(self.class.permalink_field).to_s.empty?
|
100
|
-
send("#{self.class.permalink_field}=", create_permalink_for(self.class.permalink_attributes))
|
99
|
+
if send(self.class.read_inheritable_attribute(:permalink_field)).to_s.empty?
|
100
|
+
send("#{self.class.read_inheritable_attribute(:permalink_field)}=", create_permalink_for(self.class.read_inheritable_attribute(:permalink_attributes)))
|
101
101
|
end
|
102
|
-
limit = self.class.columns_hash[self.class.permalink_field].limit
|
103
|
-
base = send("#{self.class.permalink_field}=",
|
102
|
+
limit = self.class.columns_hash[self.class.read_inheritable_attribute(:permalink_field)].limit
|
103
|
+
base = send("#{self.class.read_inheritable_attribute(:permalink_field)}=",
|
104
|
+
send(self.class.read_inheritable_attribute(:permalink_field))[0..limit - 1])
|
104
105
|
counter = 1
|
105
106
|
# oh how i wish i could use a hash for conditions
|
106
|
-
conditions = ["#{self.class.permalink_field} = ?", base]
|
107
|
+
conditions = ["#{self.class.read_inheritable_attribute(:permalink_field)} = ?", base]
|
107
108
|
unless new_record?
|
108
109
|
conditions.first << " and id != ?"
|
109
110
|
conditions << id
|
110
111
|
end
|
111
|
-
if self.class.permalink_options[:scope]
|
112
|
-
conditions.first << " and #{self.class.permalink_options[:scope]} = ?"
|
113
|
-
conditions << send(self.class.permalink_options[:scope])
|
112
|
+
if self.class.read_inheritable_attribute(:permalink_options)[:scope]
|
113
|
+
conditions.first << " and #{self.class.read_inheritable_attribute(:permalink_options)[:scope]} = ?"
|
114
|
+
conditions << send(self.class.read_inheritable_attribute(:permalink_options)[:scope])
|
114
115
|
end
|
115
116
|
while self.class.exists?(conditions)
|
116
117
|
suffix = "-#{counter += 1}"
|
117
118
|
conditions[1] = "#{base[0..limit-suffix.size-1]}#{suffix}"
|
118
|
-
send("#{self.class.permalink_field}=", conditions[1])
|
119
|
+
send("#{self.class.read_inheritable_attribute(:permalink_field)}=", conditions[1])
|
119
120
|
end
|
120
121
|
end
|
121
122
|
|
@@ -125,10 +126,10 @@ protected
|
|
125
126
|
|
126
127
|
private
|
127
128
|
def should_create_permalink?
|
128
|
-
if self.class.permalink_options[:if]
|
129
|
-
evaluate_method(self.class.permalink_options[:if])
|
130
|
-
elsif self.class.permalink_options[:unless]
|
131
|
-
!evaluate_method(self.class.permalink_options[:unless])
|
129
|
+
if self.class.read_inheritable_attribute(:permalink_options)[:if]
|
130
|
+
evaluate_method(self.class.read_inheritable_attribute(:permalink_options)[:if])
|
131
|
+
elsif self.class.read_inheritable_attribute(:permalink_options)[:unless]
|
132
|
+
!evaluate_method(self.class.read_inheritable_attribute(:permalink_options)[:unless])
|
132
133
|
else
|
133
134
|
true
|
134
135
|
end
|