Sha256: 52ea51271a6ee2593ec927980d2a921ab08e525456a7fe2bf02c00c83a4d5fbb

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

module Locomotive
  module Steam
    module Liquid
      module Tags

        # Execute javascript code server side.
        # The API allows you to:
        # - access the current liquid context
        # - modify the session
        # - send emails
        # - find / create / update content entries
        #
        # 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>
        #
        class Action < ::Liquid::Block

          Syntax = /(#{::Liquid::QuotedString}+)/o

          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

          def render(context)
            Locomotive::Common::Logger.info "[action] executing #{@description}"
            service(context).run(super, context['params'], context)
            ''
          end

          private

          def service(context)
            context.registers[:services].action
          end

        end

        ::Liquid::Template.register_tag('action'.freeze, Action)

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
locomotivecms_steam-1.2.0.rc2 lib/locomotive/steam/liquid/tags/action.rb
locomotivecms_steam-1.2.0.rc1 lib/locomotive/steam/liquid/tags/action.rb
locomotivecms_steam-1.2.0.beta1 lib/locomotive/steam/liquid/tags/action.rb