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

Constants included from AmendmentsHelper

AmendmentsHelper::TOTAL_STEPS

Instance Method Summary collapse

Methods inherited from ViewModel

#call, #current_user, #view_context

Methods included from IconHelper

#component_icon, #manifest_icon, #resource_icon, #resource_type_icon, #resource_type_icon_key, #text_with_resource_icon

Methods included from LayoutHelper

#_icon_classes, #apple_favicon, #application_path, #current_url, #current_user_unread_data, #external_icon, #favicon, #icon, #legacy_favicon, #role, #root_url

Methods included from TooltipHelper

#with_tooltip

Methods included from ModalHelper

#decidim_drawer, #decidim_modal

Methods included from OrganizationHelper

#current_organization_name, #organization_colors, #organization_description_label, #organization_name

Methods included from TranslatableAttributes

#attachment?, #default_locale?

Methods included from ApplicationHelper

#add_body_classes, #cell, #edit_link, #extra_admin_link, #html_truncate, #layout_item_classes, #present, #prevent_timeout_seconds, #resolve_presenter_class, #text_initials

Methods included from CacheHelper

#cache

Methods included from AmendmentsHelper

#accept_and_reject_buttons_for, #action_button_card_for, #allowed_to_accept_and_reject?, #allowed_to_promote?, #amendments_enabled?, #amendments_form_field_for, #amendments_form_fields_label, #amendments_form_fields_value, #can_participate_in_private_space?, #can_react_to_emendation?, #current_step, #emendation_actions_for, #emendation_announcement_for, #promote_button_for, #render_emendation_body, #total_steps, #wizard_aside_back_url, #wizard_header_title

Methods included from RichTextEditorHelper

included, #text_editor_for

Methods included from ContextualHelpHelper

#floating_help

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, #tab_element_class_for, #translated_field_tag

Methods included from OmniauthHelper

#normalize_provider_name, #oauth_icon, #provider_name

Methods included from SanitizeHelper

#decidim_escape_translated, #decidim_html_escape, #decidim_rich_text, #decidim_sanitize, #decidim_sanitize_admin, #decidim_sanitize_editor, #decidim_sanitize_editor_admin, #decidim_sanitize_newsletter, #decidim_sanitize_translated, #decidim_url_escape, included

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 ResourceHelper

#linked_classes_filter_values_for, #linked_classes_for, #resource_locator

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

#explanationObject



88
89
90
91
92
93
94
95
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 88

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

  (
    :span,
    I18n.t(explanation_text, scope: "decidim.participatory_processes.participatory_processes.filters.explanations")
  )
end

#explanation_textObject



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

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)
  Decidim::ParticipatoryProcesses::Engine
    .routes
    .url_helpers
    .participatory_processes_path(**filter_params(date_filter))
end

#filter_name(filter) ⇒ Object



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

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

#filter_params(date_filter) ⇒ Object



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

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

#filtered_processes(date_filter) ⇒ Object



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

def filtered_processes(date_filter)
  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)
    },
    current_user:
  ).result

  query.published.visible_for(current_user)
end

#get_filter(filter_name, default = nil) ⇒ Object



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

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

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



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

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

#other_filtersObject



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

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

#other_filters_with_valueObject



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

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



53
54
55
56
57
58
59
60
61
62
63
64
# File 'decidim-participatory_processes/app/cells/decidim/participatory_processes/process_filters_cell.rb', line 53

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)


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

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

#titleObject



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

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