Fixes skipping object callback filters #10034

Merged
merged 1 commit into from Apr 4, 2013

2 participants

@benmcredmond

This allows you to skip callbacks that are defined by objects, e.g. for
ActionController:

skip_after_filter MySpecialFilter

Previously this didn't work due to a bug in how Rails compared callbacks
in Callback#matches?. When a callback is compiled, if it's an object
filter (i.e. not a method, proc, etc.), Callback now defines a method on
@klass that is derived from the class name rather than @callback_id.
So, when skip_callback tries to find the appropriate callback to
remove, Callback can regenerate the method name for the filter
object and return the correct value for Callback#matches?.

@josevalim josevalim and 1 other commented on an outdated diff Apr 1, 2013
activesupport/lib/active_support/callbacks.rb
@@ -234,6 +236,14 @@ def recompile_options!
@compiled_options = conditions.flatten.join(" && ")
end
+ def _method_name_for_object_filter(kind, filter)
+ class_name = filter.kind_of?(Class) ? filter.to_s : filter.class.to_s
+ class_name.gsub!(/<|>|#/, '')
+ class_name.gsub!(/\/|:/, "_")
+
+ "_callback_#{kind}_#{class_name.underscore}"
@josevalim
Ruby on Rails member

We don't need to underscore it, do we? Just pass the full class name.

you're right. changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@rafaelfranca
Ruby on Rails member

Could you add a CHANGELOG entry?

@rafaelfranca rafaelfranca and 1 other commented on an outdated diff Apr 4, 2013
activesupport/lib/active_support/callbacks.rb
@@ -255,6 +264,8 @@ def recompile_options!
# a method is created that calls the before_foo method
# on the object.
def _compile_filter(filter)
+ @is_object_filter = false
@rafaelfranca
Ruby on Rails member

Since it is internal could you call it @_is_object_filter?

sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@benmcredmond benmcredmond Fixes skipping object callback filters
This allows you to skip callbacks that are defined by objects, e.g. for
`ActionController`:

    skip_after_filter MySpecialFilter

Previously this didn't work due to a bug in how Rails compared callbacks
in `Callback#matches?`. When a callback is compiled, if it's an object
filter (i.e. not a method, proc, etc.), `Callback` now defines a method on
`@klass` that is derived from the class name rather than `@callback_id`.
So, when `skip_callback` tries to find the appropriate callback to
remove, `Callback` can regenerate the method name for the filter
object and return the correct value for `Callback#matches?`.
e377228
@benmcredmond

@rafaelfranca made those changes.

@rafaelfranca rafaelfranca merged commit c79c698 into rails:master Apr 4, 2013
@rafaelfranca
Ruby on Rails member

This pull request broke the Rails tests. I'll revert it. Please fix the erros and submit another pull request. https://travis-ci.org/rails/rails/builds/6061839

@rafaelfranca rafaelfranca added a commit that referenced this pull request Apr 4, 2013
@rafaelfranca rafaelfranca Revert "Merge pull request #10034 from benofsky/fix_skipping_object_c…
…allback_filters"

This reverts commit c79c698, reversing
changes made to ba4c274.

This broke all the tests. See https://travis-ci.org/rails/rails/builds/6061839
49a0f55
@benmcredmond
@tenderlove tenderlove added a commit that referenced this pull request Apr 5, 2013
@tenderlove tenderlove Merge branch 'master' into railstest
* master: (44 commits)
  Improve the changelog entry [ci skip]
  Fix explicit names on multiple file fields
  Correctly parse bigint defaults in PostgreSQL
  Move changelog to the top [ci skip]
  Fix indent and remove extra white spaces
  Fix scope chaining + STI
  failing test for #9869
  Improve `belongs_to touch: true` timestamp test
  Sort modules in alphabetical order.
  Avoid an attempt to fetch old record when id was not present in touch callback
  Use the correct pk field from the reflected class to find the old record
  Refactor mail_to to not generate intermediate hashes when adding href
  Ensure mail_to helper does not modify the given html options hash
  Use inspect when writing the foreign key from the reflection
  Use a space after the comment sign when showing the result of commands
  Exclude template files for rdoc API [ci skip]
  template should have generic name
  use | to have more intent revealing code
  Revert "Merge pull request #10034 from benofsky/fix_skipping_object_callback_filters"
  stop depending on callbacks
  ...

Conflicts:
	railties/test/application/rake_test.rb
01034d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment