Jason R. wrote:
self.bonk!
LOL 
Yeah, missed that. This might just need to be controller-side validation. In
terms of MVC architecture, the model should know nothing about the state of
the controller.
Yes, but it is not in the controller - both attributes are on the
model. I want to check that the :controller value indeed matches an
existing controller, and then, that the :action matches an action
implemented in this controller. But to do the latter (the former works
fine right now), I need to check my self-created controller hash at
the correct index - like this,
validates_inclusion_of(:action, :in =>
controllers[:controller], :message => ‘is not implemented in the
specified controller’)
Except that I don’t know how to get the value in the :controller
attribute. Nothing I can think of writing yields that value… Do I
make any sense? Here’s the entire model:
== Schema Information
Schema version: 6
Table name: navigation_points
id :integer(11) not null, primary key
parent_id :integer(11)
controller :string(255) default(), not null
action :string(255) default(index), not null
name_da :string(255) default(), not null
name_en :string(255) default(), not null
class NavigationPoint < ActiveRecord::Base
acts_as_tree :order => ‘action’, :counter_cache => :children_count
validates_presence_of :controller,
:action,
:name_da,
:name_en
validates_inclusion_of(:controller, :in => controllers, :message =>
‘does not exist in this application’)
validates_inclusion_of(:action, :in =>
controllers[:controller], :message => ‘is not implemented in the
specified controller’)
end
And here’s my ‘controllers’ method, which is a library function:
Generates a 2D array of all possible controllers and their methods
in the application.
def controllers
Suck in all controllers:
require ‘find’
Find.find(File.join(RAILS_ROOT, ‘app/controllers’)) { |name|
require_dependency(name) if /_controller.rb$/ =~ name
}
Consume all classes in a hash:
@classes = {}
ObjectSpace.subclasses_of(ActionController::Base).each do |obj|
@classes["#{obj.controller_name}"] = obj
end
Iterate over the hash, and create our controllers hash:
controllers = Hash.new()
@classes.each_value { |c|
# Add the controller name:
controllers["#{c.controller_name}"] = []
# Add all methods under the controller:
@methods = c.public_instance_methods(false) # This one gets all
methods, even hidden ones.
@methods.sort.each { |m|
controllers["#{c.controller_name}"] << m.to_s
}
}
Return the array
controllers
end
Cheers,
Daniel 