Sha256: e3025ea105c8a4536d47c2eb9c51cc1580ae2ff31224a5b19576d3d0b8288566
Contents?: true
Size: 1.8 KB
Versions: 1
Compression:
Stored size: 1.8 KB
Contents
# frozen_string_literal: true # This module provides an interface for attaching easy filtering capabilities to a controller. # It acts as a base for models to interact with their controllers. When included in a model, # it allows the controller to accept a specific set of filter parameters which are then processed # and applied on the model's data for tailored querying and data selection. # This is particularly useful when you want to have the flexibility of filtering # model data directly from controller parameters. module ScopeHound # Adds capability of filtering to the controllers module FilterableController extend ActiveSupport::Concern attr_accessor :all_filtered_records, :unique_filters def filter(scope, filters = filter_params) filtered_scope, unique_filters = scope.filter_by(**filters) self.unique_filters = unique_filters self.all_filtered_records = filtered_scope filtered_scope rescue NoMethodError raise " Controller #{self.class.name} tried to filter a scope of type #{scope.class.name}. Scope class does not extend FilterProxy interface. " end def filter_params raise "FilterableModel controller #{self.class.name} does not define filter_params method." end def filter_values(attribute, all_values, show_all: false) if show_all all_values else all_values.select do |_, id| unique_filters[attribute]&.include?(id) end end end included do helper_method :filter_params, :filter_values, :all_filtered_records end private def get_attribute_and_associations(attr_name) attribute_parts = attr_name.to_s.split(".") field = attribute_parts.pop associations = attribute_parts [associations, field] end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
scope_hound-0.1.0 | app/controllers/concerns/scope_hound/filterable_controller.rb |