The background to this problem is quite complex and convoluted, and as I am looking for a simple answer, I will leave it by the wayside in explaining my problem, and instead provide this hypothetical situation.
If I have a simple ActiveRecord model called Automobile, with named_scopes like below:
named_scope :classic, :conditions => { :build_date <= 1969 }
named_scope :fast, lambda { |speed| :top_speed >= speed }
Ignoring the scopes themselves, if I were to call:
Automobile.scopes
What exactly would this be returning? What I see in the console is:
[ :classic => #<Proc:0x01a543d4@/Users/user_name/.gem/ruby/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:87>,
:fast => #<Proc:0x01a543d4@/Users/user_name/.gem/ruby/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:87> ]
This seems to me to be an array of key/values, the key being the symbol of the named scope, and the value being a Proc pointing to the named_scope.rb file in ActiveRecord.
If I want the hash or Proc given as the actual named scope (meaning for :classic, I would receive ":conditions => { :build_date <= 1969 }", how could I go about finding this?
I am writing a plugin that conditional merges some named_scopes, and I am running up against some resistance in regards to this. I am currently using the following to merge these scopes:
scopes_to_use = Automobile.scopes
scoped_options = {}
Automobile.scopes.each do |scope|
scoped_options.safe_merge!(eval "Automobile.#{scope}(self).proxy_options")
end
Ignoring the 'correctness' of what I am doing here, is there a better way that I can retrieve the actual Hash or Proc given to the named_scope? I don't like using 'eval' in this function, and if I could actually retrieve the Hash or Proc, then I would be able to introduce some much more powerful merging logic. Any thoughts on this would be much appreciated. Thanks.