Sha256: 4901c9778e6d875a593514cbf68ca86ddaa5feed0cb0da6dbf21527a31af5c36

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Tramway::Concerns::Filtering
  def list_filtering(records)
    params[:list_filters]&.each do |filter, _value|
      case decorator_class.list_filters[filter.to_sym][:type]
      when :select
        records = list_filtering_select records, filter
      when :dates
        records = list_filtering_dates records, filter
      end
    end

    records
  end

  def list_filtering_select(records, filter)
    value.present? ? decorator_class.list_filters[filter.to_sym][:query].call(records, value) : records
  end

  def list_filtering_dates(records, filter)
    begin_date = date_filter :begin, filter
    end_date = date_filter :end, filter
    if begin_date.present? && end_date.present? && value.present?
      decorator_class.list_filters[filter.to_sym][:query].call(records, begin_date, end_date)
    else
      records
    end
  end

  def date_filter(type, filter)
    params[:list_filters][filter.to_sym]["#{type}_date".to_sym]
  end

  def filtering(records)
    if params[:filter].present?
      params[:filter] = JSON.parse params[:filter] if params[:filter].is_a? String
      records.ransack(params[:filter]).result(distinct: true)
    else
      records
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tramway-0.1.2 app/controllers/tramway/concerns/filtering.rb