Sha256: 6d0a7de04ac4a958100e8a4ac878b2a70a72b9047f6c0b6c9df08b4bb0d5dc43

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 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 create_condition(name)
          if name == :order
            alias_scope name, lambda { |scope_name|
              return scoped({}) if !condition?(scope_name)
              send(scope_name)
            }
          elsif details = ordering_condition_details(name)
            create_ordering_conditions(details[:column])
          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

6 entries across 6 versions & 1 rubygems

Version Path
searchlogic-2.5.17 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.5.16 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.5.15 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.5.14 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.5.13 lib/searchlogic/named_scopes/ordering.rb
searchlogic-2.5.12 lib/searchlogic/named_scopes/ordering.rb