0

I want to execute a method before each class method. Is it possible to do this using ActiveModel callbacks or do i have to use metaprogramming ?

I tried :

class Image
  extend ActiveModel::Callbacks

  define_model_callbacks :create
  before_create :set_base_uri

  def self.create
    run_callbacks :create do
      ..my_code..
    end
  end

  def set_base_uri
    ..my_code..
  end
end

When i call Image.create i get

undefined method `run_callbacks' for Image:Class

The same code works if i change create to an instance method. I looked at the documentation but they don't mention class methods, does ActiveModel support class methods callbacks ?

vdaubry
  • 11,369
  • 7
  • 54
  • 76

1 Answers1

0

i don't think callback can work, as the callback method is invoked in the instance level. Use an alias_method may works.

nickcen
  • 1,682
  • 10
  • 10