Sha256: b4a993c9eb9b232f6c198e56406eda66f6f47e6262ee9f7bb75a23abfe9fed72

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

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.downcase if dir
        @dir =
          if Ransack::Constants::ASC_DESC.include?(dir)
            dir
          else
            Ransack::Constants::ASC
          end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ransack-1.5.1 lib/ransack/nodes/sort.rb
ransack-1.5.0 lib/ransack/nodes/sort.rb