Sha256: 7a1a97bbdd0c17ed2c14880094596acd486163e1d34e8d1b014d150abe141cbf

Contents?: true

Size: 967 Bytes

Versions: 2

Compression:

Stored size: 967 Bytes

Contents

# frozen_string_literal: true

module Pragma
  module Macro
    def self.Filtering
      step = ->(input, options) { Filtering.for(input, options) }
      [step, name: 'filtering']
    end

    module Filtering
      class << self
        def for(_input, options)
          set_defaults(options)

          options['model'] = apply_filtering(options)

          true
        end

        private

        def set_defaults(options)
          {
            'filtering.filters' => []
          }.each_pair do |key, value|
            options[key] = value unless options[key]
          end
        end

        def apply_filtering(options)
          relation = options['model']

          options['filtering.filters'].each do |filter|
            value = options['params'][filter.param]
            next unless value.present?

            relation = filter.apply(relation: relation, value: value)
          end

          relation
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pragma-2.5.0 lib/pragma/macro/filtering.rb
pragma-2.4.0 lib/pragma/macro/filtering.rb