Sha256: 70a64b7f8853a33d52a23c09931c2932c08787392ba0d3e52940ac7a94c9604a

Contents?: true

Size: 1.65 KB

Versions: 11

Compression:

Stored size: 1.65 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, options = {})
        selected = object.send(name)

        if selected.present?
          if selected == "all"
            types = Decidim::InitiativesType.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 = Decidim::InitiativesType.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

11 entries across 11 versions & 1 rubygems

Version Path
decidim-initiatives-0.13.1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.12.2 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.13.0 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.12.1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.13.0.pre1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.12.0 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.11.2 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.12.0.pre lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.11.1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.11.0.pre1 lib/decidim/initiatives/initiatives_filter_form_builder.rb
decidim-initiatives-0.9.1 lib/decidim/initiatives/initiatives_filter_form_builder.rb