Skip to content

Commit

Permalink
Rails3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
knapo committed Jun 6, 2010
1 parent 135ae7d commit 5d9abd6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
2 changes: 1 addition & 1 deletion init.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require 'string_ext'
require 'lib/string_ext'
ActiveRecord::Base.extend PermalinkFu::PluginMethods
49 changes: 17 additions & 32 deletions lib/permalink_fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,53 +61,38 @@ module PluginMethods
# end
#
def has_permalink(attr_names = [], permalink_field = nil, options = {})
include InstanceMethods

if permalink_field.is_a?(Hash)
options = permalink_field
permalink_field = nil
end
ClassMethods.setup_permalink_fu_on self do
self.permalink_attributes = Array(attr_names)
self.permalink_field = (permalink_field || 'permalink').to_s
self.permalink_options = {:unique => true}.update(options)
end
end
end

# Contains class methods for ActiveRecord models that have permalinks
module ClassMethods
def self.setup_permalink_fu_on(base)
base.extend self
class << base
attr_accessor :permalink_options
attr_accessor :permalink_attributes
attr_accessor :permalink_field
end
base.send :include, InstanceMethods

yield
cattr_accessor :permalink_options
cattr_accessor :permalink_attributes
cattr_accessor :permalink_field

self.permalink_attributes = Array(attr_names)
self.permalink_field = (permalink_field || 'permalink').to_s
self.permalink_options = {:unique => true}.update(options)

if base.permalink_options[:unique]
base.before_validation :create_unique_permalink
if self.permalink_options[:unique]
before_validation :create_unique_permalink
else
base.before_validation :create_common_permalink
before_validation :create_common_permalink
end
class << base
alias_method :define_attribute_methods_without_permalinks, :define_attribute_methods
alias_method :define_attribute_methods, :define_attribute_methods_with_permalinks
end unless base.respond_to?(:define_attribute_methods_without_permalinks)
end

def define_attribute_methods_with_permalinks
if value = define_attribute_methods_without_permalinks
evaluate_attribute_method permalink_field, "def #{self.permalink_field}=(new_value);write_attribute(:#{self.permalink_field}, new_value.blank? ? '' : PermalinkFu.escape(new_value));end", "#{self.permalink_field}="
define_method :"#{self.permalink_field}=" do |value|
value.blank? ? '' : PermalinkFu.escape(value)
end
value

end
end

# This contains instance methods for ActiveRecord models that have permalinks.
module InstanceMethods
protected
protected
def create_common_permalink
return unless should_create_permalink?
if read_attribute(self.class.permalink_field).blank? || permalink_fields_changed?
Expand Down Expand Up @@ -157,7 +142,7 @@ def create_permalink_for(attr_names)
str.blank? ? PermalinkFu.random_permalink : str
end

private
private
def should_create_permalink?
if self.class.permalink_field.blank?
false
Expand Down

0 comments on commit 5d9abd6

Please sign in to comment.