Sha256: dc0d6272dc8ee296165c9d9270d4f9d05bd103875be26174e9ff58a59d122b96

Contents?: true

Size: 631 Bytes

Versions: 2

Compression:

Stored size: 631 Bytes

Contents

require 'active_support'

module CollectionSorter
  module Sorter
    extend ActiveSupport::Concern

    included do
      class_eval do
        class_attribute :sorters
      end
    end

    module ClassMethods
      def sort_with(name, &block)
        self.sorters = self.sorters || {}
        self.sorters[name] = block
      end
    end

    def apply_sort(scope, default=nil)
      sorter = (request.params[:sort] rescue nil) || default || (return scope)
      sorter = default unless self.sorters.include?(sorter.to_sym)

      return self.sorters[sorter.to_sym].call(scope) if sorter.present?
      scope
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
collection_sorter-0.0.4 lib/collection_sorter/sorter.rb
collection_sorter-0.0.3 lib/collection_sorter/sorter.rb