33 let ( :parent ) { TestClass . create ( :col1 => 4 ) }
44
55 it "delegates to parent" do
6- TestClass . virtual_delegate :col1 , :prefix => 'parent' , :to => :ref1
6+ TestClass . virtual_delegate :col1 , :prefix => 'parent' , :to => :ref1 , :type => :integer
77 tc = TestClass . new ( :ref1 => parent )
88 expect ( tc . parent_col1 ) . to eq ( 4 )
99 end
1010
1111 it "delegates to nil parent" do
12- TestClass . virtual_delegate :col1 , :prefix => 'parent' , :to => :ref1 , :allow_nil => true
12+ TestClass . virtual_delegate :col1 , :prefix => 'parent' , :to => :ref1 , :allow_nil => true , :type => :integer
1313 tc = TestClass . new
1414 expect ( tc . parent_col1 ) . to be_nil
1515 end
1616
1717 it "defines parent virtual attribute" do
18- TestClass . virtual_delegate :col1 , :prefix => 'parent' , :to => :ref1
18+ TestClass . virtual_delegate :col1 , :prefix => 'parent' , :to => :ref1 , :type => :integer
1919 expect ( TestClass . virtual_attribute_names ) . to include ( "parent_col1" )
2020 end
2121
2222 it "delegates to parent (sql)" do
23- TestClass . virtual_delegate :col1 , :prefix => 'parent' , :to => :ref1
23+ TestClass . virtual_delegate :col1 , :prefix => 'parent' , :to => :ref1 , :type => :integer
2424 TestClass . create ( :ref1 => parent )
2525 tcs = TestClass . select ( :id , :col1 , TestClass . arel_table [ :parent_col1 ] . as ( "x" ) )
2626 expect ( tcs . map ( &:x ) ) . to match_array ( [ nil , 4 ] )
4444 end . to raise_error ( ArgumentError , /needs an association/ )
4545 end
4646
47+ it "expects a ':type' for delegation" do
48+ expect ( ActiveRecord ::VirtualAttributes . deprecator ) . to receive ( :warn ) . with ( /type/ , anything ( ) )
49+ TestClass . virtual_delegate :col1 , :to => :ref1
50+ TestClass . new
51+ end
52+
4753 it "only allows 1 method when delegating to a specific method" do
4854 expect do
49- TestClass . virtual_delegate :col1 , :col2 , :to => "ref1.method"
55+ TestClass . virtual_delegate :col1 , :col2 , :to => "ref1.method" , :type => :string
5056 end . to raise_error ( ArgumentError , /single virtual method/ )
5157 end
5258
5359 it "only allows 1 level deep delegation" do
5460 expect do
55- TestClass . virtual_delegate :col1 , :to => "ref1.method.method2"
61+ TestClass . virtual_delegate :col1 , :to => "ref1.method.method2" , :type => :string
5662 end . to raise_error ( ArgumentError , /single association/ )
5763 end
5864
5965 it "detects invalid destination" do
6066 expect do
61- TestClass . virtual_delegate :col1 , :to => "bogus_ref.method"
67+ TestClass . virtual_delegate :col1 , :to => "bogus_ref.method" , :type => :string
6268 TestClass . new
6369 end . to raise_error ( ArgumentError , /needs an association/ )
6470 end
7278 let ( :child ) { TestClass . create }
7379
7480 it "delegates to child" do
75- TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2
81+ TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2 , :type => :integer
7682 tc = TestClass . create ( :ref2 => child )
7783 expect ( tc . child_col1 ) . to eq ( tc . id )
7884 end
7985
8086 it "delegates to nil child" do
81- TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2 , :allow_nil => true
87+ TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2 , :allow_nil => true , :type => :integer
8288 tc = TestClass . new
8389 expect ( tc . child_col1 ) . to be_nil
8490 end
8591
8692 it "defines child virtual attribute" do
87- TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2
93+ TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2 , :type => :integer
8894 expect ( TestClass . virtual_attribute_names ) . to include ( "child_col1" )
8995 end
9096
9197 it "delegates to child (sql)" do
92- TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2
98+ TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2 , :type => :integer
9399 tc = TestClass . create ( :ref2 => child )
94100 tcs = TestClass . select ( :id , :col1 , :child_col1 ) . to_a
95101 expect { expect ( tcs . map ( &:child_col1 ) ) . to match_array ( [ nil , tc . id ] ) } . to_not make_database_queries
98104 # this may fail in the future as our way of building queries may change
99105 # just want to make sure it changed due to intentional changes
100106 it "uses table alias for subquery" do
101- TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2
107+ TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2 , :type => :integer
102108 sql = TestClass . select ( :id , :col1 , :child_col1 ) . to_sql
103109 expect ( sql ) . to match ( /["`]test_classes_[^"`]*["`][.]["`]col1["`]/i )
104110 end
114120 # ensure virtual attribute referencing a relation with a select()
115121 # does not throw an exception due to multi-column select
116122 it "properly generates sub select" do
117- TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2
123+ TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2 , :type => :integer
118124 TestClass . create ( :ref2 => child )
119125 expect { TestClass . select ( :id , :child_col1 ) . to_a } . to_not raise_error
120126 end
131137 # ensure virtual attribute referencing a relation with a select()
132138 # does not throw an exception due to multi-column select
133139 it "properly generates sub select" do
134- TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2
140+ TestClass . virtual_delegate :col1 , :prefix => 'child' , :to => :ref2 , :type => :integer
135141 TestClass . create ( :ref2 => child )
136142 expect { TestClass . select ( :id , :child_col1 ) . to_a } . to_not raise_error
137143 end
@@ -150,7 +156,7 @@ def self.connection
150156 end
151157 # TODO: -> { order(:col1) }
152158 TestClass . has_one :child , :class_name => 'TestOtherClass' , :foreign_key => :ocol1
153- TestClass . virtual_delegate :child_str , :to => "child.ostr"
159+ TestClass . virtual_delegate :child_str , :to => "child.ostr" , :type => :string
154160 end
155161
156162 after do
@@ -185,7 +191,7 @@ def self.connection
185191 end
186192
187193 it "delegates to another table" do
188- TestOtherClass . virtual_delegate :col1 , :to => :oref1
194+ TestOtherClass . virtual_delegate :col1 , :to => :oref1 , :type => :integer
189195 TestOtherClass . create ( :oref1 => TestClass . create )
190196 TestOtherClass . create ( :oref1 => TestClass . create ( :col1 => 99 ) )
191197 tcs = TestOtherClass . select ( :id , :ocol1 , TestOtherClass . arel_table [ :col1 ] . as ( "x" ) )
@@ -198,7 +204,7 @@ def self.connection
198204 # this may fail in the future as our way of building queries may change
199205 # just want to make sure it changed due to intentional changes
200206 it "delegates to another table without alias" do
201- TestOtherClass . virtual_delegate :col1 , :to => :oref1
207+ TestOtherClass . virtual_delegate :col1 , :to => :oref1 , :type => :integer
202208 sql = TestOtherClass . select ( :id , :ocol1 , TestOtherClass . arel_table [ :col1 ] . as ( "x" ) ) . to_sql
203209 expect ( sql ) . to match ( /["`]test_classes["`].["`]col1["`]/i )
204210 end
0 commit comments