Sha256: 62f04d08aa9ef6a0e42bfa0db9d2d63f85f91d7183d9d9c9c2b485a0143f2bda

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Pragma
  module Operation
    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: options['model'], value: value)
            end

            relation
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pragma-2.1.1 lib/pragma/operation/macro/filtering.rb
pragma-2.1.0 lib/pragma/operation/macro/filtering.rb