# This will probably be embarrassingly obvious. It acts as if it's an
# evaluation order issue (like the method call is happening prior to the
# method definition). But that isn't the case. The model_names method
# works fine on the console. I'm missing a core concept obviously.
# Error message from test
tylers-mbpro:test tyler$ ruby helpers/application_helper_test.rb
/Users/tyler/versioned/tma-code/rails/facilit-e/app/helpers/application_helper.rb:23:
undefined local variable or method `model_names' for
ApplicationHelper:Module (NameError)
# ApplicationHelper
module ApplicationHelper
def self.make_user_select_collects(assoc_id_param)
module_eval %{
def select_collect_by_#{assoc_id_param}(klass, assoc_id,
order_by_col='full_name')
klass.find_all_by_#{assoc_id_param}(assoc_id,
:order => order_by_col.to_sym).collect {|s| [s.full_name, s.id]}
end
}
end
def self.model_names
models = Array.new
Dir["app/models/**/*.rb"].each do |f|
models <<
File.basename(f).sub(/.rb$/,'').titlecase.gsub(/\s/,'').constantize
end
models
end
ids = Array.new
model_names.each do |klass| #model_names
klass.column_names.grep(/_id$/).each do |assoc_id|
unless ids.member?(assoc_id)
ids << assoc_id
make_user_select_collects(assoc_id)
end
end
end