views:

59

answers:

1

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

A: 

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

class Parent < ActiveRecord::Base
  has_permalink :name
end

class Child < Parent
end

you can access the permalink by writing Parent.new.permalink or Child.new.permalink. This method name can be changed by writing

class Parent < ActiveRecord::Base
  has_permalink :name 'custom_permalink_name'
end

If so, the permalink will be accessible by writing Parent.new.custom_permalink_name (or Child.new.custom_permalink_name).

What's the problem with this? The permalink_field accessor methods are defined on Parent's metaclass:

class << self
  attr_accessor :permalink_field
end

When you run the has_permalink method, it calls Parent.permalink_field = 'permalink'.

The problem is that although the permalink_field method 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_field is stored on the Parent class, the Child does not inherit the value, although it inherits the accessor methods. As Child.permalink_field is blank, the should_create_permalink? returns false, and Child.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

class << base
  attr_accessor :permalink_options
  attr_accessor :permalink_attributes
  attr_accessor :permalink_field
end

with

base.cattr_accessor :permalink_options
base.cattr_accessor :permalink_attributes
base.cattr_accessor :permalink_field

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 Parent and all its subclasses (and subsubclasses).

Hugo Peixoto
Thanks for the help, it definitely gets me started on a fix.
fullware