Ticket #11109: no_named_block_arguments_in_associations_fixed.patch
| File no_named_block_arguments_in_associations_fixed.patch, 5.9 kB (added by adymo, 7 months ago) |
|---|
-
activerecord/lib/active_record/associations/association_proxy.rb
old new 120 120 end 121 121 122 122 private 123 def method_missing(method, *args , &block)123 def method_missing(method, *args) 124 124 if load_target 125 @target.send(method, *args, &block) 125 if block_given? 126 @target.send(method, *args) { |*block_args| yield(*block_args) } 127 else 128 @target.send(method, *args) 129 end 126 130 end 127 131 end 128 132 -
activerecord/lib/active_record/associations/association_collection.rb
old new 43 43 end 44 44 45 45 # Calculate sum using SQL, not Enumerable 46 def sum(*args, &block) 47 calculate(:sum, *args, &block) 46 def sum(*args) 47 if block_given? 48 calculate(:sum, *args) { |*block_args| yield(*block_args) } 49 else 50 calculate(:sum, *args) 51 end 48 52 end 49 53 50 54 # Remove +records+ from this association. Does not destroy +records+. … … 121 125 size.zero? 122 126 end 123 127 124 def any? (&block)128 def any? 125 129 if block_given? 126 method_missing(:any? , &block)130 method_missing(:any?) { |*block_args| yield(*block_args) } 127 131 else 128 132 !empty? 129 133 end … … 157 161 158 162 159 163 protected 160 def method_missing(method, *args , &block)164 def method_missing(method, *args) 161 165 if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) 162 super 166 if block_given? 167 super { |*block_args| yield(*block_args) } 168 else 169 super 170 end 163 171 else 164 @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.send(method, *args, &block) } 172 @reflection.klass.send(:with_scope, construct_scope) { 173 if block_given? 174 @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } 175 else 176 @reflection.klass.send(method, *args) 177 end 178 } 165 179 end 166 180 end 167 181 … … 187 201 188 202 private 189 203 190 def create_record(attrs , &block)204 def create_record(attrs) 191 205 ensure_owner_is_not_new 192 206 record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.new(attrs) } 193 add_record_to_target_with_callbacks(record, &block) 207 if block_given? 208 add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } 209 else 210 add_record_to_target_with_callbacks(record) 211 end 194 212 end 195 213 196 def build_record(attrs , &block)214 def build_record(attrs) 197 215 record = @reflection.klass.new(attrs) 198 add_record_to_target_with_callbacks(record, &block) 216 if block_given? 217 add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } 218 else 219 add_record_to_target_with_callbacks(record) 220 end 199 221 end 200 222 201 223 def add_record_to_target_with_callbacks(record) -
activerecord/lib/active_record/associations/has_many_through_association.rb
old new 113 113 end 114 114 115 115 # Calculate sum using SQL, not Enumerable 116 def sum(*args, &block) 117 calculate(:sum, *args, &block) 116 def sum(*args) 117 if block_given? 118 calculate(:sum, *args) { |*block_args| yield(*block_args) } 119 else 120 calculate(:sum, *args) 121 end 118 122 end 119 123 120 124 def count(*args) … … 128 132 end 129 133 130 134 protected 131 def method_missing(method, *args , &block)135 def method_missing(method, *args) 132 136 if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) 133 super 137 if block_given? 138 super { |*block_args| yield(*block_args) } 139 else 140 super 141 end 134 142 else 135 @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.send(method, *args, &block) } 143 @reflection.klass.send(:with_scope, construct_scope) { 144 if block_given? 145 @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } 146 else 147 @reflection.klass.send(method, *args) 148 end 149 } 136 150 end 137 151 end 138 152 -
activerecord/test/cases/associations/join_model_test.rb
old new 290 290 assert_equal nil, authors(:david).categories.find_by_name('Technology') 291 291 end 292 292 293 def test_has_many_array_methods_called_by_method_missing 294 assert true, authors(:david).categories.any? { |category| category.name == 'General' } 295 assert_nothing_raised { authors(:david).categories.sort } 296 end 297 293 298 def test_has_many_going_through_join_model_with_custom_foreign_key 294 299 assert_equal [], posts(:thinking).authors 295 300 assert_equal [authors(:mary)], posts(:authorless).authors