0
attr_accessor :translation_to
0
attr_accessor :translation_from
0
- s = Iconv.iconv(translation_to
, translation_from, str).to_s
0
+ s = Iconv.iconv(translation_to
+ '//IGNORE', translation_from, str).to_s
0
s.gsub!(/\W+/, ' ') # all non-word chars to spaces
0
@@ -13,27 +14,78 @@ module PermalinkFu
0
+ def self.included(base)
0
+ base.extend ClassMethods
0
+ attr_accessor :permalink_options
0
+ attr_accessor :permalink_attributes
0
+ attr_accessor :permalink_field
0
+ # Specifies the given field(s) as a permalink, meaning it is passed through PermalinkFu.escape and set to the permalink_field. This
0
+ # class Foo < ActiveRecord::Base
0
+ # # stores permalink form of #title to the #permalink attribute
0
+ # has_permalink :title
0
+ # # stores a permalink form of "#{category}-#{title}" to the #permalink attribute
0
+ # has_permalink [:category, :title]
0
+ # # stores permalink form of #title to the #category_permalink attribute
0
+ # has_permalink [:category, :title], :category_permalink
0
+ # has_permalink :title, :scope => :blog_id
0
+ # # add a scope and specify the permalink field name
0
+ # has_permalink :title, :slug, :scope => :blog_id
0
+ def has_permalink(attr_names = [], permalink_field = nil, options = {})
0
+ if permalink_field.is_a?(Hash)
0
+ options = permalink_field
0
+ self.permalink_attributes = Array(attr_names)
0
+ self.permalink_field = permalink_field || :permalink
0
+ self.permalink_options = options
0
+ before_validation :create_unique_permalink
0
+ evaluate_attribute_method permalink_field, "def #{self.permalink_field}=(new_value);write_attribute(:#{self.permalink_field}, PermalinkFu.escape(new_value));end", "#{self.permalink_field}="
0
+ def create_unique_permalink
0
+ if send(self.class.permalink_field).to_s.empty?
0
+ send("#{self.class.permalink_field}=", create_permalink_for(self.class.permalink_attributes))
0
+ base = send(self.class.permalink_field)
0
+ # oh how i wish i could use a hash for conditions
0
+ conditions = ["#{self.class.permalink_field} = ?", base]
0
+ conditions.first << " and id != ?"
0
+ if self.class.permalink_options[:scope]
0
+ conditions.first << " and #{self.class.permalink_options[:scope]} = ?"
0
+ conditions << send(self.class.permalink_options[:scope])
0
+ while self.class.exists?(conditions)
0
+ conditions[1] = "#{base}-#{counter += 1}"
0
+ send("#{self.class.permalink_field}=", conditions[1])
0
- # Specifies the given field(s) as a permalink, meaning it is passed through PermalinkFu.escape and set to the permalink_field. This
0
- # class Foo < ActiveRecord::Base
0
- # # stores permalink form of #title to the #permalink attribute
0
- # has_permalink :title
0
- # # stores a permalink form of "#{category}-#{title}" to the #permalink attribute
0
- # has_permalink [:category, :title]
0
- # # stores permalink form of #title to the #category_permalink attribute
0
- # has_permalink [:category, :title], :category_permalink
0
- def has_permalink(attr_names = [], permalink_field = nil)
0
- permalink_field ||= 'permalink'
0
- before_validation { |record| record.send("#{permalink_field}=", Array(attr_names).collect { |attr_name| PermalinkFu.escape(record.send(attr_name).to_s) }.join('-')) if record.send(permalink_field).to_s.empty? }
0
+ def create_permalink_for(attr_names)
0
+ attr_names.collect { |attr_name| send(attr_name).to_s } * " "
0
PermalinkFu.translation_to = 'ascii//translit'
0
-PermalinkFu.translation_from = 'utf-8'
0
+PermalinkFu.translation_from = 'utf-8'
0
\ No newline at end of file
Comments
No one has commented yet.