Sha256: 8c20efdf1a4638c40514729c0c259f980a333a1eb28eb586c01daf97a869df19

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module ActionSet
  class SortInstructions
    def initialize(params, set, controller)
      @params = params
      @set = set
      @controller = controller
    end

    def get
      instructions_hash = if form_friendly_complex_params?
                            form_friendly_complex_params_to_hash
                          elsif form_friendly_simple_params?
                            form_friendly_simple_params_to_hash
                          else
                            @params
                          end

      instructions_hash.transform_values { |v| v.remove('ending') }
    end

    private

    def form_friendly_complex_params?
      @params.key?(:'0')
    end

    def form_friendly_simple_params?
      @params.key?(:attribute) &&
        @params.key?(:direction)
    end

    def form_friendly_complex_params_to_hash
      ordered_instructions = @params.sort_by(&:first)
      array_of_instructions = ordered_instructions.map { |_, h| [h[:attribute], h[:direction]] }
      Hash[array_of_instructions]
    end

    def form_friendly_simple_params_to_hash
      { @params[:attribute] => @params[:direction] }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
actionset-0.11.0 lib/action_set/sort_instructions.rb
actionset-0.10.0 lib/action_set/sort_instructions.rb
actionset-0.9.2 lib/action_set/sort_instructions.rb