Sha256: 61edb306e268f36543b1de21119ceca7eee16ffdd6132f6d4a1d5e0a5d0cd531

Contents?: true

Size: 1.36 KB

Versions: 13

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

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

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

        return_set(processed_set)
      end

      private

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

      def processed_set
        @set.includes(@instruction.associations_hash)
            .references(@instruction.associations_hash)
            .merge(arel_operation)
      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

13 entries across 13 versions & 1 rubygems

Version Path
activeset-0.5.7 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.5.6 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.5.5 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.5.4 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.5.3 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.5.2 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.5.1 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.5.0 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.4.4 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.4.3 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.4.2 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.4.1 lib/active_set/processors/sort/active_record_adapter.rb
activeset-0.4.0 lib/active_set/processors/sort/active_record_adapter.rb