Skip to content

Instantly share code, notes, and snippets.

@svoboda-jan
Created June 3, 2013 16:34
Show Gist options
  • Save svoboda-jan/5699420 to your computer and use it in GitHub Desktop.
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
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