Sha256: 7386b96451f3f47d8e9dbc8eef7eb0c871e34d4644b1ed1de18b147ce6f87313

Contents?: true

Size: 1.47 KB

Versions: 54

Compression:

Stored size: 1.47 KB

Contents

module Searchlogic
  module NamedScopes
    # Handles dynamically creating named scopes for ordering by columns. Example:
    #
    #   User.ascend_by_id
    #   User.descend_by_username
    #
    # See the README for a more detailed explanation.
    module Ordering
      def condition?(name) # :nodoc:
        super || ordering_condition?(name)
      end
      
      private
        def ordering_condition?(name) # :nodoc:
          !ordering_condition_details(name).nil?
        end
        
        def method_missing(name, *args, &block)
          if name == :order
            named_scope name, lambda { |scope_name|
              return {} if !condition?(scope_name)
              send(scope_name).proxy_options
            }
            send(name, *args)
          elsif details = ordering_condition_details(name)
            create_ordering_conditions(details[:column])
            send(name, *args)
          else
            super
          end
        end
        
        def ordering_condition_details(name)
          if name.to_s =~ /^(ascend|descend)_by_(#{column_names.join("|")})$/
            {:order_as => $1, :column => $2}
          elsif name.to_s =~ /^order$/
            {}
          end
        end
        
        def create_ordering_conditions(column)
          named_scope("ascend_by_#{column}".to_sym, {:order => "#{table_name}.#{column} ASC"})
          named_scope("descend_by_#{column}".to_sym, {:order => "#{table_name}.#{column} DESC"})
        end
    end
  end
end

Version data entries

54 entries across 54 versions & 9 rubygems

Version Path
searchlogic-donotuse-2.3.9 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.32 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.31 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.30 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.29 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.28 lib/searchlogic/named_scopes/ordering.rb
nberger-searchlogic-2.4.29 lib/searchlogic/named_scopes/ordering.rb
nberger-searchlogic-2.4.28 lib/searchlogic/named_scopes/ordering.rb
obitum-searchlogic-2.4.28 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.27 lib/searchlogic/named_scopes/ordering.rb
yoomee-searchlogic-2.4.27 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.26 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.25 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.24 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.23 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.22 lib/searchlogic/named_scopes/ordering.rb
searchlogic-heroku-2.4.19 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.21 lib/searchlogic/named_scopes/ordering.rb
gravis-searchlogic-2.4.20 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.4.19 lib/searchlogic/named_scopes/ordering.rb