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, #view_context

Methods included from LayoutHelper

#_icon_classes, #apple_favicon, #application_path, #current_user_unread_data, #extended_navigation_bar, #external_icon, #favicon, #icon, #legacy_favicon, #organization_colors, #role

Methods included from TooltipHelper

#with_tooltip

Methods included from ModalHelper

#decidim_modal

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_select_field, #scopes_select_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, #ordered_scopes_descendants, #ordered_scopes_descendants_for_select, #scopes_picker_field_tag, #tab_element_class_for, #translated_field_tag

Methods included from ResourceHelper

#linked_classes_filter_values_for, #linked_classes_for, #resource_locator, #resource_title

Instance Method Details

#current_filterObject



15
16
17
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 15

def current_filter
  get_filter_in(:with_date, ALL_FILTERS, model[:default_filter])
end

#current_type_filter_nameObject



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

def current_type_filter_name
  participatory_process_types_for_select.find { |_, id| id == get_filter(:with_any_type) }&.first ||
    I18n.t("all_types", scope: "decidim.participatory_processes.participatory_processes.filters")
end

#explanationObject



97
98
99
100
101
102
103
104
105
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 97

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



107
108
109
110
111
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 107

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

  "no_active_nor_upcoming"
end


8
9
10
11
12
13
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 8

def filter_link(date_filter, type_filter = nil)
  Decidim::ParticipatoryProcesses::Engine
    .routes
    .url_helpers
    .participatory_processes_path(**filter_params(date_filter, type_filter))
end

#filter_name(filter) ⇒ Object



93
94
95
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 93

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

#filter_params(date_filter, type_filter) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 33

def filter_params(date_filter, type_filter)
  {
    filter: {
      with_date: date_filter,
      with_any_scope: get_filter(:with_any_scope),
      with_any_area: get_filter(:with_any_area),
      with_any_type: type_filter || get_filter(:with_any_type)
    }
  }
end

#filter_type_labelObject



89
90
91
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 89

def filter_type_label
  I18n.t("filter_by", scope: "decidim.participatory_processes.participatory_processes.filters")
end

#filtered_processes(date_filter, filter_with_type: true) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 44

def filtered_processes(date_filter, filter_with_type: true)
  query = ParticipatoryProcess.where(organization: current_organization).ransack(
    {
      with_date: date_filter,
      with_any_scope: get_filter(:with_any_scope),
      with_any_area: get_filter(:with_any_area),
      with_any_type: filter_with_type ? get_filter(:with_any_type) : nil
    },
    current_user:
  ).result

  query.published.visible_for(current_user)
end

#get_filter(filter_name, default = nil) ⇒ Object



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

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

#get_filter_in(filter_name, options, default = nil) ⇒ Object



28
29
30
31
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 28

def get_filter_in(filter_name, options, default = nil)
  value = get_filter(filter_name)
  options.include?(value) ? value : default
end

#other_filtersObject



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

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

#other_filters_with_valueObject



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

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

#participatory_process_typesObject



113
114
115
116
117
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 113

def participatory_process_types
  @participatory_process_types ||= Decidim::ParticipatoryProcessType.joins(:processes).where(
    decidim_participatory_processes: { id: filtered_processes(current_filter, filter_with_type: false) }
  ).distinct
end

#participatory_process_types_for_selectObject



119
120
121
122
123
124
125
126
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 119

def participatory_process_types_for_select
  return if participatory_process_types.blank?

  [[I18n.t("all_types", scope: "decidim.participatory_processes.participatory_processes.filters"), ""]] +
    participatory_process_types.map do |type|
      [translated_attribute(type.title), type.id.to_s]
    end
end

#process_count_by_filterObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 58

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)
    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)


81
82
83
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 81

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

#titleObject



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

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