Dynamic conditions are now accepted if you pass a block to scope out. Notice how the block evaluates to a Hash. This is important because the block is evaluated each time the scope is called and the result is fed directly to Rails' with_scope method as the options parameter.
class Article < ActiveRecord::Base
scope_out :published_today do
{:conditions => {:publish_date => Date.today}}
end
end
You can combine scopes with a new method called (predictably) combined_scope. This method takes previously defined scopes which were created with scope_out and nests them in order to create a new scope.
class Person < ActiveRecord::Base
scope_out :software_developers, :conditions => {:job => "Software Developer"}
scope_out :ruby_programmers, :conditions => {:primary_language => "Ruby"}
combined_scope :happy_programmers, [:software_developers, :ruby_programmers]
end
7 comments:
Awesome plugin - I'm definitely going to be using this.
One point - I would suggest you make the with_blah method protected for the same reason that with_scope is becoming protected:
RoR Core thread
John, this is excelent, the plugin works great.
In the plugin README you mention that dynamic conditions are evaluated at class load time and not when the condition is checked. Have you found any workaround for this?
Thank you and keep up the good work!
@3836: Thanks for the compliment. scope_out has had the ability to do dynamic conditions for some time now, but I hadn't updated the docs, apparently. I have now done so.
@anonymous: I just checked in revision 29 of the plugin, which makes with_x protected. Thanks for pointing that out to me.
Wish I'd found this code 3 weeks ago :-)
I have a situation where the scope to be set is based on joining several tables.
Any chance of a version of with_scope that allows :joins, or :includes?
That would just be fantastic.
--Kip
John, any chance you could tell me if its possible to refactor the following with your plugin. Ive tried with no success:
http://pastie.caboo.se/125594
This is a situation I seem to run into a lot that I havent found an elegant solution for yet. I have a feeling your plugin may provide the way to do this nicely, but I wasnt able to completely figure it out.
Excellent work - but I'm not sure how to mix with has_many_polymorphs?
Could I persuade you to consider taking just a quick peep at http://www.railsforum.com/viewtopic.php?id=14746
Beyond that - all the best from Denmark,
Walther
Dear John....
I am hoping you are from the family I have been looking for a very long time. My parents back in the 50 and 60s were very good friend with Ben and Emma Andrews. You would of grown up in the suburbs of Cleveland. Would you either way please e-mail back to me....at DeckerP@aol.com
thank you very much..and I will keep my fingers crossed!
pam
Post a Comment