class Locomotive::Steam::Liquid::Tags::Action

Execute javascript code server side. The API allows you to:

Usage:

{% action “My javascript action” %}

var lastPost = allEntries('posts', { 'posted_at.lte': getProp('today'), published: true, order_by: 'posted_at desc' })[0];
var views = lastPost.views + 1;

updateEntry('posts', lastPost._id, { views: views });

setProp('views', views);

{% endaction %}

<p>Number of views for the last published post: {{ views }}</p>

Constants

Syntax

Public Class Methods

new(tag_name, markup, options) click to toggle source
Calls superclass method
# File lib/locomotive/steam/liquid/tags/action.rb, line 30
def initialize(tag_name, markup, options)
  if markup =~ Syntax
    @description = $1.to_s
  else
    raise ::Liquid::SyntaxError.new("Syntax Error in 'action' - Valid syntax: action \"<description>\"")
  end
  super
end

Public Instance Methods

render(context) click to toggle source
Calls superclass method
# File lib/locomotive/steam/liquid/tags/action.rb, line 39
def render(context)
  Locomotive::Common::Logger.info "[action] executing #{@description}"
  begin
    service(context).run(super, safe_params(context), context)
  rescue Locomotive::Steam::ActionError => e
    e.action = @description
    raise e
  end
  ''
end

Private Instance Methods

replace_tempfile(hash) click to toggle source
# File lib/locomotive/steam/liquid/tags/action.rb, line 65
def replace_tempfile(hash)
  hash.each do |key, value|
    case value
    when Tempfile then hash[key] = value.path
    when Hash     then replace_tempfile(value)
    end
  end
end
safe_params(context) click to toggle source
# File lib/locomotive/steam/liquid/tags/action.rb, line 56
def safe_params(context)
  return {} if context.registers[:params].blank?

  context.registers[:params].dup.tap do |params|
    # Tempfile can't be converted in Duktape for obvious reasons
    replace_tempfile(params)
  end
end
service(context) click to toggle source
# File lib/locomotive/steam/liquid/tags/action.rb, line 52
def service(context)
  context.registers[:services].action
end