Commit Graph

2476 Commits

Author SHA1 Message Date
Aaron Patterson
f8700038af adding a test for no method error 2011-01-11 17:57:02 -08:00
Aaron Patterson
fcd8925f23 use underlying _read_attribute method rather than causing NoMethodErrors 2011-01-11 15:39:26 -08:00
Jon Leighton
552df9b933 Support for create_association! for has_one associations 2011-01-11 13:45:09 -08:00
Jon Leighton
681ab53ba1 Get rid of set_association_target and association_loaded? as the parts of the code that need that can now just use association_proxy(:name).loaded?/target= 2011-01-11 13:45:08 -08:00
Jon Leighton
3b797c8c86 DRY up the code which instantiates the association proxy 2011-01-11 13:45:08 -08:00
Jon Leighton
6055bbedaa Raise ActiveRecord::RecordNotSaved if an AssociationCollection fails to be replaced 2011-01-11 13:45:07 -08:00
Jon Leighton
1d6e218428 When assigning a has_one, if anything fails, the assignment should be rolled back entirely 2011-01-11 13:45:07 -08:00
Jon Leighton
4e19ec566c In a number of places in the tests, we only need to turn off transactional fixtures when the DB does not support savepoints. This speeds the test run up by about 8-9% on my computer, when running rake test_sqlite3_mem :) 2011-01-11 13:45:07 -08:00
Jon Leighton
7f7b480098 When assigning a has_one, if the new record fails to save, raise an error 2011-01-11 13:45:07 -08:00
Jon Leighton
1bc71ed960 When assigning a has_one, if the existing record fails to be removed from the association, raise an error 2011-01-11 13:45:06 -08:00
Jon Leighton
c47c541402 Have a separate test connection directory for sqlite3 in-memory so that the tests can be run without having to specifically rename the connection file (which then causes git to pick up the changes) 2011-01-11 13:45:06 -08:00
Jon Leighton
80df74bf51 Enable the sqlite3 in-memory test connection to work 2011-01-11 13:45:06 -08:00
Jon Leighton
c6e10b0f60 has_one should always remove the old record (properly), even if not saving the new record, so we don't get the database into a pickle 2011-01-11 13:45:06 -08:00
Ernie Miller
7d4d745730 Fix polymorphic belongs_to associationproxy raising errors when loading target. 2011-01-11 09:19:19 -08:00
Raimonds Simanovskis
f4f4964ce0 Always return decimal average of integer fields
In previous version if database adapter (e.g. SQLite and Oracle) returned non-String calculated values then type_cast_using_column converted decimal average value of intefer field to integer value. Now operation parameter is always checked to decide which conversion of calculated value should be done.
2011-01-10 15:51:32 -08:00
Aaron Patterson
0616585619 use SQLite3::VERSION rather than the deprecated class 2011-01-10 11:40:19 -08:00
Jeremy Kemper
63ed6ca998 Add test for e0e3adf 2011-01-07 16:25:25 -08:00
Jon Leighton
5b28e52542 Don't not remove double negatives 2011-01-07 15:03:16 -08:00
Jon Leighton
770e6893b9 Construct an actual ActiveRecord::Relation object for the association scope, rather than a hash which is passed to apply_finder_options. This allows more flexibility in how the scope is created, for example because scope.where(a, b) and scope.where(a).where(b) mean different things. 2011-01-07 15:03:15 -08:00
Jon Leighton
441118458d Use encode_with for marshalling 2011-01-07 15:03:15 -08:00
Aaron Patterson
2efd780dcb send() will raise an ArgumentError, so we should leverage ruby 2011-01-07 14:30:20 -08:00
Aaron Patterson
839f3bf682 just use a hash for doing association caching 2011-01-07 11:28:11 -08:00
Aaron Patterson
eba8411652 adding an encode_with method for Psych dump/load methods 2011-01-05 14:59:19 -08:00
Aaron Patterson
97bc74c746 make sure new objects can round trip 2011-01-05 14:03:13 -08:00
Aaron Patterson
6d74710828 make sure that Psych can roundtrip an AR object 2011-01-05 14:01:47 -08:00
Aaron Patterson
3f4143eedb fixing merge errors 2011-01-04 15:16:56 -08:00
Jon Leighton
40afcade0d Remove undocumented feature from has_one where you could pass false as the second parameter to build_assoc or create_assoc, and the existing associated object would be untouched (the foreign key would not be nullified, and it would not be deleted). If you want behaviour similar to this you can do the following things:
* Use :dependent => :nullify (or don't specify :dependent) if you want to prevent the existing associated object from being deleted
* Use has_many if you actually want multiple associated objects
* Explicitly set the foreign key if, for some reason, you really need to have multiple objects associated with the same has_one. E.g.

    previous = obj.assoc
    obj.create_assoc
    previous.update_attributes(:obj_id => obj.id)
2011-01-03 16:24:32 -08:00
Jon Leighton
2120da7f73 ActiveRecord::Relation#primary_key should return a string, just like ActiveRecord::Base.primary_key does. 2011-01-03 16:24:32 -08:00
Jon Leighton
0619dc2319 Implement deprecated version of AssociationReflection#primary_key_name, which has been renamed to #foreign_key. Also bumping the deprecation_horizon in Active Support to 3.1. 2011-01-03 16:24:32 -08:00
Jon Leighton
a9bed985cf When preloading a belongs_to, the target should still be set (to nil) if there is no foreign key present. And the loaded flag should be set on the association proxy. This then allows us to remove the foreign_key_present? check from BelongsToAssociation#find_target. Also added a test for the same thing on polymorphic associations. 2011-01-03 16:24:32 -08:00
Jon Leighton
1313d386da Make Relation#create_with always merge rather than overwrite, not just when merging two relations. If you wish to overwrite, you can do relation.create_with(nil), or for a specific attribute, relation.create_with(:attr => nil). 2011-01-03 16:24:32 -08:00
Jon Leighton
3103296a61 Let AssociationCollection#find use #scoped to do its finding. Note that I am removing test_polymorphic_has_many_going_through_join_model_with_disabled_include, since this specifies different behaviour for an association than for a regular scope. It seems reasonable to expect scopes and association proxies to behave in roughly the same way rather than having subtle differences. 2011-01-03 16:24:31 -08:00
Jon Leighton
d6289aadce Fix test_any in relations_test.rb, which was failing when relations_test.rb is run on its own (it passes when the entire suite is run). This is a hacky fix for a problem I didn't quite get to the bottom of, so I'd welcome a better solution... 2011-01-03 16:24:31 -08:00
Jon Leighton
a0be389d39 Allow assignment on has_one :through where the owner is a new record [#5137 state:resolved]
This required changing the code to keep the association proxy for a belongs_to around, despite its target being nil. Which in turn required various changes to the way that stale target checking is handled, in order to support various edge cases (loaded target is nil then foreign key added, foreign key is changed and then changed back, etc). A side effect is that the code is nicer and more succinct.

Note that I am removing test_no_unexpected_aliasing since that is basically checking that the proxy for a belongs_to *does* change, which is the exact opposite of the intention of this commit. Also adding various tests for various edge cases and related things.

Phew, long commit message!
2011-01-03 16:24:31 -08:00
Jon Leighton
c47f802d0e Have a proper AssociationReflection#foreign_type method rather than using options[:foreign_type] 2011-01-03 16:24:31 -08:00
Jon Leighton
16065b4f19 Some basic tests for the :foreign_type option on belongs_to, which was previously completely untested. 2011-01-03 16:24:30 -08:00
Aaron Patterson
60cf65def8 herp derpricating add_limit_offset! 2011-01-03 15:59:13 -08:00
Robert Pankowecki (Gavdi)
febdf5a5a9 Added one more failing test for bug #6036 2011-01-04 01:21:25 +08:00
Jon Leighton
1267598881 Rename AssociationReflection#primary_key_name to foreign_key, since the options key which it relates to is :foreign_key 2010-12-31 20:00:45 +00:00
Jon Leighton
3c400627eb Support for :counter_cache on polymorphic belongs_to 2010-12-31 20:00:44 +00:00
Jon Leighton
bea4065d3c Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociation to inherit from it 2010-12-31 20:00:44 +00:00
Jon Leighton
62b084f807 Specify the STI type condition using SQL IN rather than a whole load of ORs. Required a fix to ActiveRecord::Relation#merge for properly merging create_with_value. This also fixes a situation where the type condition was appearing twice in the resultant SQL query. 2010-12-31 20:00:44 +00:00
Jon Leighton
2bf3186803 Verify that when has_many associated objects are destroyed via :dependent => :destroy, when the parent is destroyed, the callbacks are run 2010-12-31 20:00:44 +00:00
Jon Leighton
38fbfa6390 Refactor configure_dependency_for_has_many to use AssociationCollection#delete_all. It was necessary to change test_before_destroy in lifecycle_test.rb so that it checks topic.replies.size *before* doing the destroy, as afterwards it will now (correctly) be 0. 2010-12-31 20:00:44 +00:00
Pratik Naik
573fd39e22 Make sure Model#touch doesn't try to update non existing columns 2010-12-30 18:42:18 +00:00
Pratik Naik
fd1cf13f74 Make serialized fixtures work again 2010-12-29 16:15:45 +00:00
Santiago Pastorino
304d38c053 Allow primary_key to be an attribute when the model is a new record 2010-12-28 10:11:27 -02:00
Aaron Patterson
7e91ad3f89 stop calling deprecated apis 2010-12-26 19:56:18 -07:00
Jon Leighton
e8ada11aac Associations: DRY up the code which is generating conditions, and make it all use arel rather than SQL strings 2010-12-26 19:38:04 +00:00
Jon Leighton
f2230c06ed Fix dodgy tests which were effectively asserting nil == nil 2010-12-26 19:38:04 +00:00