Last active
December 14, 2015 21:39
-
-
Save alvinsj/5153101 to your computer and use it in GitHub Desktop.
custom liquid filter + drop
This file contains hidden or 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
class Data | |
attr_accessor :date_range | |
def initialize(type) | |
@type = type | |
end | |
def graph_points | |
SomeClass.graph_points( | |
@type, | |
date_between: @date_range) | |
end | |
def to_liquid | |
self | |
end | |
end |
This file contains hidden or 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
Liquid::Template.register_filter(StatsFilter) | |
Liquid::Template. | |
parse("$.draw_graph({{stats.users | date_range: options.date_start, options.date_end | compute }})"). | |
render('stats' => StatsDrop.new, 'options' => OptionsDrop.new(params)) |
This file contains hidden or 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
class OptionsDrop < Liquid::Drop | |
def date_start | |
Date.today - 3.month | |
end | |
def date_end | |
Date.today | |
end | |
end |
This file contains hidden or 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
class StatsDrop < Liquid::Drop | |
def users | |
Data.new("User") | |
end | |
end |
This file contains hidden or 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 StatsFilter | |
def date_range(input, *options) | |
input.date_range = options[0]..options[1] | |
input | |
end | |
def compute(input) | |
input.graph_points.to_json | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment