Sha256: 8af3b7a9868eddc16d3b82f20945614c68a4d40b685ad3af3a24c3aa16b46f46

Contents?: true

Size: 1.63 KB

Versions: 28

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    # This custom Form builder add the fields needed to deal with
    # Initiative types.
    class InitiativesFilterFormBuilder < Decidim::FilterFormBuilder
      # Public: Generates a select field with the initiative types.
      #
      # name       - The name of the field (usually type_id)
      # collection - A collection of initiative types.
      # options    - An optional Hash with options:
      # - prompt   - An optional String with the text to display as prompt.
      #
      # Returns a String.
      def initiative_types_select(name, collection, options = {})
        selected = object.send(name)

        if selected.present?
          if selected == "all"
            types = collection.all.map do |type|
              [type.title[I18n.locale.to_s], type.id]
            end
          else
            selected = selected.values if selected.is_a?(Hash)
            selected = [selected] unless selected.is_a?(Array)
            types = collection.where(id: selected.map(&:to_i)).map do |type|
              [type.title[I18n.locale.to_s], type.id]
            end
          end
        else
          types = []
        end

        prompt = options.delete(:prompt)
        remote_path = options.delete(:remote_path) || false
        multiple = options.delete(:multiple) || false
        html_options = {
          multiple: multiple,
          class: "select2",
          "data-remote-path" => remote_path,
          "data-placeholder" => prompt
        }

        select(name, @template.options_for_select(types, selected: selected), options, html_options)
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
decidim-initiatives-0.23.6 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.23.5 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.23.4 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.23.3 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.23.2 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.23.1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.23.1.rc1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.23.0 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.22.0 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.21.0 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.20.1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.20.0 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.19.1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.18.1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.19.0 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.17.2 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.18.0 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.17.1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.16.1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.17.0 lib/decidim/initiatives/initiatives_filter_form_builder.rb