Removed support for deprecated delete_sql in associations.

This commit is contained in:
Neeraj Singh 2013-07-03 00:08:48 +05:30
parent f98d47b1ab
commit 8ef2463807
5 changed files with 14 additions and 45 deletions

@ -1,3 +1,7 @@
* Removed support for deprecated `delete_sql` in associations.
*Neeraj Singh*
* Removed support for deprecated `insert_sql` in associations. * Removed support for deprecated `insert_sql` in associations.
*Neeraj Singh* *Neeraj Singh*

@ -5,7 +5,7 @@ def macro
end end
def valid_options def valid_options
super + [:join_table, :association_foreign_key, :delete_sql] super + [:join_table, :association_foreign_key]
end end
def build def build

@ -35,22 +35,17 @@ def count_records
end end
def delete_records(records, method) def delete_records(records, method)
if sql = options[:delete_sql] relation = join_table
records = load_target if records == :all condition = relation[reflection.foreign_key].eq(owner.id)
records.each { |record| owner.class.connection.delete(interpolate(sql, record)) }
else
relation = join_table
condition = relation[reflection.foreign_key].eq(owner.id)
unless records == :all unless records == :all
condition = condition.and( condition = condition.and(
relation[reflection.association_foreign_key] relation[reflection.association_foreign_key]
.in(records.map { |x| x.id }.compact) .in(records.map { |x| x.id }.compact)
) )
end
owner.class.connection.delete(relation.where(condition).compile_delete)
end end
owner.class.connection.delete(relation.where(condition).compile_delete)
end end
def invertible_for?(record) def invertible_for?(record)

@ -351,31 +351,6 @@ def test_deleting_array
assert_equal 0, david.projects(true).size assert_equal 0, david.projects(true).size
end end
def test_deleting_with_sql
david = Developer.find(1)
active_record = Project.find(1)
active_record.developers.reload
assert_equal 3, active_record.developers_by_sql.size
active_record.developers_by_sql.delete(david)
assert_equal 2, active_record.developers_by_sql(true).size
end
def test_deleting_array_with_sql
active_record = Project.find(1)
active_record.developers.reload
assert_equal 3, active_record.developers_by_sql.size
active_record.developers_by_sql.delete(Developer.all)
assert_equal 0, active_record.developers_by_sql(true).size
end
def test_deleting_all_with_sql
project = Project.find(1)
project.developers_by_sql.delete_all
assert_equal 0, project.developers_by_sql.size
end
def test_deleting_all def test_deleting_all
david = Developer.find(1) david = Developer.find(1)
david.projects.reload david.projects.reload

@ -7,11 +7,6 @@ class Project < ActiveRecord::Base
has_and_belongs_to_many :developers_named_david, -> { where("name = 'David'").distinct }, :class_name => "Developer" has_and_belongs_to_many :developers_named_david, -> { where("name = 'David'").distinct }, :class_name => "Developer"
has_and_belongs_to_many :developers_named_david_with_hash_conditions, -> { where(:name => 'David').distinct }, :class_name => "Developer" has_and_belongs_to_many :developers_named_david_with_hash_conditions, -> { where(:name => 'David').distinct }, :class_name => "Developer"
has_and_belongs_to_many :salaried_developers, -> { where "salary > 0" }, :class_name => "Developer" has_and_belongs_to_many :salaried_developers, -> { where "salary > 0" }, :class_name => "Developer"
ActiveSupport::Deprecation.silence do
has_and_belongs_to_many :developers_by_sql, :class_name => "Developer", :delete_sql => proc { |record| "DELETE FROM developers_projects WHERE project_id = #{id} AND developer_id = #{record.id}" }
end
has_and_belongs_to_many :developers_with_callbacks, :class_name => "Developer", :before_add => Proc.new {|o, r| o.developers_log << "before_adding#{r.id || '<new>'}"}, has_and_belongs_to_many :developers_with_callbacks, :class_name => "Developer", :before_add => Proc.new {|o, r| o.developers_log << "before_adding#{r.id || '<new>'}"},
:after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || '<new>'}"}, :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || '<new>'}"},
:before_remove => Proc.new {|o, r| o.developers_log << "before_removing#{r.id}"}, :before_remove => Proc.new {|o, r| o.developers_log << "before_removing#{r.id}"},