Sha256: e1ccab1858b8c610a436ec1f45d8f3f694c28ac257486532f6011d29f6afa511

Contents?: true

Size: 841 Bytes

Versions: 16

Compression:

Stored size: 841 Bytes

Contents

module Administrate
  class Order
    def initialize(attribute = nil, direction = nil)
      @attribute = attribute
      @direction = direction || :asc
    end

    def apply(relation)
      if relation.columns_hash.keys.include?(attribute.to_s)
        relation.order(attribute => direction)
      else
        relation
      end
    end

    def ordered_by?(attr)
      attr.to_s == attribute.to_s
    end

    def order_params_for(attr)
      {
        order: attr,
        direction: reversed_direction_param_for(attr)
      }
    end

    attr_reader :direction

    private

    attr_reader :attribute

    def reversed_direction_param_for(attr)
      if ordered_by?(attr)
        opposite_direction
      else
        :asc
      end
    end

    def opposite_direction
      direction.to_sym == :asc ? :desc : :asc
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
administrate-0.7.0 lib/administrate/order.rb
administrate-0.6.0 lib/administrate/order.rb
administrate-0.5.0 lib/administrate/order.rb
administrate-0.4.0 lib/administrate/order.rb
administrate-0.3.0 lib/administrate/order.rb
administrate-0.2.2 lib/administrate/order.rb
administrate-0.2.1 lib/administrate/order.rb
administrate-0.2.0 lib/administrate/order.rb
administrate-0.2.0.rc1 lib/administrate/order.rb
administrate-0.1.5 lib/administrate/order.rb
administrate-0.1.4 lib/administrate/order.rb
administrate-0.1.3 lib/administrate/order.rb
administrate-0.1.2 lib/administrate/order.rb
administrate-0.1.1 lib/administrate/order.rb
administrate-0.1.0 lib/administrate/order.rb
administrate-0.0.12 lib/administrate/order.rb