Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Inheritance Determination in V...

Replytotopic

Inheritance Determination in View

Posted in Forums : Ask a Rails expert

 
Profile

Authority 12
Posting Rating 0
Sign in to rate this post

I tried posting this on Railsforums.com, but got no answer after a week. I’m guessing that the answer is easy, I just don’t know where to look for it. Anyway, here it is:

I have a view that is going to create a select box in a form, allowing the user to choose from a set of Fields. Each type of Field is determined according to an inheritance hierarchy that may be modified at some time in the future, meaning that I need the select box entries to be determined automatically. I tried using this function:
[code]
  1. def self.subclasses(direct = false)
  2. classes = []
  3. if direct
  4. ObjectSpace.each_object(self.class) do |c|
  5. next unless c.superclass == self
  6. classes << c
  7. end
  8. else
  9. ObjectSpace.each_object(self.class) do |c|
  10. next unless c.ancestors.include?(self) and (c != self)
  11. classes << c
  12. end
  13. end
  14. classes
  15. end
    [/code]

The problem comes from when I call this function within the view that displays the select box. What is happening is that ObjectSpace only sees the current object associated with this view (a subclass of Field), and the Field parent, probably because Rails’ dynamic loading doesn’t see fit to load the other models within the hierarchy (which is understandable default behavior). I could manually add requires to see all the objects in question, but then I would have to update that list of requires every time a change is made to the hierarchy, and depending on where I stick the requires in the first place, I may be loading code that won’t be used in a particular situation. Since this seems like an obvious issue to come up, I’m imagining that Rails has solved this in some beautiful way, but I haven’t been able to find what it is.

I have a temporary solution in place-I stuck the name of every class in the two views that needed their names-but then if a class is added to the hierarchy, it will have to also be added to both of those places, which is not acceptable, for obvious reasons.

So that’s the story. Can anyone help me out?

 
Profile

Authority 62
Posting Rating 7
Sign in to rate this post

Just thinking outloud (I haven’t actually tried this), but what if you created a subdirectory in models to hold all these related classes (i.e. app/modes/fields/...).

Then you could use a Dir.glob call on that subforlder to see what classes are defined. Assuming you stick to the standard of your filenames matching your class names, I think that should work.

Replytotopic

Other Recent Topics

Ask a Rails expert : How to parse <pubdate> in RSS

Ask a Rails expert : Couldn't find Product without an ID

Ask a Rails expert : HTML to XML ---> get title & description

Ask a Rails expert : Accessing controller actions from scripted page?

Ask a Rails expert : seledted option for select_tag

Ask a Rails expert : Merging fields from two tables into one, including duplicates

Ask a Rails expert : saving has_many :through

Ask a Rails expert : Use Rails to develop sites for both Designer and Programmer

Ask a Rails expert : Rails+RS232

Ask a Rails expert : Is this a good way to add Admin section

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel