I want to create a gem which adds some functionality to my models. How to define a custom keyword for a Mongoid model? E.g.:
class Book
include Mongoid::Document
has_my_awesome_functionality
end
I want to create a gem which adds some functionality to my models. How to define a custom keyword for a Mongoid model? E.g.:
class Book
include Mongoid::Document
has_my_awesome_functionality
end
If you have a gem called foobar
you can put the following into your gem's initialiser, or in a separate file (usually called railtie.rb
[, just make sure it's being loaded]):
require 'foobar'
require 'rails'
class FooBar
class Railtie < Rails::Railtie
config.before_initialize do
::Mongoid::Document.module_eval do
def self.included(base)
base.extend FooBar::MongoidExtension
end
end
end if defined?(Mongoid)
end
and your extension can look like this:
module Foobar::MongoidExtension
def has_my_awesome_functionality
# logic here
end
end