Module: Decidim::Budgets::ProjectsHelper

Includes:
ApplicationHelper, MapHelper
Included in:
ApplicationHelper, BudgetListItemCell, BudgetMCell, ProjectListItemCell, ProjectMCell
Defined in:
decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb

Overview

A helper to render order and budgets actions

Instance Method Summary collapse

Instance Method Details

#budget_confirm_disabled_attrObject


33
34
35
36
37
# File 'decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb', line 33

def budget_confirm_disabled_attr
  return if current_order_can_be_checked_out?

  %( disabled="disabled" ).html_safe
end

#budget_to_currency(budget) ⇒ Object

Render a budget as a currency

budget - A integer to represent a budget


13
14
15
# File 'decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb', line 13

def budget_to_currency(budget)
  number_to_currency budget, unit: Decidim.currency_unit, precision: 0
end

#current_order_budget_percentObject

Return a percentage of the current order budget from the total budget


18
19
20
# File 'decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb', line 18

def current_order_budget_percent
  current_order&.budget_percent.to_f.floor
end

#current_order_budget_percent_minimumObject

Return the minimum percentage of the current order budget from the total budget


23
24
25
26
27
28
29
30
31
# File 'decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb', line 23

def current_order_budget_percent_minimum
  return 0 if current_order.minimum_projects_rule?

  if current_order.projects_rule?
    (current_order.minimum_projects.to_f / current_order.maximum_projects)
  else
    component_settings.vote_threshold_percent
  end
end

#current_order_can_be_checked_out?Boolean

Return true if the user can continue to the checkout process

Returns:

  • (Boolean)

43
44
45
# File 'decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb', line 43

def current_order_can_be_checked_out?
  current_order&.can_checkout?
end

#current_rule_descriptionObject


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb', line 67

def current_rule_description
  return unless current_order

  if current_order.projects_rule?
    if current_order.minimum_projects.positive? && current_order.minimum_projects < current_order.maximum_projects
      t(
        ".projects_rule.description",
        minimum_number: current_order.minimum_projects,
        maximum_number: current_order.maximum_projects
      )
    else
      t(".projects_rule_maximum_only.description", maximum_number: current_order.maximum_projects)
    end
  elsif current_order.minimum_projects_rule?
    t(".minimum_projects_rule.description", minimum_number: current_order.minimum_projects)
  else
    t(".vote_threshold_percent_rule.description", minimum_budget: budget_to_currency(current_order.minimum_budget))
  end
end

#current_rule_explanationObject


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb', line 47

def current_rule_explanation
  return unless current_order

  if current_order.projects_rule?
    if current_order.minimum_projects.positive? && current_order.minimum_projects < current_order.maximum_projects
      t(
        ".projects_rule.instruction",
        minimum_number: current_order.minimum_projects,
        maximum_number: current_order.maximum_projects
      )
    else
      t(".projects_rule_maximum_only.instruction", maximum_number: current_order.maximum_projects)
    end
  elsif current_order.minimum_projects_rule?
    t(".minimum_projects_rule.instruction", minimum_number: current_order.minimum_projects)
  else
    t(".vote_threshold_percent_rule.instruction", minimum_budget: budget_to_currency(current_order.minimum_budget))
  end
end

#has_position?(project) ⇒ Boolean

Returns:

  • (Boolean)

107
108
109
110
111
# File 'decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb', line 107

def has_position?(project)
  return if project.address.blank?

  project.latitude.present? && project.longitude.present?
end

#project_data_for_map(project) ⇒ Object


96
97
98
99
100
101
102
103
104
105
# File 'decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb', line 96

def project_data_for_map(project)
  project
    .slice(:latitude, :longitude, :address)
    .merge(
      title: decidim_html_escape(translated_attribute(project.title)),
      description: html_truncate(decidim_sanitize_editor(translated_attribute(project.description)), length: 100),
      icon: icon("project", width: 40, height: 70, remove_icon_class: true),
      link: ::Decidim::ResourceLocatorPresenter.new([project.budget, project]).path
    )
end

#projects_data_for_map(geocoded_projects) ⇒ Object

Serialize a collection of geocoded projects to be used by the dynamic map component

geocoded_projects - A collection of geocoded projects


90
91
92
93
94
# File 'decidim-budgets/app/helpers/decidim/budgets/projects_helper.rb', line 90

def projects_data_for_map(geocoded_projects)
  geocoded_projects.map do |project|
    project_data_for_map(project)
  end
end