Module: Card::Query::Sorting
- Included in:
- Card::Query
- Defined in:
- lib/card/query/sorting.rb
Constant Summary collapse
- SORT_JOIN_TO_ITEM_MAP =
{ left: "left_id", right: "right_id" }.freeze
Instance Method Summary collapse
- #sort(val) ⇒ Object
-
#sort_by_count(val, item) ⇒ Object
EXPERIMENTAL!.
Instance Method Details
#sort(val) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/card/query/sorting.rb', line 6 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 Card::Error::BadQuery, "sort item: #{item} not yet implemented" end end |
#sort_by_count(val, item) ⇒ Object
EXPERIMENTAL!
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/card/query/sorting.rb', line 24 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 Card::Error::BadQuery, "count with item: #{item} not yet implemented" end end |