Sha256: cccdf69a0ceb64b7c3cdf9b327a40772ddbeda53e0b00019ab878a586335b5b7

Contents?: true

Size: 1.01 KB

Versions: 26

Compression:

Stored size: 1.01 KB

Contents

module CompositePrimaryKeys::ActiveRecord::QueryMethods

  def reverse_sql_order(order_query)
    # CPK
    # order_query = ["#{quoted_table_name}.#{quoted_primary_key} ASC"] if order_query.empty?

    # break apart CPKs 
    order_query = primary_key.map do |key|
      "#{quoted_table_name}.#{connection.quote_column_name(key)} ASC"
    end if order_query.empty?

    order_query.map do |o|
      case o
      when Arel::Nodes::Ordering
        o.reverse
      when String, Symbol
        o.to_s.split(',').collect do |s|
          s.strip!
          s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC')
        end
      else
        o
      end
    end.flatten
  end


  def order(*args)    
    args.map! do |arg|
      if arg.is_a?(Arel::Nodes::Ordering) && arg.expr.name.is_a?(Array)
        arg = arg.expr.name.map do |key|
          cloned_node = arg.clone
          cloned_node.expr.name = key
          cloned_node
        end
      end
      arg
    end if composite?
    super(*args)
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
composite_primary_keys-7.0.0 lib/composite_primary_keys/relation/query_methods.rb
composite_primary_keys-6.0.6 lib/composite_primary_keys/relation/query_methods.rb
composite_primary_keys-6.0.5 lib/composite_primary_keys/relation/query_methods.rb
composite_primary_keys-6.0.3 lib/composite_primary_keys/relation/query_methods.rb
composite_primary_keys-6.0.1 lib/composite_primary_keys/relation/query_methods.rb
composite_primary_keys-6.0.0 lib/composite_primary_keys/relation/query_methods.rb