Sha256: 98b69151d16bd00be5df72943031a7a9917b9a019c313d69a634f9a936ec1cfd

Contents?: true

Size: 1.62 KB

Versions: 47

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
        {
          feature: current_feature,
          current_user: current_user
        }
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 2 rubygems

Version Path
decidim-core-0.10.1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.10.0 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.9.3 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.9.2 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.9.1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.9.0 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.8.4 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.8.3 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.8.2 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.8.1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.8.0 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.7.4 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.7.3 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.7.2 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.7.1 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.7.0 app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.6.8 app/controllers/concerns/decidim/filter_resource.rb
decidim-0.6.8 decidim-core/app/controllers/concerns/decidim/filter_resource.rb
decidim-core-0.6.7 app/controllers/concerns/decidim/filter_resource.rb
decidim-0.6.7 decidim-core/app/controllers/concerns/decidim/filter_resource.rb