Sha256: 31effb7d9e46914bb728c0c6653a7bd0f35054be7da53cd9ffa0acb92c286583

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require_relative '../base_adapter'
require_relative '../base_processor'

class ActiveSet
  class SortProcessor < BaseProcessor
    class ActiveRecordAdapter < BaseAdapter
      def process(set)
        @set = set
        return @set unless @set.respond_to? :to_sql
        return @set unless attribute_is_field?

        @set.includes(@instruction.associations_hash)
            .references(@instruction.associations_hash)
            .merge(arel_operation)
      end

      private

      def attribute_is_field?
        return false unless attribute_model
        attribute_model.attribute_names
                       .include?(@instruction.attribute)
      end

      def arel_operation
        column = case_insensitive? ? arel_column.lower : arel_column
        attribute_model.order(column.send(@instruction.value))
      end

      def attribute_model
        @instruction.associations_array
                    .reduce(@set) do |obj, assoc|
                      obj.reflections[assoc.to_s]&.klass
                    end
      end

      def arel_column
        arel_table[@instruction.attribute]
      end

      def arel_table
        Arel::Table.new(attribute_model.table_name)
      end

      def case_insensitive?
        @instruction.operator.to_s == 'i'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activeset-0.3.1 lib/active_set/processors/sort/active_record_adapter.rb