-
-
Save pacifists/1269595 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyApp | |
class IncViewsCounterFor < ::Liquid::Tag | |
Syntax = /(#{::Liquid::Expression}+)?/ | |
def initialize(tag_name, markup, tokens, context) | |
if markup =~ Syntax | |
@name = $1 | |
else | |
raise ::Liquid::SyntaxError.new("Syntax Error in 'inc_views_counter_for' - Valid syntax: inc_views_counter_for <content>") | |
end | |
super | |
end | |
def render(context) | |
model = context[@name] # returns a liquid drop object (Locomotive::Liquid::Drops:Content) | |
model._source.inc(:views_counter, 1) # mongoid command | |
'' # returns an empty string since this tag does not have to write to the output | |
end | |
end | |
::Liquid::Template.register_tag('inc_views_counter_for', IncViewsCounterFor) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment