Created
June 3, 2013 16:34
-
-
Save svoboda-jan/5699420 to your computer and use it in GitHub Desktop.
A simple module include that allows client side model sorting in Vienna
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 Vienna | |
module Sorting | |
module ClassMethods | |
def sorted_by(field, options = {}) | |
@_sort_field = field | |
@_sort_descending = options.fetch(:order, "asc") != 'asc' | |
end | |
def all | |
return @_id_map.values unless @_sort_field | |
@_id_map.values.sort do |a,b| | |
a,b = b,a if @_sort_descending | |
a[@_sort_field] <=> b[@_sort_field] | |
end | |
end | |
end | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment