From f19b4eeede72a3ff7e93d3776b2dab95a41c7359 Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Wed, 20 May 2026 17:04:35 +0200 Subject: [PATCH] wip --- app/controllers/categories_controller.rb | 2 +- app/models/filter.rb | 25 +++++++++++++----------- app/models/processor_filter.rb | 4 ++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 6dabb53c6..0389f9f96 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -9,7 +9,7 @@ class CategoriesController < ApplicationController # GET /categories # GET /categories.json def index - @filter = ProcessorFilter.new(Category.order(name: :asc), params) + @filter = ProcessorFilter.new(Category.all, params, defaults: { order_by: :name, sort_by: :asc }) authorize! @categories = @filter.results end diff --git a/app/models/filter.rb b/app/models/filter.rb index cd5fd8f1a..53704b968 100644 --- a/app/models/filter.rb +++ b/app/models/filter.rb @@ -4,13 +4,24 @@ class Filter include ActiveModel::Conversion extend ActiveModel::Translation - attr_reader :attribute_names + attr_reader :attributes, :attribute_names delegate :model_name, to: :class - def initialize(params, attribute_names) - @params = params.dup + def initialize(params, attribute_names, defaults: {}) @attribute_names = attribute_names + + params = if params.respond_to?(:permit) + _compact_attributes params.permit(*_permitted_attributes_names).to_h + else + _compact_attributes params.with_indifferent_access + end + + @attributes = defaults.with_indifferent_access + .select { |k, v| attribute_names.include?(k.to_sym) } + .deep_transform_values(&:to_s) + .merge(params) + # raise end class << self @@ -41,14 +52,6 @@ def filled?(attribute_name = nil) def filled_attributes @filled_attributes ||= attributes.compact_blank end - - def attributes - @attributes ||= if @params.respond_to?(:permit) - _compact_attributes @params.permit(*_permitted_attributes_names).to_h - else - _compact_attributes @params.with_indifferent_access - end - end alias to_h attributes private diff --git a/app/models/processor_filter.rb b/app/models/processor_filter.rb index 00741b050..f51ee19ca 100644 --- a/app/models/processor_filter.rb +++ b/app/models/processor_filter.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true class ProcessorFilter < Filter - def initialize(records, params, with: nil) + def initialize(records, params, with: nil, defaults: {}) @records = records @rubanok_class = with || guess_processor_from_records(records) raise "Processor class missing" unless @rubanok_class - super(params, @rubanok_class.fields_set.to_a) + super(params, @rubanok_class.fields_set.to_a, defaults:) end def results