Sha256: 1a2f7b6674d475c9bba68841a591cd604981cb6f4e2ba62bd2e7e7bc2317cc96

Contents?: true

Size: 846 Bytes

Versions: 3

Compression:

Stored size: 846 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

3 entries across 3 versions & 2 rubygems

Version Path
semantic-administrate-0.1.0 lib/administrate/order.rb
administrate-0.8.1 lib/administrate/order.rb
administrate-0.8.0 lib/administrate/order.rb