Sha256: 09a637deb8247ce6711a6206345e4f9f6c55a56297b8c4e3bf24e3063e1c5708

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module FetcheableOnApi
  module Filtreable
    #
    # Supports
    #

    #
    # Public class methods
    #
    def self.included(base)
      base.class_eval do
        extend ClassMethods
        class_attribute :filters_configuration, :instance_writer => false
        self.filters_configuration = {}
      end
    end

    module ClassMethods
      def filter_by(*attrs)
        options = attrs.extract_options!
        options.symbolize_keys!
        options.assert_valid_keys(:as, :class_name)

        self.filters_configuration = filters_configuration.dup

        attrs.each do |attr|
          filters_configuration[attr] ||= {
            as: options[:as] || attr
          }

          filters_configuration[attr] = filters_configuration[attr].merge(options)
        end
      end
    end

    #
    # Public instance methods
    #

    #
    # Protected instance methods
    #
    protected

    def apply_filters(collection)
      return collection if params[:filter].blank?

      filter_params = params.require(:filter)
                            .permit(filters_configuration.keys)
                            .to_hash

      filtering = filter_params.map do |column, values|
        values.split(',').map do |value|
          column_name = filters_configuration[column.to_sym].fetch(:as, column)
          klass       = filters_configuration[column.to_sym].fetch(:class_name, collection.klass)

          klass.arel_table[column_name].matches("%#{value}%")
        end.inject(:or)
      end

      collection.where(filtering.flatten.compact.inject(:and))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fetcheable_on_api-0.1.4 lib/fetcheable_on_api/filtreable.rb
fetcheable_on_api-0.1.3 lib/fetcheable_on_api/filtreable.rb