Sha256: a9d48739d36345c6aa93c365605326529aeb495970971cbd7970a497fc86e1e8

Contents?: true

Size: 1.62 KB

Versions: 22

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # Common logic to filter resources
  module FilterResource
    extend ActiveSupport::Concern

    # Internal: Defines a class that will wrap in an object the URL params used by the filter.
    # this way we can use Rails' form helpers and have automatically checked checkboxes and
    # radio buttons in the view, for example.
    class Filter
      def initialize(filter)
        @filter = filter
      end

      def method_missing(method_name, *_arguments)
        @filter.present? && @filter.has_key?(method_name) ? @filter[method_name] : super
      end

      def respond_to_missing?(method_name, include_private = false)
        @filter.present? && @filter.has_key?(method_name) || super
      end
    end

    included do
      helper_method :search, :filter

      private

      def search
        @search ||= search_klass.new(search_params)
      end

      def search_klass
        raise NotImplementedError, "A search class is neeeded to filter resources"
      end

      def filter
        @filter ||= Filter.new(filter_params)
      end

      def search_params
        default_search_params
          .merge(filter_params)
          .merge(context_params)
      end

      def filter_params
        default_filter_params
          .merge(params.to_unsafe_h[:filter].try(:symbolize_keys) || {})
      end

      def default_search_params
        {}
      end

      def default_filter_params
        {}
      end

      def context_params
        {
          component: current_component,
          current_user: current_user
        }
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
decidim-core-0.17.2 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.17.1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.16.1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.17.0 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.16.0 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.15.2 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.15.1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.15.0 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.14.4 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.14.3 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.14.2 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.14.1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.13.1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.12.2 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.13.0 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.12.1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.13.0.pre1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.12.0 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.11.2 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.12.0.pre app/controllers/concerns/decidim/filter_resource.rb