Skip to content

Instantly share code, notes, and snippets.

@pacifists
Forked from did/gist:1110642
Created October 7, 2011 06:25
Show Gist options
  • Save pacifists/1269595 to your computer and use it in GitHub Desktop.
Save pacifists/1269595 to your computer and use it in GitHub Desktop.
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