Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 14 additions & 11 deletions app/models/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@
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

Check failure on line 15 in app/models/filter.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Layout/IndentationWidth: Use 2 (not 9) spaces for indentation. (https://rubystyle.guide#spaces-indentation)
else

Check failure on line 16 in app/models/filter.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Layout/ElseAlignment: Align `else` with `if`.
_compact_attributes params.with_indifferent_access
end

Check failure on line 18 in app/models/filter.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Layout/EndAlignment: `end` at 18, 20 is not aligned with `if` at 14, 13.

@attributes = defaults.with_indifferent_access
.select { |k, v| attribute_names.include?(k.to_sym) }

Check failure on line 21 in app/models/filter.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Lint/UnusedBlockArgument: Unused block argument - `v`. If it's necessary, use `_` or `_v` as an argument name to indicate that it won't be used. (https://rubystyle.guide#underscore-unused-vars)
.deep_transform_values(&:to_s)
.merge(params)
# raise

Check failure on line 24 in app/models/filter.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Layout/CommentIndentation: Incorrect indentation detected (column 12 instead of 4).
end

class << self
Expand Down Expand Up @@ -41,14 +52,6 @@
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
Expand Down
4 changes: 2 additions & 2 deletions app/models/processor_filter.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading