Sha256: 6c84b16e61ee945257c86ac71d5760fb17c18cdeaa9b7bce146b9198480ecbb5

Contents?: true

Size: 1.72 KB

Versions: 20

Compression:

Stored size: 1.72 KB

Contents

module Searchlogic
  module NamedScopes
    # Handles dynamically creating order named scopes for associations:
    #
    #   User.has_many :orders
    #   Order.has_many :line_items
    #   LineItem
    #
    #   User.ascend_by_orders_line_items_id
    #
    # See the README for a more detailed explanation.
    module AssociationOrdering
      def condition?(name) # :nodoc:
        super || association_ordering_condition?(name)
      end

      private
        def association_ordering_condition?(name)
          !association_ordering_condition_details(name).nil?
        end

        def create_condition(name)
          if details = association_ordering_condition_details(name)
            create_association_ordering_condition(details[:association], details[:order_as], details[:condition])
          else
            super
          end
        end

        def association_ordering_condition_details(name)
          associations = reflect_on_all_associations
          association_names = associations.collect { |assoc| assoc.name }
          if name.to_s =~ /^(ascend|descend)_by_(#{association_names.join("|")})_(\w+)$/
            {:order_as => $1, :association => associations.find { |a| a.name == $2.to_sym }, :condition => $3}
          end
        end

        def create_association_ordering_condition(association, order_as, condition)
         cond = condition
         poly_class = nil
         if condition =~ /^(\w+)_type_(\w+)$/
           poly_type = $1
           cond = $2
           poly_class = poly_type.camelcase.constantize if poly_type
         end
         named_scope("#{order_as}_by_#{association.name}_#{condition}", association_condition_options(association, "#{order_as}_by_#{cond}", poly_class))
        end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
searchlogic-2.5.19 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.18 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.17 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.16 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.15 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.14 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.13 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.12 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.11 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.10 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.9 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.8 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.7 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.6 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.5 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.4 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.3 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.2 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.1 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.5.0 lib/searchlogic/named_scopes/association_ordering.rb