Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:40:02+00:00 2026-05-30T08:40:02+00:00

I have a Model , which has method_1 to method_10 . I also have

  • 0

I have a Model, which has method_1 to method_10. I also have ModelObserver.
I would like to notifiy ModelObserver before invoking method1 to method_9, but not method_10.

Is there a DRY way to write this, instead of repeating notify_observers(:after_something) in all 9 methods?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:40:03+00:00Added an answer on May 30, 2026 at 8:40 am

    Add a file called monkey_patches.rb in config/initializers dirctory.

    class Object
      def self.method_hook(*args)
        options = args.extract_options!
        return unless (options[:before].present? or options[:after].present?)
        args.each do |method_name|      
          old_method = instance_method(method_name) rescue next
    
          define_method(method_name) do |*args|
            # invoke before callback
            if options[:before].present?
              options[:before].is_a?(Proc) ? options[:before].call(method_name, self):
                send(options[:before], method_name) 
            end
    
            # you can modify the code to call after callback 
            # only when the old method returns true etc..
            old_method.bind(self).call(*args)
    
            # invoke after callback
            if options[:after].present?
              options[:after].is_a?(Proc) ? options[:after].call(method_name, self): 
                send(options[:after], method_name) 
            end
          end
        end
      end
    end  
    

    The patch enables you to add before and after callbacks on an instance method of a class. A hook can be:

    • The name of an instance method which accepts one parameter
    • A lambda accepting two parameters

    Multiple hooks can be registered on a same method. The method being hooked should come before the hook.

    E.g:

    class Model < ActiveRecord::Base
    
      def method1
      end
    
      def method2
      end
    
      def method3
      end
    
      def method4
      end
    
      def update_cache
      end
    
      # instance method name as `after` callback parameter
      method_hook :method1, :method2, :after => :update_cache
    
      # lambda as `before` callback parameter
      method_hook :method1, :method2, 
        :before => lambda{|name, record| p name;p record}
    
      # lambda as `after` callback parameter
      method_hook :method3, :method4, 
        :after => lambda{|name, record| 
           Model2.increment_counter(:post_count, record.model2_id)}
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ActiveRecord model, Foo , which has a name field. I'd like
I have model Foo which has field bar. The bar field should be unique,
I'm using ASP.NET MVC and have a model which has an image (byte array)
I have an Entry model which has many Tag s. Tag s are added
I have a model called Contact which has_one guest like so. class Contact <
I have a model called Answer which has a ForeignKey relationship to another model
I have a fairly involved managed data model which has a central object with
Say I have a model object 'Person' defined, which has a field called 'Name'.
In my domain model I have an abstract class CommunicationChannelSpecification, which has child classes
I have a model called Category which looks like this: class Category < ActiveRecord::Base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.