Sha256: fc1f0ac57550bde07b1bfbdd331ba8276155c893d5e6e07c9901fd01689fcae8

Contents?: true

Size: 943 Bytes

Versions: 7

Compression:

Stored size: 943 Bytes

Contents

module Ransack
  module Nodes
    class Sort < Node
      include Bindable

      attr_reader :name, :dir
      i18n_word :asc, :desc

      class << self
        def extract(context, str)
          attr, direction = str.split(/\s+/,2)
          self.new(context).build(name: attr, dir: direction)
        end
      end

      def build(params)
        params.with_indifferent_access.each do |key, value|
          if key.match(/^(name|dir)$/)
            self.send("#{key}=", value)
          end
        end

        self
      end

      def valid?
        bound? && attr &&
          context.klassify(parent).ransortable_attributes(context.auth_object)
          .include?(attr_name)
      end

      def name=(name)
        @name = name
        context.bind(self, name) unless name.blank?
      end

      def dir=(dir)
        dir = dir.try(:downcase)
        @dir = %w(asc desc).include?(dir) ? dir : 'asc'
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ransack-1.4.1 lib/ransack/nodes/sort.rb
ransack-1.4.0 lib/ransack/nodes/sort.rb
ransack-1.3.0 lib/ransack/nodes/sort.rb
ransack-1.2.3 lib/ransack/nodes/sort.rb
ransack-1.2.2 lib/ransack/nodes/sort.rb
ransack-1.2.1 lib/ransack/nodes/sort.rb
ransack-1.2.0 lib/ransack/nodes/sort.rb