I'd like to override a gem method (a Jekyll extension) that looks like this:
File: lib/jekyll-amazon/amazon_tag.rb
.
module Jekyll
module Amazon
class AmazonTag < Liquid::Tag
def detail(item)
...
end
end
end
end
Liquid::Template.register_tag('amazon', Jekyll::Amazon::AmazonTag)
I have placed code with the same structure in my project in the folder config/initializers/presentation.rb
_plugins/presentation.rb
. If I change the name of the method detail
to a new name, it works, but I can't get it to override the name detail
.
What have I done wrong?
(Note: In version 0.2.2 of the jekyll-amazon gem, the detail
method is private; I have changed this locally so that the method is no longer private.)
There are two ways to install Jekyll plugins: This is the easiest method. We simply download a plugin to the _plugins directory on our site. In this example we’ll add a plugin for calculating an estimated read time of a piece of content. First let’s create _plugins/reading_time.rb and adding the following source code (credit zachleat ):
Jekyll gives a special treatment to gems listed as part of the :jekyll_plugins group in a Gemfile. Any gem under this group is loaded at the very beginning of any Jekyll process, irrespective of the --safe CLI flag or entries in the configuration file (s).
You can still use GitHub Pages to publish your site, but you’ll need to build the site locally and push the generated files to your GitHub repository instead of the Jekyll source files. You may use any of the aforementioned plugin routes simultaneously in the same site if you so choose.
jekyll-spaceship - advanced example. Provides powerful supports for table, mathjax, plantuml, video, etc. A boolean flag that informs Jekyll whether this plugin may be safely executed in an environment where arbitrary code execution is not allowed.
You can use alias_method
module Jekyll
module Amazon
class AmazonTag < Liquid::Tag
alias_method :old_detail, :detail
def detail(item)
# do your stuff here
# eventually pass your stuff to old method
old_detail(item)
end
end
end
end
Liquid::Template.register_tag('amazon', Jekyll::Amazon::AmazonTag)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With