From fab5447209fcfe3d7ef0b3b43b9c037d144306dc Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Sat, 27 Nov 2010 14:10:19 -0500 Subject: [PATCH] spring-load scopes array --- hobo/doctest/scopes.rdoctest | 16 +++++++++++++--- hobo/lib/hobo/scopes/named_scope_extensions.rb | 19 ++++++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/hobo/doctest/scopes.rdoctest b/hobo/doctest/scopes.rdoctest index edac738..aced1ea 100644 --- a/hobo/doctest/scopes.rdoctest +++ b/hobo/doctest/scopes.rdoctest @@ -95,6 +95,7 @@ Let's set up a few models for our testing: born_at :date code :integer male :boolean + foo :string timestamps end @@ -119,19 +120,19 @@ Generate a migration and run it: >> ActiveRecord::Migration.class_eval(HoboFields::MigrationGenerator.run[0]) >> Person.columns.*.name - => ["id", "name", "born_at", "code", "male", "created_at", "updated_at", "state"] + => ["id", "name", "born_at", "code", "male", "foo", "created_at", "updated_at", "state"] {.hidden} And create a couple of fixtures: >> Bryan = Person.new(:name => "Bryan", :code => 17, - :born_at => Date.new(1973,4,8), :male => true) + :born_at => Date.new(1973,4,8), :male => true, :foo => "common") >> Bryan.state = "active" >> Bryan.save! >> Bethany = Person.new(:name => "Bethany", :code => 42, - :born_at => Date.new(1975,5,13), :male => false) + :born_at => Date.new(1975,5,13), :male => false, :foo => "common") >> Bethany.state = "inactive" >> Bethany.save! >> Friendship.new(:person => Bryan, :friend => Bethany).save! @@ -394,6 +395,15 @@ Like named scopes, Hobo scopes can be chained: >> Bryan.inactive_friends.inactive.*.name => ["Bethany"] + >> Person.scoped(:conditions => {:state => "active"}).scoped(:conditions => {:foo => "common"}).*.name + => ["Bryan"] + + >> Person.with_friendship(Friendship.first).born_before(Date.new(1974)).*.name + => ["Bryan"] + + >> Person.born_after(Date.new(1974)).with_friendship(Friendship.first).*.name + => [] + Clean up our test database: {.hidden} diff --git a/hobo/lib/hobo/scopes/named_scope_extensions.rb b/hobo/lib/hobo/scopes/named_scope_extensions.rb index 8c769f9..a29bec4 100644 --- a/hobo/lib/hobo/scopes/named_scope_extensions.rb +++ b/hobo/lib/hobo/scopes/named_scope_extensions.rb @@ -6,18 +6,23 @@ module ActiveRecord include Hobo::Scopes::ApplyScopes - def respond_to_with_hobo_scopes?(method, include_private=false) - scopes.include?(method) || proxy_scope.respond_to?(method, include_private) || respond_to_without_hobo_scopes?(method, include_private) + end + module ClassMethods + def scopes + read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, new_automatic_scoping_hash(self)) end - alias_method_chain :respond_to?, :hobo_scopes private - def method_missing_with_hobo_scopes(method, *args, &block) - respond_to?(method) # required for side effects, see LH#839 - method_missing_without_hobo_scopes(method, *args, &block) + def new_automatic_scoping_hash(o) + Hash.new { |hash, key| o.create_automatic_scope(key) && hash[key] }.tap do |h| + h.meta_eval do + define_method :include? do |key| + super || o.create_automatic_scope(key) + end + end + end end - alias_method_chain :method_missing, :hobo_scopes end end -- 1.5.3.1