Class: Decidim::ParticipatoryProcesses::ProcessFiltersCell

Inherits:
ViewModel
  • Object
show all
Defined in:
decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb

Constant Summary collapse

ALL_FILTERS =
%w(active upcoming past all).freeze

Instance Method Summary collapse

Methods inherited from ViewModel

#call, #current_user

Methods included from FilterParamsHelper

#filter_link_params

Methods included from MarkupHelper

#element_id

Methods included from ReplaceButtonsHelper

#button_to, #submit_tag

Methods included from ActionAuthorizationHelper

#action_authorized_button_to, #action_authorized_link_to, #logged_button_to, #logged_link_to

Methods included from ScopesHelper

#has_visible_scopes?, #scope_name_for_picker, #scopes_picker_field, #scopes_picker_filter, #scopes_picker_tag

Methods included from TranslatableAttributes

#default_locale?

Methods included from DecidimFormHelper

#areas_for_select, #base_error_messages, #decidim_form_for, #decidim_form_slug_url, #editor_field_tag, #form_field_has_error?, #form_required_explanation, #name_with_locale, #scopes_picker_field_tag, #tab_element_class_for, #translated_field_tag

Methods included from ResourceHelper

#linked_classes_filter_values_for, #linked_classes_for, #linked_resources_for, #resource_locator, #resource_title

Instance Method Details

#current_filterObject


21
22
23
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 21

def current_filter
  get_filter(:date, model[:default_filter])
end

#explanationObject


74
75
76
77
78
79
80
81
82
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 74

def explanation
  return if process_count_by_filter["active"].positive?

  (
    :span,
    I18n.t(explanation_text, scope: "decidim.participatory_processes.participatory_processes.filters.explanations"),
    class: "muted mr-s ml-s"
  )
end

#explanation_textObject


84
85
86
87
88
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 84

def explanation_text
  return "no_active" if process_count_by_filter["upcoming"].positive?

  "no_active_nor_upcoming"
end

8
9
10
11
12
13
14
15
16
17
18
19
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 8

def filter_link(filter)
  Decidim::ParticipatoryProcesses::Engine
    .routes
    .url_helpers
    .participatory_processes_path(
      filter: {
        scope_id: get_filter(:scope_id),
        area_id: get_filter(:area_id),
        date: filter
      }
    )
end

#filter_name(filter) ⇒ Object


70
71
72
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 70

def filter_name(filter)
  I18n.t(filter, scope: "decidim.participatory_processes.participatory_processes.filters.names")
end

#filtered_processes(date_filter) ⇒ Object


29
30
31
32
33
34
35
36
37
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 29

def filtered_processes(date_filter)
  ParticipatoryProcessSearch.new(
    date: date_filter,
    scope_id: get_filter(:scope_id),
    area_id: get_filter(:area_id),
    current_user: current_user,
    organization: current_organization
  )
end

#get_filter(filter_name, default = nil) ⇒ Object


25
26
27
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 25

def get_filter(filter_name, default = nil)
  params&.dig(:filter, filter_name) || default
end

#other_filtersObject


52
53
54
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 52

def other_filters
  @other_filters ||= ALL_FILTERS - [current_filter]
end

#other_filters_with_valueObject


56
57
58
59
60
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 56

def other_filters_with_value
  @other_filters_with_value ||= other_filters.select do |filter|
    process_count_by_filter[filter].positive?
  end
end

#process_count_by_filterObject


39
40
41
42
43
44
45
46
47
48
49
50
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 39

def process_count_by_filter
  return @process_count_by_filter if @process_count_by_filter

  @process_count_by_filter = %w(active upcoming past).inject({}) do |collection_by_filter, filter_name|
    filtered_processes = filtered_processes(filter_name).results
    processes = filtered_processes.groupless
    groups = Decidim::ParticipatoryProcessGroup.where(id: filtered_processes.grouped.group_ids)
    collection_by_filter.merge(filter_name => processes.count + groups.count)
  end
  @process_count_by_filter["all"] = @process_count_by_filter.values.sum
  @process_count_by_filter
end

#should_show_tabs?Boolean

Returns:

  • (Boolean)

62
63
64
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 62

def should_show_tabs?
  other_filters_with_value.any? && other_filters_with_value != ["all"]
end

#titleObject


66
67
68
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 66

def title
  I18n.t(current_filter, scope: "decidim.participatory_processes.participatory_processes.filters.counters", count: process_count_by_filter[current_filter])
end