This question is related to extending class methods in Ruby, perhaps more specifically in the way that permalink_fu does so.
It appears that has_permalink on a model will not be available in a derived model. Certainly I would expect anything defined in a class to be inherited by its derived classes.
class MyScope::MyClass < ActiveRecord::Base
unloadable
self.abstract_class = true
has_permalink :name
end
class MyClass < MyScope::MyClass
unloadable
#has_permalink :name # This seems to be required
end
Is there something in the way permalink_fu mixes itself in that causes this issue?
I’m using the permalink-v.1.0.0 gem http://github.com/goncalossilva/permalink_fu
After investigating this, I can now see that the problem is related to how permalink_fu verifies it it should create a permalink or not. It verifies this by checking if the permalink_field of the class is blank or not.
What’s the permalink_field? When you do
you can access the permalink by writing
Parent.new.permalinkorChild.new.permalink. This method name can be changed by writingIf so, the permalink will be accessible by writing
Parent.new.custom_permalink_name(orChild.new.custom_permalink_name).What’s the problem with this? The permalink_field accessor methods are defined on
Parent‘s metaclass:When you run the has_permalink method, it calls
Parent.permalink_field = 'permalink'.The problem is that although the
permalink_fieldmethod is available on all subclasses, its value is stored on the class it was called. This means that the value is not propagated to the subclasses.So, as the
permalink_fieldis stored on theParentclass, the Child does not inherit the value, although it inherits the accessor methods. AsChild.permalink_fieldis blank, theshould_create_permalink?returnsfalse, andChild.create :name => 'something'does not create a permalink.A possible solution would be to replace the attr_acessors on the metaclass with cattr_accessors on the class (lines 57 to 61 on the permalink_fu.rb file).
Replace
with
Note that this will invalidate any possible customization on the subclass. You will no longer be able to specify different options for the subclasses, as these three attributes are shared by
Parentand all its subclasses (and subsubclasses).