Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6542 (new enhancement)

Opened 2 years ago

Last modified 8 months ago

[PATCH] Create injection points in sql-creation methods

Reported by: ymendel Assigned to: David
Priority: normal Milestone: 2.x
Component: ActiveRecord Version:
Severity: normal Keywords: unverified
Cc:

Description

The update method presently creates an SQL statement and passes it to the connection adapter in one fell swoop. This requires a rewrite (usually mostly copy/paste) of the method if one wishes to modify the behavior in some way, such as for scoping or to limit the number of attributes that appear in the SET clause (see ticket #5961 or the acts_as_changed/acts_as_modified plugins at http://www.agilewebdevelopment.com/plugins/owner/119). This patch simply breaks the operation into smaller chunks that be easily modified.

From

# Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows.
def update
  connection.update(
    "UPDATE #{self.class.table_name} " +
    "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " +
    "WHERE #{self.class.primary_key} = #{quote_value(id)}",
    "#{self.class.name} Update"
  )
end

To

def attributes_for_update
  attributes_with_quotes(false)
end

def sql_for_update
  "UPDATE #{self.class.table_name} " +
  "SET #{quoted_comma_pair_list(connection, attributes_for_update)} " +
  "WHERE #{self.class.primary_key} = #{quote_value(id)}"
end

# Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows.
def update
  connection.update(sql_for_update, "#{self.class.name} Update")
end

This concept can also be carried over to the insert and delete statements, of course.

Attachments

update_injection_points.diff (1.1 kB) - added by ymendel on 11/03/06 15:53:03.
more_injection_points.diff (4.1 kB) - added by ymendel on 11/07/06 13:54:00.
sql_creation_injection_points.diff (4.5 kB) - added by ymendel on 09/25/07 15:22:06.
Updated diff (some changes happen in 11 months)

Change History

11/03/06 15:53:03 changed by ymendel

  • attachment update_injection_points.diff added.

11/07/06 13:53:50 changed by ymendel

Took the code from the patch I'm using on my own system and applied it to trunk. This splits update, destroy, and create (both instance and class) into manageable chucnks.

11/07/06 13:54:00 changed by ymendel

  • attachment more_injection_points.diff added.

09/25/07 14:17:41 changed by ymendel

  • keywords set to unverified.

09/25/07 14:20:26 changed by rickbradley

+1

09/25/07 15:22:06 changed by ymendel

  • attachment sql_creation_injection_points.diff added.

Updated diff (some changes happen in 11 months)

09/25/07 15:22:52 changed by ymendel

Added updated patch. The latest one (sql_creation_injection_points.diff) is the one to use.

09/25/07 15:23:42 changed by ymendel

  • summary changed from [PATCH] Create injection points in update method to [PATCH] Create injection points in sql-creation methods.

09/25/07 22:01:46 changed by feldpost

+1

09/26/07 09:44:02 changed by lifofifo

Looks good at first sight. Will post my vote soon after having a closer look at the code.