On Thu, Mar 26, 2009 at 6:33 PM, <ty...@oneinterface.net> wrote:

>
> # 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
>
> end
>
> # Thanks for any help
>
> --
> Thanks,
>
> Tyler Arrigoni
> President
>
> OneInterface.net Network Engineering, Inc.
> 7475 North Palm, Suite 105, Fresno, CA 93711
> Voice: 559-437-3750 ext. 110
> Cell: 559-696-0400
> Fax1: 866-691-5145 (confidential)
> Fax2: 559-437-3754 (non-confidential)
> Email: ty...@oneinterface.net
> GnuPG: http://www.oneinterface.net/crypt/tyler.asc
>

Tyler, I wasn't able to reproduce the error message above using this
simplified example:

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

  puts "Model #{self} completed"

end

darnoc-desktop:app conradwt$ ruby helpers/application_helper.rb

=> Model ApplicationHelper completed

BTW, I used Ruby 1.9.1 to execute the above code.  I'll try the above on my
laptop using
Ruby 1.8.6 to see if it's consistent with Ruby 1.9.1.

-Conrad

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to