I have a rather unique class that allows its child classes to declare virtual fields. The child can declare virtual fields stored as XML by calling a method of the parent class like this:
class Child1 < Parent
create_xml_field ["readings", "usage"]
end
I have managed to get it working via a nasty work around. The create_xml_field method stores the field names in Class variable (see below). The init_xml_fields method is called from inside the after_initialize method.
class Parent < ActiveRecord::Base
def self.create_xml_field(fields)
@@xml_fields[self.name] = fields
end
def init_xml_fields(xml_fields)
xml_fields.each do |f|
f=f.to_sym
self.class_eval do
define_method(f) { ... } # define getter
define_method(f) { ... } # define setter
attr_accessible(f) # add to mass assign OK list, does not seem to work!
end
end
end
protected
def after_initialize
init_xml_fields
end
end
Nasty enough eh? I’m not proud, but I am having trouble making it work. Also, the work around doesn’t work with mass-assignment of form parameters.
Does anyone have experience calling attr_acessible dynamically to allow mass-assignment in the child class? Thank you in advance!
This post was edited for clarity!
Here’s how I’d implement the metaprogramming part that creates the accessor methods and sets them as attr_accessibles.
I’m using YAML intead of XML just as a personal crusade. I even went ahead and implemented the unneeded serialization part just to nudge you towards YAML.
Now, if you want to know why your particular code didn’t work, you’ll have to post more of it.
I should probably expand a bit on class inheritable attributes. They are a bit like class variables, a bit like class instance variables.
If you define an inheritable attribute in a class, all its subclasses will share it. But if you update said attribute in a child class, this child class copies the original attribute and updates it, so the updates are exclusive to it and don’t affect other classes around it in the inheritance chain.
With the normal
write_inheritable_attributemethod, setting it on a child class will simply override the value from the parent. With inheritable arrays and hashes,write_inheritable_*will concat / merge to the parent class’s values.So, in practice, my
add_yaml_fieldsworks like this:With that, the yaml attributes for each class will be: