Sha256: 6f3c8f7c96ddd8300576932616a4ee654e4b91ecc6d36c4df0264937e1ad92aa

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

module Locomotive
  module Sort
    class BooleanSortTag < ::Liquid::Tag

      Syntax = /to\s*(#{::Liquid::VariableSignature}+)\s*from\s*(#{::Liquid::VariableSignature}+)\s*by\s*(#{::Liquid::QuotedFragment}+)\s*(reverse)?/


        def initialize(tag_name, markup, tokens, context)
          if markup =~ Syntax
            @target  = $1
            @collection = $2
            @field = $3
            @rev = $4
          else
            raise ::Liquid::SyntaxError.new("Syntax Error in 'sort_boolean' - Valid syntax: sort_boolean to <var> from <content_type> by <field>")
          end
          super
        end

      def render(context)
        collection = context[@collection].dup
        first =  collection.first
        if @field and first.respond_to?(@field.to_sym)
          top = []
          bot = []
          collection.each do |entry|
            !!entry.send(@field.to_sym) ^ !!@rev ? top << entry : bot << entry
          end
          collection = Locomotive::Liquid::Drops::ProxyCollection.new(top + bot)
        end

        context[@target.to_s] = collection
        ""
      end

      def render_disabled(context)
        collection = context[@collection]
        context[@target.to_s] = collection
        ""
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotive_sort_plugin-0.4.0 lib/locomotive/sort/plugin/boolean_sort_tag.rb
locomotive_sort_plugin-0.3.1 lib/locomotive/sort/plugin/boolean_sort_tag.rb