Monkey Patching and acts_as_indexed

Hi,
I’ve monkey patched ActiveRecord::Base in a way so that every model
class knows which attributes are “important”.

So in my model I can now do sth like this:

class User < ActiveRecord::Base
attribute_order :username, :password, :name, :mailaddress, :usergroup,
:created_at
acts_as_indexed :fields => self.sorted_attributes
end

The monkey-patch looks like this:

class ActiveRecord::Base

def self.attribute_order(*values)
@_sorted_attributes = []
values.collect do |v|
@_sorted_attributes.push(v)
end
end

def self.sorted_attributes
if defined? @_sorted_attributes
@_sorted_attributes
else
self.attribute_names.map do |a|
a.to_sym
end
end
end
end

Now I’d like to add the acts_as_indexed method to ALL my models by
adding it into the monkey-patch:

class ActiveRecord::Base
acts_as_indexed :fields => self.sorted_attributes

end

This doesn’t work as the sorted attributes don’t seem to exist when the
Class is Inheriting from ActiveRecord::Base, and the acts_as_indexed is
called too early. How could I delay the call of acts_as_indexed until
the class has been initialized? putting it into attribute_order doesn’t
help, because this method must not be called by every model.

Wolfgang

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs