diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-04-06 02:18:42 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-04-06 02:18:42 +0000 |
commit | 9bc75fd007d56d818b8620569410a20aa92c9fc5 (patch) | |
tree | 750bf397569d4fdf20eb6285d68330b9efe91a56 /activerecord/lib/active_record/associations/has_many_through_association.rb | |
parent | f6b12c11cd3a6df8525dd16ec093ec473813489e (diff) | |
download | rails-9bc75fd007d56d818b8620569410a20aa92c9fc5.tar.gz rails-9bc75fd007d56d818b8620569410a20aa92c9fc5.tar.bz2 rails-9bc75fd007d56d818b8620569410a20aa92c9fc5.zip |
Remove duplicate code from associations. [Pratik]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9231 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations/has_many_through_association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 70 |
1 files changed, 10 insertions, 60 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 23cead3ca6..ebea313c18 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -2,37 +2,8 @@ module ActiveRecord module Associations class HasManyThroughAssociation < HasManyAssociation #:nodoc: def initialize(owner, reflection) - super reflection.check_validity! - @finder_sql = construct_conditions - end - - - def find(*args) - options = args.extract_options! - - conditions = "#{@finder_sql}" - if sanitized_conditions = sanitize_sql(options[:conditions]) - conditions << " AND (#{sanitized_conditions})" - end - options[:conditions] = conditions - - if options[:order] && @reflection.options[:order] - options[:order] = "#{options[:order]}, #{@reflection.options[:order]}" - elsif @reflection.options[:order] - options[:order] = @reflection.options[:order] - end - - options[:select] = construct_select(options[:select]) - options[:from] ||= construct_from - options[:joins] = construct_joins(options[:joins]) - options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil? - - merge_options_from_reflection!(options) - - # Pass through args exactly as we received them. - args << options - @reflection.klass.find(*args) + super end alias_method :new, :build @@ -59,15 +30,6 @@ module ActiveRecord return @target.size if loaded? return count end - - # Calculate sum using SQL, not Enumerable - def sum(*args) - if block_given? - calculate(:sum, *args) { |*block_args| yield(*block_args) } - else - calculate(:sum, *args) - end - end def count(*args) column_name, options = @reflection.klass.send(:construct_count_options_from_args, *args) @@ -79,8 +41,14 @@ module ActiveRecord @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) } end - protected + def construct_find_options!(options) + options[:select] = construct_select(options[:select]) + options[:from] ||= construct_from + options[:joins] = construct_joins(options[:joins]) + options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil? + end + def insert_record(record, force=true) if record.new_record? if force @@ -101,26 +69,6 @@ module ActiveRecord end end - def method_missing(method, *args) - if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) - if block_given? - super { |*block_args| yield(*block_args) } - else - super - end - elsif @reflection.klass.scopes.include?(method) - @reflection.klass.scopes[method].call(self, *args) - else - with_scope construct_scope do - if block_given? - @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } - else - @reflection.klass.send(method, *args) - end - end - end - end - def find_target @reflection.klass.find(:all, :select => construct_select, @@ -237,6 +185,8 @@ module ActiveRecord @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" @finder_sql << " AND (#{conditions})" if conditions + else + @finder_sql = construct_conditions end if @reflection.options[:counter_sql] |