Skip to content

Instantly share code, notes, and snippets.

@Insood
Last active June 4, 2018 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Insood/b50331ce313477cb06111435523ffa5b to your computer and use it in GitHub Desktop.
Save Insood/b50331ce313477cb06111435523ffa5b to your computer and use it in GitHub Desktop.
module IncludableOne
def self.included(base)
base.register_on_save :includable_one_saved
end
def includable_one_saved
puts "Saved One"
end
end
module IncludableTwo
def self.included(base)
base.register_on_save :includable_two_saved
end
def includable_two_saved
puts "Saved Two"
end
end
class MyBaseClass
include ActiveModel::Model
def initialize
@callbacks = []
end
def register_on_save(callback)
@callbacks << callback
end
def save
@callbacks.each do |callback|
send(callbak)
end
end
end
class MyDerivedClassOne < MyBaseClass
include IncludableOne
include IncludableTwo
end
class MyDerivedClassTwo < MyBaseClass
include IncludableOne
end
derived1 = MyDerivedClassOne.new()
derived1.save
derived2 = MyDerivedClassTwo.new()
derived2.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment