diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-02-21 01:09:41 +0000 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-21 10:16:15 -0800 |
commit | 52f8e4b9dae6137fcd95793dffc26ddff80b623e (patch) | |
tree | 11a80259a618472810155ca46fd826246aab9906 /activerecord/lib/active_record/associations/collection_association.rb | |
parent | a5274bb52c058bae69476bee3c95f472513a5725 (diff) | |
download | rails-52f8e4b9dae6137fcd95793dffc26ddff80b623e.tar.gz rails-52f8e4b9dae6137fcd95793dffc26ddff80b623e.tar.bz2 rails-52f8e4b9dae6137fcd95793dffc26ddff80b623e.zip |
Use proper objects to do the work to build the associations (adding methods, callbacks etc) rather than calling a whole bunch of methods with rather long names.
Diffstat (limited to 'activerecord/lib/active_record/associations/collection_association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index ba98542b67..f3761bd2c7 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -32,6 +32,45 @@ module ActiveRecord @proxy = CollectionProxy.new(self) end + # Implements the reader method, e.g. foo.items for Foo.has_many :items + def reader(force_reload = false) + if force_reload + klass.uncached { reload } + elsif stale_target? + reload + end + + proxy + end + + # Implements the writer method, e.g. foo.items= for Foo.has_many :items + def writer(records) + replace(records) + end + + # Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items + def ids_reader + if loaded? || options[:finder_sql] + load_target.map do |record| + record.send(reflection.association_primary_key) + end + else + column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}" + + scoped.select(column).except(:includes).map! do |record| + record.send(reflection.association_primary_key) + end + end + end + + # Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items + def ids_writer(ids) + pk_column = reflection.primary_key_column + ids = Array.wrap(ids).reject { |id| id.blank? } + ids.map! { |i| pk_column.type_cast(i) } + replace(klass.find(ids).index_by { |r| r.id }.values_at(*ids)) + end + def reset @loaded = false @target = [] |