1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
Index: test/test_helper.rb
===================================================================
--- test/test_helper.rb (revision 407)
+++ test/test_helper.rb (working copy)
@@ -10,6 +10,7 @@
 require File.dirname(__FILE__) + '/../lib/ez/condition'
 require File.dirname(__FILE__) + '/../lib/ez/hash'
 require File.dirname(__FILE__) + '/../lib/ez/compositions'
+require File.dirname(__FILE__) + '/../lib/ez/scope'
 
 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
 ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
Index: test/ez_where_tests.rb
===================================================================
--- test/ez_where_tests.rb  (revision 407)
+++ test/ez_where_tests.rb  (working copy)
@@ -1178,4 +1178,22 @@
     assert_equal 2, articles.first.id
   end
   
+  def test_where_scope
+    fab = Proc.new { author_id == 1 }
+    
+    cond  = EZ::Where::Condition.new(&fab)
+    scope = EZ::Where::Scope.new(&fab)
+    
+    assert_equal({:find => {:conditions => cond.to_sql}, :create => {:author_id => 1}}, scope.to_sql)
+  end
+  
+  def test_where_scope_with_like
+    fabs_rails_articles = Proc.new { author_id == 1; title =~ '%Rails%' }
+    
+    cond  = EZ::Where::Condition.new(&fabs_rails_articles)
+    scope = EZ::Where::Scope.new(&fabs_rails_articles)
+    
+    assert_equal({:find => {:conditions => cond.to_sql}, :create => {:author_id => 1}}, scope.to_sql)
+  end
+  
 end
Index: init.rb
===================================================================
--- init.rb (revision 407)
+++ init.rb (working copy)
@@ -3,5 +3,6 @@
 require 'ez/condition'
 require 'ez/hash'
 require 'ez/compositions'
+require 'ez/scope'
 
 ActionController::Base.send :include, EZ::Where
\ No newline at end of file
Index: lib/ez/condition.rb
===================================================================
--- lib/ez/condition.rb (revision 407)
+++ lib/ez/condition.rb (working copy)
@@ -4,6 +4,10 @@
     EZ::Where::Condition.new(*args, &block)
   end
   
+  def sc(*args, &block)
+    EZ::Where::Scope.new(*args, &block)
+  end
+  
   def to_c
     self.c + self
   end
Index: lib/ez/scope.rb
===================================================================
--- lib/ez/scope.rb (revision 0)
+++ lib/ez/scope.rb (revision 0)
@@ -0,0 +1,21 @@
+module EZ
+
+  module Where
+    # EZ::Scope plugin for generating the arguments to 
+    # ActiveRecord::Base.with_scope.
+    
+    class Scope < Condition
+      def to_sql
+        scope = {:find => {:conditions => super}, :create => (create = {})}
+        
+        @clauses.each do |cv|
+          cv = cv.to_clause if cv.respond_to?(:to_clause)
+          next unless cv.test == :equals
+          create[cv.name.to_sym] = cv.value
+        end
+        
+        scope
+      end
+    end
+  end
+end
\ No newline at end of file
Index: lib/ez/where.rb
===================================================================
--- lib/ez/where.rb (revision 407)
+++ lib/ez/where.rb (working copy)
@@ -99,7 +99,7 @@
           end
           options
         end
-
+        
         # Returns model specific (table_name prefixed) Condition
         def where_condition(*args, &block)
           options = args.last.is_a?(Hash) ? args.last : {}
@@ -107,7 +107,15 @@
           Condition.new(options, &block)
         end
         alias :ez_condition :where_condition
-        alias :c :where_condition        
+        alias :c :where_condition     
+        
+        def where_scope(*args, &block)
+          options = args.last.is_a?(Hash) ? args.last : {}
+          options[:table_name] ||= table_name
+          Scope.new(options, &block)
+        end   
+        alias :ez_scope :where_scope
+        alias :s :where_scope
       
         def composition(name = :default, *args)
           cond_klass = name.to_s.classify