views:

556

answers:

2

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!

+1  A: 

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.

require 'yaml'
require 'rubygems'
require 'active_support'
require 'active_record'

module Yamlable
  def self.included m
    m.extend ClassMethods
  end

  module ClassMethods
    def add_yaml_fields *args
      write_inheritable_array(:yaml_fields, args)
      attr_accessor(*args)
      attr_accessible(*args)
      before_save :serialize_yaml_fields
    end
  end

  def serialize_yaml_fields
    self.yamlable_column = read_inheritable_attribute(:yaml_fields)\
      .inject({}) { |h, a| h[a] = send(a); h }.to_yaml
  end

  def initialize(*args)
    super
    YAML::load(yamlable_column).each { |k, v| send("#{k}=", v) }
  end
end

class ParentModel < ActiveRecord::Base
  include Yamlable
  add_yaml_fields :foo, :bar
end

class ChildModel < ParentModel
end

# look, they're there:
y ChildModel.read_inheritable_attribute(:yaml_fields)

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_attribute method, 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_fields works like this:

class Parent
  add_yaml_attributes :foo

class Child1 < Parent
  add_yaml_attributes :bar

class Child2 < Parent
  add_yaml_attributes :baz

With that, the yaml attributes for each class will be:

  • Parent: foo
  • Child1: foo, bar
  • Child2: foo, baz
kch
This seems to be on the right track, but I'm not sure how to integrate this into my parent class, and your example calls "add_yaml_fields" from the parent and not the child. Thanks for any extra advice.
crunchyt
the Yamlable model can be included in any class, and it'll provide add_yaml_fields to it and all its descendants. So, to call it on the ChildModel, just call it there instead of in the parent. No tricks required.
kch
Thanks so much for trying again! I got it working after changing the "yamlable_column" to "content" which is specific to my DB table. Nice work, I have awarded you the correct answer (and there was so much competition!) Cheers mate.
crunchyt
A: 

@kch is correct, however I found one issue using initialize(*args). ActiveRecord does not always instantiate model objects using new() so the initialize() method is not always called.

Instead use after_initialize(*args) as shown below.

def self.included m
    m.extend ClassMethods
  end

  module ClassMethods
    def add_yaml_fields *args
      write_inheritable_array(:yaml_fields, args)
      attr_accessor(*args)
      attr_accessible(*args)
      before_save :serialize_yaml_fields
    end
  end

  def serialize_yaml_fields
    self.yamlable_column = read_inheritable_attribute(:yaml_fields)\
      .inject({}) { |h, a| h[a] = send(a); h }.to_yaml
  end

  def after_initialize(*args)
    super
    YAML::load(yamlable_column).each { |k, v| send("#{k}=", v) }
  end
end

class ParentModel < ActiveRecord::Base
  include Yamlable
  add_yaml_fields :foo, :bar
end

class ChildModel < ParentModel
end
crunchyt