Changeset 7826
- Timestamp:
- 10/10/07 19:11:50 (2 years ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/base.rb (modified) (4 diffs)
- trunk/activerecord/test/finder_test.rb (modified) (1 diff)
- trunk/activerecord/test/fixtures/computer.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r7812 r7826 1 1 *SVN* 2 3 * find_and_(initialize|create)_by methods can now properly initialize protected attributes [Tobias Luetke] 2 4 3 5 * belongs_to infers the foreign key from the association name instead of from the class name. [Jeremy Kemper] trunk/activerecord/lib/active_record/base.rb
r7802 r7826 1270 1270 end 1271 1271 end 1272 } 1272 }, __FILE__, __LINE__ 1273 1273 send(method_id, *arguments) 1274 1274 elsif match = /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/.match(method_id.to_s) … … 1285 1285 find_attributes = attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args) 1286 1286 end 1287 1287 1288 1288 options = { :conditions => find_attributes } 1289 1289 set_readonly_option!(options) 1290 1290 1291 find_initial(options) || send(:#{instantiator}, attributes) 1291 record = find_initial(options) 1292 if record.nil? 1293 record = self.new { |r| r.send(:attributes=, attributes, false) } 1294 #{'record.save' if instantiator == :create} 1295 record 1296 else 1297 record 1298 end 1292 1299 end 1293 } 1300 }, __FILE__, __LINE__ 1294 1301 send(method_id, *arguments) 1295 1302 else … … 1842 1849 # from this form of mass-assignment by using the +attr_protected+ macro. Or you can alternatively 1843 1850 # specify which attributes *can* be accessed in with the +attr_accessible+ macro. Then all the 1844 # attributes not included in that won't be allowed to be mass-assigned. 1845 def attributes=(new_attributes )1851 # attributes not included in that won't be allowed to be mass-assigned. 1852 def attributes=(new_attributes, guard_protected_attributes = true) 1846 1853 return if new_attributes.nil? 1847 1854 attributes = new_attributes.dup … … 1849 1856 1850 1857 multi_parameter_attributes = [] 1851 remove_attributes_protected_from_mass_assignment(attributes).each do |k, v| 1858 attributes = remove_attributes_protected_from_mass_assignment(attributes) if guard_protected_attributes 1859 1860 attributes.each do |k, v| 1852 1861 k.include?("(") ? multi_parameter_attributes << [ k, v ] : send(k + "=", v) 1853 1862 end trunk/activerecord/test/finder_test.rb
r7510 r7826 470 470 assert sig38.new_record? 471 471 end 472 473 def test_find_or_initialize_from_one_attribute_should_set_attribute_even_when_protected 474 c = Company.find_or_initialize_by_name_and_rating("Fortune 1000", 1000) 475 assert_equal "Fortune 1000", c.name 476 assert_equal 1000, c.rating 477 assert c.valid? 478 assert c.new_record? 479 end 480 481 def test_find_or_create_from_one_attribute_should_set_attribute_even_when_protected 482 c = Company.find_or_create_by_name_and_rating("Fortune 1000", 1000) 483 assert_equal "Fortune 1000", c.name 484 assert_equal 1000, c.rating 485 assert c.valid? 486 assert !c.new_record? 487 end 472 488 473 489 def test_dynamic_find_or_initialize_from_one_attribute_caches_method trunk/activerecord/test/fixtures/computer.rb
r388 r7826 2 2 belongs_to :developer, :foreign_key=>'developer' 3 3 end 4