Sha256: 3d69568a8342e5595c31450b42283ed4d0397256bed1f340cd0ef0d840ef5ec1

Contents?: true

Size: 1.83 KB

Versions: 11

Compression:

Stored size: 1.83 KB

Contents

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
module FatFreeCRM
  module Sortable

    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods

      # Model class method to define sort options, for example:
      #   sortable :by => "first_name ASC"
      #   sortable :by => [ "first_name ASC", "last_name ASC" ]
      #   sortable :by => [ "first_name ASC", "last_name ASC" ], :default => "last_name ASC"
      #--------------------------------------------------------------------------
      def sortable(options = {})
        cattr_accessor :sort_by,            # Default sort order with prepended table name.
                       :sort_by_fields,     # Array of fields to sort by without ASC/DESC.
                       :sort_by_clauses     # A copy of sortable :by => ... stored as array.

        self.sort_by_clauses = [options[:by]].flatten
        self.sort_by_fields = self.sort_by_clauses.map(&:split).map(&:first)
        self.sort_by = self.name.tableize + "." + (options[:default] || options[:by].first)
      end

      # Return hash that maps sort options to the actual :order strings, for example:
      #   "first_name" => "leads.first_name ASC",
      #   "last_name"  => "leads.last_name ASC"
      #--------------------------------------------------------------------------
      def sort_by_map
        Hash[
          self.sort_by_fields.zip(self.sort_by_clauses).map do |field, clause|
            [ field, self.name.tableize + "." + clause ]
          end
        ]
      end

    end # ClassMethods

  end
end

ActiveRecord::Base.send(:include, FatFreeCRM::Sortable)

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
fat_free_crm-0.13.6 lib/fat_free_crm/sortable.rb
fat_free_crm-0.13.5 lib/fat_free_crm/sortable.rb
fat_free_crm-0.13.4 lib/fat_free_crm/sortable.rb
fat_free_crm-0.13.3 lib/fat_free_crm/sortable.rb
fat_free_crm-0.13.2 lib/fat_free_crm/sortable.rb
fat_free_crm-0.12.3 lib/fat_free_crm/sortable.rb
fat_free_crm-0.12.2 lib/fat_free_crm/sortable.rb
fat_free_crm-0.13.1 lib/fat_free_crm/sortable.rb
fat_free_crm-0.12.1 lib/fat_free_crm/sortable.rb
fat_free_crm-0.13.0 lib/fat_free_crm/sortable.rb
fat_free_crm-0.12.0 lib/fat_free_crm/sortable.rb