Sha256: 7f15913fc4ce8d047661426acc49b48a375ad706c552a7613cd025e2b07ea48a

Contents?: true

Size: 1.49 KB

Versions: 15

Compression:

Stored size: 1.49 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 method_missing(name, *args, &block)
          if details = association_ordering_condition_details(name)
            create_association_ordering_condition(details[:association], details[:order_as], details[:condition], args)
            send(name, *args)
          else
            super
          end
        end
        
        def association_ordering_condition_details(name)
          associations = reflect_on_all_associations.collect { |assoc| assoc.name }
          if name.to_s =~ /^(ascend|descend)_by_(#{associations.join("|")})_(\w+)$/
            {:order_as => $1, :association => $2, :condition => $3}
          end
        end
        
        def create_association_ordering_condition(association, order_as, condition, args)
          named_scope("#{order_as}_by_#{association}_#{condition}", association_condition_options(association, "#{order_as}_by_#{condition}", args))
        end
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
binarylogic-searchlogic-2.3.0 lib/searchlogic/named_scopes/association_ordering.rb
binarylogic-searchlogic-2.3.1 lib/searchlogic/named_scopes/association_ordering.rb
binarylogic-searchlogic-2.3.2 lib/searchlogic/named_scopes/association_ordering.rb
binarylogic-searchlogic-2.3.3 lib/searchlogic/named_scopes/association_ordering.rb
binarylogic-searchlogic-2.3.4 lib/searchlogic/named_scopes/association_ordering.rb
binarylogic-searchlogic-2.3.5 lib/searchlogic/named_scopes/association_ordering.rb
kazjote-searchlogic-2.3.4 lib/searchlogic/named_scopes/association_ordering.rb
kazjote-searchlogic-2.3.6 lib/searchlogic/named_scopes/association_ordering.rb
kazjote-searchlogic-2.3.5 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.3.5 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.3.4 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.3.3 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.3.2 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.3.1 lib/searchlogic/named_scopes/association_ordering.rb
searchlogic-2.3.0 lib/searchlogic/named_scopes/association_ordering.rb