1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Simplify association proxy implementation by factoring construct_scope out of method_missing. Closes #6643.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5564 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2006-11-19 11:16:12 +00:00
parent ae8171ddc5
commit 02adc49d72
4 changed files with 25 additions and 30 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Simplify association proxy implementation by factoring construct_scope out of method_missing. #6643 [martin]
* Oracle: automatically detect the primary key. #6594 [vesaria, Michael Schoen]
* Oracle: to increase performance, prefetch 100 rows and enable similar cursor sharing. Both are configurable in database.yml. #6607 [philbogle@gmail.com, ray.fortna@jobster.com, Michael Schoen]

View file

@ -147,6 +147,19 @@ module ActiveRecord
end
protected
def method_missing(method, *args, &block)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
super
else
@reflection.klass.with_scope(construct_scope) { @reflection.klass.send(method, *args, &block) }
end
end
# overloaded in derived Association classes to provide useful scoping depending on association type.
def construct_scope
{}
end
def reset_target!
@target = Array.new
end

View file

@ -86,16 +86,6 @@ module ActiveRecord
alias :concat_with_attributes :push_with_attributes
protected
def method_missing(method, *args, &block)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
super
else
@reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
@reflection.klass.send(method, *args, &block)
end
end
end
def count_records
load_target.size
end
@ -158,6 +148,10 @@ module ActiveRecord
@join_sql = "INNER JOIN #{@reflection.options[:join_table]} ON #{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{@reflection.options[:join_table]}.#{@reflection.association_foreign_key}"
end
def construct_scope
{ :find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false } }
end
# Join tables with additional columns on top of the two foreign keys must be considered ambigious unless a select
# clause has been explicitly defined. Otherwise you can get broken records back, if, say, the join column also has
# and id column, which will then overwrite the id column of the records coming back.

View file

@ -93,26 +93,6 @@ module ActiveRecord
end
protected
def method_missing(method, *args, &block)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
super
else
create_scoping = {}
set_belongs_to_association_for(create_scoping)
@reflection.klass.with_scope(
:create => create_scoping,
:find => {
:conditions => @finder_sql,
:joins => @join_sql,
:readonly => false
}
) do
@reflection.klass.send(method, *args, &block)
end
end
end
def load_target
if !@owner.new_record? || foreign_key_present
begin
@ -205,6 +185,12 @@ module ActiveRecord
@counter_sql = @finder_sql
end
end
def construct_scope
create_scoping = {}
set_belongs_to_association_for(create_scoping)
{ :find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }, :create => create_scoping }
end
end
end
end