Sha256: 0e6f275ae1ec0a9f4be1ac57cdfe73954ff34db8e7bc2c44ddd5c3288ab23b75

Contents?: true

Size: 1.65 KB

Versions: 28

Compression:

Stored size: 1.65 KB

Contents

class Card
  module Query
    # convert @query sort rep into order by statement
    # order information is stored in @mods[:sort], @mods[:sort_as], and
    # @mods[:dir]
    class SqlStatement
      ORDER_MAP = {
        "id" => "id",
        "update" => "updated_at",
        "create" => "created_at",
        "name" => "key",
        "content" => "db_content",
        "alpha" => "key",       # DEPRECATED
        "relevance" => "updated_at" # DEPRECATED
      }.freeze

      # build ORDER BY clause
      module Order
        def order
          full_syntax do
            "ORDER BY #{order_directives.join ', '}"
          end
        end

        def order_directives
          Array.wrap(order_config).map do |order_key|
            order_directive order_key
          end
        end

        def order_directive order_key
          field = order_field order_key
          @fields += ", #{field}"
          "#{field} #{order_dir order_key}"
        end

        def order_field order_key
          order_as do
            if (field = ORDER_MAP[order_key])
              "#{@query.table_alias}.#{field}"
            else
              safe_sql order_key
            end
          end
        end

        def order_as
          field = yield
          return field unless (as = @mods[:sort_as])

          "CAST(#{field} AS #{cast_type(safe_sql(as))})"
        end

        def order_dir order_key
          if @mods[:dir].blank?
            DEFAULT_ORDER_DIRS[order_key.to_sym] || "asc"
          else
            safe_sql @mods[:dir]
          end
        end

        def order_config
          @mods[:sort].blank? ? "update" : @mods[:sort]
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
card-1.99.1 lib/card/query/sql_statement/order.rb
card-1.99.0 lib/card/query/sql_statement/order.rb
card-1.98.3 lib/card/query/sql_statement/order.rb
card-1.98.2 lib/card/query/sql_statement/order.rb
card-1.98.1 lib/card/query/sql_statement/order.rb
card-1.98.0 lib/card/query/sql_statement/order.rb
card-1.97.0.1 lib/card/query/sql_statement/order.rb
card-1.97.0 lib/card/query/sql_statement/order.rb