Skip to content

Commit

Permalink
Move destroy to Relation
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo committed Jan 20, 2010
1 parent 223e2a2 commit 9756805
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
29 changes: 1 addition & 28 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -556,7 +556,7 @@ def colorize_logging(*args)
end
alias :colorize_logging= :colorize_logging

delegate :find, :first, :last, :all, :destroy_all, :exists?, :delete, :to => :scoped
delegate :find, :first, :last, :all, :destroy, :destroy_all, :exists?, :delete, :to => :scoped
delegate :select, :group, :order, :limit, :joins, :where, :preload, :eager_load, :includes, :from, :lock, :readonly, :having, :to => :scoped
delegate :count, :average, :minimum, :maximum, :sum, :calculate, :to => :scoped

Expand Down Expand Up @@ -646,33 +646,6 @@ def update(id, attributes)
end
end

# Destroy an object (or multiple objects) that has the given id, the object is instantiated first,
# therefore all callbacks and filters are fired off before the object is deleted. This method is
# less efficient than ActiveRecord#delete but allows cleanup methods and other actions to be run.
#
# This essentially finds the object (or multiple objects) with the given id, creates a new object
# from the attributes, and then calls destroy on it.
#
# ==== Parameters
#
# * +id+ - Can be either an Integer or an Array of Integers.
#
# ==== Examples
#
# # Destroy a single object
# Todo.destroy(1)
#
# # Destroy multiple objects
# todos = [1,2,3]
# Todo.destroy(todos)
def destroy(id)
if id.is_a?(Array)
id.map { |one_id| destroy(one_id) }
else
find(id).destroy
end
end

# Updates all records with details given if they match a set of conditions supplied, limits and order can
# also be supplied. This method constructs a single SQL UPDATE statement and sends it straight to the
# database. It does not instantiate the involved models and it does not trigger Active Record callbacks
Expand Down
27 changes: 27 additions & 0 deletions activerecord/lib/active_record/relation.rb
Expand Up @@ -119,6 +119,33 @@ def destroy_all(conditions = nil)
end
end

# Destroy an object (or multiple objects) that has the given id, the object is instantiated first,
# therefore all callbacks and filters are fired off before the object is deleted. This method is
# less efficient than ActiveRecord#delete but allows cleanup methods and other actions to be run.
#
# This essentially finds the object (or multiple objects) with the given id, creates a new object
# from the attributes, and then calls destroy on it.
#
# ==== Parameters
#
# * +id+ - Can be either an Integer or an Array of Integers.
#
# ==== Examples
#
# # Destroy a single object
# Todo.destroy(1)
#
# # Destroy multiple objects
# todos = [1,2,3]
# Todo.destroy(todos)
def destroy(id)
if id.is_a?(Array)
id.map { |one_id| destroy(one_id) }
else
find(id).destroy
end
end

def delete_all
arel.delete.tap { reset }
end
Expand Down

0 comments on commit 9756805

Please sign in to comment.