Sha256: f7de823bdae2443ff1e0d7093d5eed8490824bf6b1959bacead87a3dad3b9dc5

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

class Card
  class Query
    module Sorting
      SORT_JOIN_TO_ITEM_MAP = { left: 'left_id', right: 'right_id' }.freeze

      def sort val
        return nil if @superquery
        sort_field = val[:return] || 'db_content'
        item = val.delete(:item) || 'left'

        if sort_field == 'count'
          sort_by_count val, item
        elsif (join_field = SORT_JOIN_TO_ITEM_MAP[item.to_sym])
          sq = join_cards(val, to_field: join_field,
                               side: 'LEFT',
                               conditions_on_join: true)
          @mods[:sort] ||= "#{sq.table_alias}.#{sort_field}"
        else
          raise BadQuery, "sort item: #{item} not yet implemented"
        end
      end

      # EXPERIMENTAL!
      def sort_by_count val, item
        if item == 'referred_to'
          @mods[:sort] = 'coalesce(count,0)' # needed for postgres
          cs = Query.new(
            return: 'coalesce(count(*), 0) as count',
            group: 'sort_join_field',
            superquery: self
          )
          subselect = Query.new val.merge(return: 'id', superquery: self)
          cs.add_condition "referer_id in (#{subselect.sql})"
          # FIXME: - SQL generated before SQL phase
          cs.joins << Join.new(
            from: cs,
            to: %w(card_references wr referee_id)
          )
          cs.mods[:sort_join_field] = "#{cs.table_alias}.id as sort_join_field"
          # HACK!

          joins << Join.new(
            from: self,
            to: [cs, 'srtbl', 'sort_join_field']
          )
        else
          raise BadQuery, "count with item: #{item} not yet implemented"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
card-1.18.6 lib/card/query/sorting.rb
card-1.18.5 lib/card/query/sorting.rb