Sha256: b95f6a4737325bc7f7037731fbc68ebf4dc3032d81735d5bd46976b8e6d411e7

Contents?: true

Size: 1.13 KB

Versions: 18

Compression:

Stored size: 1.13 KB

Contents

module Ransack
  module Nodes
    class Sort < Node
      include Bindable

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

      class << self
        def extract(context, str)
          return unless 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|ransacker_args)$/)
            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)
      end

      def dir=(dir)
        dir = dir.downcase if dir
        @dir =
          if dir == 'asc'.freeze || dir == 'desc'.freeze
            dir
          else
            'asc'.freeze
          end
      end

      def ransacker_args=(ransack_args)
        @ransacker_args = ransack_args
      end

    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ransack-2.4.1 lib/ransack/nodes/sort.rb
ransack-2.4.0 lib/ransack/nodes/sort.rb
ransack-2.3.2 lib/ransack/nodes/sort.rb
ransack-2.3.1 lib/ransack/nodes/sort.rb
ransack-2.3.0 lib/ransack/nodes/sort.rb
ransack-1.8.10 lib/ransack/nodes/sort.rb
ransack-2.1.1 lib/ransack/nodes/sort.rb
ransack-2.1.0 lib/ransack/nodes/sort.rb
ransack-2.0.1 lib/ransack/nodes/sort.rb
ransack-2.0.0 lib/ransack/nodes/sort.rb
ransack-1.8.9 lib/ransack/nodes/sort.rb
ransack-1.8.8 lib/ransack/nodes/sort.rb
ransack-1.8.7 lib/ransack/nodes/sort.rb
ransack-1.8.6 lib/ransack/nodes/sort.rb
ransack-1.8.4 lib/ransack/nodes/sort.rb
ransack-1.8.3 lib/ransack/nodes/sort.rb
ransack-1.8.2 lib/ransack/nodes/sort.rb
ransack-1.8.1 lib/ransack/nodes/sort.rb