lib/permalink/orm/base.rb in permalink-1.1.2 vs lib/permalink/orm/base.rb in permalink-1.2.0

- old
+ new

@@ -3,19 +3,21 @@ module Base module ClassMethods def setup_permalink(orm_options, from, options) options.reverse_merge!(orm_options) options.reverse_merge!({ - :to => :permalink, - :unique => false + :to => :permalink, + :unique => false, + :force => false }) self.permalink_options = { :from_column_name => from, :to_column_name => options[:to], :to_param => [options[:to_param]].flatten, - :unique => options[:unique] + :unique => options[:unique], + :force => options[:force] } before_validation :create_permalink before_save :create_permalink end @@ -64,11 +66,15 @@ def to_permalink_value read_attribute(to_permalink_name) end + def update_permalink? + self.class.permalink_options[:force] || to_permalink_value.blank? + end + def create_permalink - unless from_permalink_value.blank? || !to_permalink_value.blank? + if update_permalink? write_attribute(to_permalink_name, next_available_permalink(from_permalink_value.to_s.to_permalink)) end end end end