Sha256: 9878b1bcec759cd9f0d8c08500b391e43acecdbb563b265a467d010a52fd5f49

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Decidim
  # Helper that provides a single method to create filter resource forms
  module FiltersHelper
    # This method wraps everything in a div with class filters and calls
    # the form_for helper with a custom builder
    #
    # filter       - A filter object
    # url          - A String with the URL to post the from. Self URL by default.
    # html_options - Extra HTML options to be passed to form_for
    # block        - A block to be called with the form builder
    #
    # Returns the filter resource form wrapped in a div
    def filter_form_for(filter, url = url_for, html_options = {})
      content_tag :div, class: "filters" do
        form_for(
          filter,
          namespace: filter_form_namespace,
          builder: FilterFormBuilder,
          url: url,
          as: :filter,
          method: :get,
          remote: true,
          html: { id: nil }.merge(html_options)
        ) do |form|
          yield form
        end
      end
    end

    def filter_cache_hash(filter, type = nil)
      hash = []
      hash << "decidim/proposals/filters"
      hash << type.to_s if type.present?
      hash << Digest::MD5.hexdigest(filter.to_json)

      hash.join("/")
    end

    private

    # Creates a unique namespace for a filter form to prevent dupliacte IDs in
    # the DOM when multiple filter forms are rendered with the same fields (e.g.
    # for desktop and mobile).
    def filter_form_namespace
      "filters_#{SecureRandom.uuid}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decidim-core-0.24.2 app/helpers/decidim/filters_helper.rb