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

Ticket #10727 (closed enhancement: fixed)

Opened 4 months ago

Last modified 4 months ago

[PATCH] ActiveSupport::Callbacks

Reported by: josh Assigned to: bitsweat
Priority: normal Milestone: 2.1
Component: ActiveSupport Version: edge
Severity: normal Keywords:
Cc: chuyeow

Description

Extract common callback code from ActiveRecord::Callbacks, ActiveRecord::Validations, ActiveSupport::Testing::SetupAndTeardown, etc into ActiveSupport::Callbacks.

Attachments

activesupport_callbacks.diff (19.7 kB) - added by josh on 01/10/08 15:20:06.
activesupport_callbacks.2.diff (24.8 kB) - added by josh on 01/13/08 06:11:26.

Change History

01/06/08 19:30:51 changed by josh

In the current patch, AR tests aren't passing. Something to do with the SetupAndTeardown code not working. Still in the works and kind of experimental.

01/06/08 20:15:40 changed by bitsweat

Good start. I'd like to move away from inheritable_attribute though in favor of piggybacking on existing Ruby method calling semantics. I was thinking of extracting the pattern I used here: simple instance variables with delegation to superclass, so changing the superclass attribute after subclassing still affects the subclasses. With inheritable_attribute, the value is copied when subclassed so changes don't propagate. See superclass_delegating_attribute in Class core_ext and attr_accessor_with_default in Module core_ext for similar ideas.

01/06/08 22:56:28 changed by josh

Yep, and AR tests pass again (as well as everything else).

I didn't tackle AR::Validations yet. It follows a similar pattern as callbacks but has a few exceptions.

Maybe this is a different ticket, well should be posted in the mailing list first but, I'd like to see the method callback technique deprecated. eg.

def after_save
  "stuff"
end

def validate
  "stuff"
end

and use this instead

after_save do |r|
  "stuff"
end

validate do |r|
  "stuff"
end

01/07/08 02:07:02 changed by chuyeow

  • cc set to chuyeow.

01/10/08 02:27:57 changed by josh

jeremy, ready to commit unless anyone else has any objections.

01/10/08 03:09:37 changed by bitsweat

  • milestone changed from 2.x to 2.1.

I like it. I was initially concerned that a single implementation wouldn't work for all these cases, but it appears it does. We'll have to expose the run_callbacks termination condition eventually since false return values doesn't apply to controller filters.

01/10/08 15:20:06 changed by josh

  • attachment activesupport_callbacks.diff added.

01/10/08 15:22:14 changed by josh

I'm not sure if I'm going be able to monkey filters around ActiveSupport::Callbacks. Its a really different code base. I'll give it a try later today.

By "termination condition" do you mean something like this, "run_callbacks(callbacks, :halt_on => false)"?

01/13/08 05:26:38 changed by josh

O, found another. Refactored dispatcher with ActiveSupport::Callbacks and added a termination condition option to run_callbacks.

01/13/08 06:11:26 changed by josh

  • attachment activesupport_callbacks.2.diff added.

01/19/08 02:44:49 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [8664]) Extract ActiveSupport::Callbacks from Active Record, test case setup and teardown, and ActionController::Dispatcher. Closes #10727.