ruby - How do you call attr_accessible dynamically in Rails? -


I have a unique class that allows its class to declare a virtual field. Basic virtual areas can be stored in XML form, like calling the method of the original class:

  class Child1 & lt; Parental creation_xml_field ["readings", "use"] and  

I have been able to work through this almost poor work. The create_xml_field method stores field names in the class variable (see below). The init_xml_fields method is called from within the after_initialize method.

  class guardian & lt; ActiveRecord :: base def self.create_xml_field (fields) @@ xml_fields [self.name] = field end init_xml_fields (xml_fields) xml_fields.each do | F | F = f.to_sym self.class_eval do define_method (f) {...} #defined getter define_method (f) {...} #define setter attr_accessible (f) # Add to the list of large scale OK, to work Does not seem to be! End end end reserved def after_initialize init_xml_fields ending  

enough dirty eh? I am not proud, but I am having trouble doing this work. In addition, work around the work does not work with collective assignments of parameters.

Is anyone a dynamically-calling experience to allow mass-assignment in a child class? thank you in advanced!

This post was edited for clarity!

Here's how I apply the metaprogramming part, which will allow accessor methods Makes and sets them as attr_accessibles.

I am using XML's YAML intead as a personal crusade. I also went ahead and applied the unnecessary serial part to just pointing to YAML.

  Requires 'rugbygams' is required 'active_support' Requires 'active_record' module required Yamlable def self.included m.extend classmethods end module classmethods def Add_yaml_fields * args write_inherty_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 start (* Args) Super YAML :: Load (yamlable_column) .each {| K, v | Send ("# {k} =", v)} End and Class ParentModel & lt; ActiveRecord :: Base contains the Yamlable add_yaml_fields: foo,: Bar and Class Balmodel & lt; Parents' end # Look, they are there: y ChildModel.read_inheritable_attribute (: yaml_fields)  

Now, if you want to know why your specific code does not work, you Post more then.


I should have a little detail on the inherited characteristics in class. They're a bit like class variables, for example, like Class Instance Variables.

If you define an inheritable trait in a class, all its subclasses will share it, but if you update, then a child's class is called a specialty, then this child The class copies the original attribute and updates it, so the updates are unique to it and do not affect other classes around it in the heritage chain around it.

With the usual write_inheritable_attribute method, setting it in a child's classroom will override the value of the parent with the inherited array and hashes, worthy to write _ * will merge / merge the values ​​of the original class


Then, in practice, my add_yaml_fields works like this:

< Pre> class parents add_yaml_attributes: foo class child1 < Parents add_yaml_attributes: Bar Class Child 2 & lt; Parents add_yaml_attributes: baz

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

  • Parents: foo
  • Child 1: foo, bar
  • hair 2: foo, baz

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -