Module: Decidim::DecidimAwesome::Admin::ConfigConstraintsHelpers

Includes:
TranslatableAttributes
Included in:
ConfigController, CustomRedirectsController, MenuHacksController, Permissions
Defined in:
app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb

Constant Summary collapse

OTHER_MANIFESTS =
[:none, :system, :process_groups].freeze

Instance Method Summary collapse

Instance Method Details

#check(status) ⇒ Object



11
12
13
# File 'app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb', line 11

def check(status)
  (:span, icon(status ? "check" : "x", class: "icon", aria_label: status, role: "img"), class: "text-#{status ? "success" : "alert"}")
end

#component_manifests(space = nil) ⇒ Object



53
54
55
56
57
58
59
# File 'app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb', line 53

def component_manifests(space = nil)
  return {} if OTHER_MANIFESTS.include?(space)

  Decidim.component_manifests.pluck(:name).to_h do |name|
    [name.to_sym, I18n.t("decidim.components.#{name}.name")]
  end
end

#components_list(manifest, slug) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb', line 70

def components_list(manifest, slug)
  space = model_for_manifest(manifest)
  return {} unless space&.column_names&.include? "slug"

  components = Component.where(participatory_space: space.find_by(slug: slug))
  components.to_h do |item|
    [item.id, "#{item.id}: #{translated_attribute(item.name)}"]
  end
end

#config_enabled?(var) ⇒ Boolean

ensure boolean value

Returns:

  • (Boolean)


41
42
43
# File 'app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb', line 41

def config_enabled?(var)
  DecidimAwesome.enabled?(var)
end

#enabled_configs(vars) ⇒ Object

returns only non :disabled vars in config



34
35
36
37
38
# File 'app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb', line 34

def enabled_configs(vars)
  vars.filter do |var|
    config_enabled? var
  end
end

#md5(text) ⇒ Object



100
101
102
# File 'app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb', line 100

def md5(text)
  Digest::MD5.hexdigest(text)
end


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb', line 15

def menus
  @menus ||= {
    editors: config_enabled?([:allow_images_in_full_editor, :allow_images_in_small_editor, :use_markdown_editor, :allow_images_in_markdown_editor]),
    proposals: config_enabled?([:allow_images_in_proposals,
                                :validate_title_min_length, :validate_title_max_caps_percent,
                                :validate_title_max_marks_together, :validate_title_start_with_caps,
                                :validate_body_min_length, :validate_body_max_caps_percent,
                                :validate_body_max_marks_together, :validate_body_start_with_caps]),
    surveys: config_enabled?(:auto_save_forms),
    styles: config_enabled?(:scoped_styles),
    proposal_custom_fields: config_enabled?(:proposal_custom_fields),
    admins: config_enabled?(:scoped_admins),
    menu_hacks: config_enabled?(:menu),
    custom_redirects: config_enabled?(:custom_redirects),
    livechat: config_enabled?([:intergram_for_admins, :intergram_for_public])
  }
end

#participatory_space_manifestsObject



45
46
47
48
49
50
51
# File 'app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb', line 45

def participatory_space_manifests
  manifests = OTHER_MANIFESTS.index_with { |m| I18n.t("decidim.decidim_awesome.admin.config.#{m}") }
  Decidim.participatory_space_manifests.pluck(:name).each do |name|
    manifests[name.to_sym] = I18n.t("decidim.admin.menu.#{name}")
  end
  manifests
end

#participatory_spaces_list(manifest) ⇒ Object



61
62
63
64
65
66
67
68
# File 'app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb', line 61

def participatory_spaces_list(manifest)
  space = model_for_manifest(manifest)
  return {} if space.blank?

  space.where(organization: current_organization).to_h do |item|
    [item.try(:slug) || item.id.to_s, translated_attribute(item.title)]
  end
end

#translate_constraint_value(constraint, key) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb', line 80

def translate_constraint_value(constraint, key)
  value = constraint.settings[key]
  case key.to_sym
  when :participatory_space_manifest
    participatory_space_manifests[value.to_sym] || value
  when :participatory_space_slug
    manifest = constraint.settings["participatory_space_manifest"]
    participatory_spaces_list(manifest)[value] || value
  when :component_manifest
    component_manifests[value.to_sym] || value
  when :component_id
    component = Component.find_by(id: value)
    return "#{component.id}: #{translated_attribute(component.name)}" if component

    value
  else
    value
  end
end