Git Product home page Git Product logo

Comments (7)

fatkodima avatar fatkodima commented on September 25, 2024 2

Valid.

from rails.

smtlaissezfaire avatar smtlaissezfaire commented on September 25, 2024 1

Here's some code to do this: just add to config/initializers/action_mailer_enhanced_hooks.rb:

module ActionMailerEnhancedHooks
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def after_deliver(method_name, options = {})
      new_method_name = generate_method_name(method_name, options)

      condition = if options[:only]
                    "Array(#{options[:only].inspect}).include?(action_name.to_sym)"
                  elsif options[:except]
                    "!Array(#{options[:except].inspect}).include?(action_name.to_sym)"
                  else
                    "true"
                  end

      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{new_method_name}(*args)
          return unless #{condition}
          #{method_name}(*args)
        end
      RUBY

      super(new_method_name)
    end

  private

    def generate_method_name(method_name, options)
      suffix = if options[:only]
                 "_only_#{Array(options[:only]).join('_')}"
               elsif options[:except]
                 "_except_#{Array(options[:except]).join('_')}"
               else
                 ""
               end
      "#{method_name}#{suffix}".to_sym
    end
  end
end

ActionMailer::Base.include ActionMailerEnhancedHooks

Now you can use only and except in your hooks.

from rails.

fatkodima avatar fatkodima commented on September 25, 2024

Delivery callbacks were added in #47630 (cc @bensheldon).

The feature request looks legitimate for me and I am willing to help by creating a PR. But, was there a reason to not add these options back then?

from rails.

bensheldon avatar bensheldon commented on September 25, 2024

Those callback options being absent was unintentional. I think that makes sense for the _delivery callbacks to be consistent with _action callbacks.

Also, revisiting my implementation in light of this, I wonder if there is a better implementation for _delivery callbacks rather than including ActiveSupport::Callbacks and maybe they should have been created with something in AbstractController instead

from rails.

rails-bot avatar rails-bot commented on September 25, 2024

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-1-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

from rails.

smtlaissezfaire avatar smtlaissezfaire commented on September 25, 2024

Haven't tested but it would be great to have this feature OR document that only / except callbacks don't work.

from rails.

smtlaissezfaire avatar smtlaissezfaire commented on September 25, 2024

hacky way to currently do this currently is to do return unless action_name == :the_method_name in the callback method.

from rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.