decidim-accountability 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +46 -0
  3. data/Rakefile +3 -0
  4. data/app/assets/config/decidim_accountability_admin_manifest.js +1 -0
  5. data/app/assets/config/decidim_accountability_manifest.js +1 -0
  6. data/app/assets/images/decidim/accountability/icon.svg +1 -0
  7. data/app/assets/javascripts/decidim/accountability/accountability.js.es6 +10 -0
  8. data/app/assets/javascripts/decidim/accountability/admin/accountability_admin.js +8 -0
  9. data/app/assets/stylesheets/decidim/accountability/_accountability.scss +1 -0
  10. data/app/assets/stylesheets/decidim/accountability/accountability/_cards.scss +13 -0
  11. data/app/assets/stylesheets/decidim/accountability/accountability/_categories.scss +109 -0
  12. data/app/assets/stylesheets/decidim/accountability/accountability/_lines_breadcrumb.scss +29 -0
  13. data/app/assets/stylesheets/decidim/accountability/accountability/_results.scss +144 -0
  14. data/app/commands/decidim/accountability/admin/create_result.rb +72 -0
  15. data/app/commands/decidim/accountability/admin/create_status.rb +42 -0
  16. data/app/commands/decidim/accountability/admin/create_timeline_entry.rb +40 -0
  17. data/app/commands/decidim/accountability/admin/update_result.rb +80 -0
  18. data/app/commands/decidim/accountability/admin/update_status.rb +46 -0
  19. data/app/commands/decidim/accountability/admin/update_template_texts.rb +47 -0
  20. data/app/commands/decidim/accountability/admin/update_timeline_entry.rb +44 -0
  21. data/app/controllers/decidim/accountability/admin/application_controller.rb +15 -0
  22. data/app/controllers/decidim/accountability/admin/imports_controller.rb +30 -0
  23. data/app/controllers/decidim/accountability/admin/results_controller.rb +84 -0
  24. data/app/controllers/decidim/accountability/admin/statuses_controller.rb +70 -0
  25. data/app/controllers/decidim/accountability/admin/template_texts_controller.rb +38 -0
  26. data/app/controllers/decidim/accountability/admin/timeline_entries_controller.rb +75 -0
  27. data/app/controllers/decidim/accountability/application_controller.rb +13 -0
  28. data/app/controllers/decidim/accountability/result_widgets_controller.rb +19 -0
  29. data/app/controllers/decidim/accountability/results_controller.rb +77 -0
  30. data/app/forms/decidim/accountability/admin/result_form.rb +74 -0
  31. data/app/forms/decidim/accountability/admin/status_form.rb +21 -0
  32. data/app/forms/decidim/accountability/admin/template_texts_form.rb +19 -0
  33. data/app/forms/decidim/accountability/admin/timeline_entry_form.rb +20 -0
  34. data/app/helpers/decidim/accountability/application_helper.rb +50 -0
  35. data/app/models/decidim/accountability/application_record.rb +10 -0
  36. data/app/models/decidim/accountability/result.rb +66 -0
  37. data/app/models/decidim/accountability/status.rb +18 -0
  38. data/app/models/decidim/accountability/template_texts.rb +17 -0
  39. data/app/models/decidim/accountability/timeline_entry.rb +11 -0
  40. data/app/services/decidim/accountability/csv_exporter.rb +77 -0
  41. data/app/services/decidim/accountability/csv_importer.rb +106 -0
  42. data/app/services/decidim/accountability/result_search.rb +40 -0
  43. data/app/services/decidim/accountability/result_stats_calculator.rb +52 -0
  44. data/app/services/decidim/accountability/results_calculator.rb +34 -0
  45. data/app/views/decidim/accountability/admin/imports/new.html.erb +47 -0
  46. data/app/views/decidim/accountability/admin/results/_form.html.erb +70 -0
  47. data/app/views/decidim/accountability/admin/results/edit.html.erb +9 -0
  48. data/app/views/decidim/accountability/admin/results/index.html.erb +52 -0
  49. data/app/views/decidim/accountability/admin/results/new.html.erb +9 -0
  50. data/app/views/decidim/accountability/admin/shared/_subnav.html.erb +3 -0
  51. data/app/views/decidim/accountability/admin/statuses/_form.html.erb +23 -0
  52. data/app/views/decidim/accountability/admin/statuses/edit.html.erb +8 -0
  53. data/app/views/decidim/accountability/admin/statuses/index.html.erb +45 -0
  54. data/app/views/decidim/accountability/admin/statuses/new.html.erb +8 -0
  55. data/app/views/decidim/accountability/admin/template_texts/_form.html.erb +30 -0
  56. data/app/views/decidim/accountability/admin/template_texts/edit.html.erb +8 -0
  57. data/app/views/decidim/accountability/admin/timeline_entries/_form.html.erb +15 -0
  58. data/app/views/decidim/accountability/admin/timeline_entries/edit.html.erb +8 -0
  59. data/app/views/decidim/accountability/admin/timeline_entries/index.html.erb +41 -0
  60. data/app/views/decidim/accountability/admin/timeline_entries/new.html.erb +8 -0
  61. data/app/views/decidim/accountability/result_widgets/show.html.erb +2 -0
  62. data/app/views/decidim/accountability/results/_home_categories.html.erb +73 -0
  63. data/app/views/decidim/accountability/results/_home_header.html.erb +24 -0
  64. data/app/views/decidim/accountability/results/_linked_results.html.erb +12 -0
  65. data/app/views/decidim/accountability/results/_nav_breadcrumb.html.erb +29 -0
  66. data/app/views/decidim/accountability/results/_results_leaf.html.erb +45 -0
  67. data/app/views/decidim/accountability/results/_results_parent.html.erb +32 -0
  68. data/app/views/decidim/accountability/results/_scope_filters.html.erb +14 -0
  69. data/app/views/decidim/accountability/results/_search.html.erb +12 -0
  70. data/app/views/decidim/accountability/results/_show_leaf.html.erb +79 -0
  71. data/app/views/decidim/accountability/results/_show_parent.html.erb +25 -0
  72. data/app/views/decidim/accountability/results/_stats.html.erb +11 -0
  73. data/app/views/decidim/accountability/results/_stats_box.html.erb +36 -0
  74. data/app/views/decidim/accountability/results/_tags.html.erb +10 -0
  75. data/app/views/decidim/accountability/results/_timeline.html.erb +24 -0
  76. data/app/views/decidim/accountability/results/home.html.erb +9 -0
  77. data/app/views/decidim/accountability/results/index.html.erb +19 -0
  78. data/app/views/decidim/accountability/results/index.js.erb +2 -0
  79. data/app/views/decidim/accountability/results/show.html.erb +14 -0
  80. data/config/i18n-tasks.yml +10 -0
  81. data/config/locales/ca.yml +190 -0
  82. data/config/locales/en.yml +190 -0
  83. data/config/locales/es.yml +190 -0
  84. data/config/locales/eu.yml +79 -0
  85. data/config/locales/fi.yml +77 -0
  86. data/db/migrate/20170425154712_create_accountability_statuses.rb +13 -0
  87. data/db/migrate/20170426104125_create_accountability_results.rb +22 -0
  88. data/db/migrate/20170508104902_add_description_and_progress_to_statuses.rb +6 -0
  89. data/db/migrate/20170508161109_create_template_texts.rb +14 -0
  90. data/db/migrate/20170606102902_add_index_to_accountability_results_on_external_id.rb +5 -0
  91. data/db/migrate/20170620154712_create_accountability_timeline_entries.rb +13 -0
  92. data/db/migrate/20170623094200_migrate_accountability_results_category.rb +13 -0
  93. data/db/migrate/20170623144902_add_children_counter_cache_to_results.rb +5 -0
  94. data/lib/decidim/accountability.rb +12 -0
  95. data/lib/decidim/accountability/admin.rb +10 -0
  96. data/lib/decidim/accountability/admin_engine.rb +32 -0
  97. data/lib/decidim/accountability/feature.rb +86 -0
  98. data/lib/decidim/accountability/list_engine.rb +27 -0
  99. data/lib/decidim/accountability/test/factories.rb +45 -0
  100. metadata +238 -0
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This controller allows an admin to manage template texts per Accountability feature
7
+ class TemplateTextsController < Admin::ApplicationController
8
+ helper_method :template_texts
9
+
10
+ def edit
11
+ @form = form(TemplateTextsForm).from_model(template_texts)
12
+ end
13
+
14
+ def update
15
+ @form = form(TemplateTextsForm).from_params(params)
16
+
17
+ UpdateTemplateTexts.call(@form, template_texts) do
18
+ on(:ok) do
19
+ flash[:notice] = I18n.t("template_texts.update.success", scope: "decidim.accountability.admin")
20
+ redirect_to edit_template_texts_path
21
+ end
22
+
23
+ on(:invalid) do
24
+ flash.now[:alert] = I18n.t("template_texts.update.invalid", scope: "decidim.accountability.admin")
25
+ render action: "edit"
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def template_texts
33
+ @template_texts ||= TemplateTexts.for(current_feature)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This controller allows an admin to manage timeline entries for a Result
7
+ class TimelineEntriesController < Admin::ApplicationController
8
+ helper_method :result, :timeline_entries
9
+
10
+ def new
11
+ @form = form(TimelineEntryForm).instance
12
+ end
13
+
14
+ def create
15
+ @form = form(TimelineEntryForm).from_params(params)
16
+ @form.decidim_accountability_result_id = params[:result_id]
17
+
18
+ CreateTimelineEntry.call(@form) do
19
+ on(:ok) do
20
+ flash[:notice] = I18n.t("timeline_entries.create.success", scope: "decidim.accountability.admin")
21
+ redirect_to result_timeline_entries_path(params[:result_id])
22
+ end
23
+
24
+ on(:invalid) do
25
+ flash.now[:alert] = I18n.t("timeline_entries.create.invalid", scope: "decidim.accountability.admin")
26
+ render action: "new"
27
+ end
28
+ end
29
+ end
30
+
31
+ def edit
32
+ @form = form(TimelineEntryForm).from_model(timeline_entry)
33
+ end
34
+
35
+ def update
36
+ @form = form(TimelineEntryForm).from_params(params)
37
+
38
+ UpdateTimelineEntry.call(@form, timeline_entry) do
39
+ on(:ok) do
40
+ flash[:notice] = I18n.t("timeline_entries.update.success", scope: "decidim.accountability.admin")
41
+ redirect_to result_timeline_entries_path(params[:result_id])
42
+ end
43
+
44
+ on(:invalid) do
45
+ flash.now[:alert] = I18n.t("timeline_entries.update.invalid", scope: "decidim.accountability.admin")
46
+ render action: "edit"
47
+ end
48
+ end
49
+ end
50
+
51
+ def destroy
52
+ timeline_entry.destroy!
53
+
54
+ flash[:notice] = I18n.t("timeline_entries.destroy.success", scope: "decidim.accountability.admin")
55
+
56
+ redirect_to result_timeline_entries_path(params[:result_id])
57
+ end
58
+
59
+ private
60
+
61
+ def timeline_entries
62
+ @timeline_entries ||= result.timeline_entries.page(params[:page]).per(15)
63
+ end
64
+
65
+ def timeline_entry
66
+ @timeline_entry ||= timeline_entries.find(params[:id])
67
+ end
68
+
69
+ def result
70
+ @result ||= Result.where(feature: current_feature).find(params[:result_id])
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ # This controller is the abstract class from which all other controllers of
6
+ # this engine inherit.
7
+ #
8
+ # Note that it inherits from `Decidim::Components::BaseController`, which
9
+ # override its layout and provide all kinds of useful methods.
10
+ class ApplicationController < Decidim::Features::BaseController
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ class ResultWidgetsController < Decidim::WidgetsController
6
+ helper_method :model
7
+
8
+ private
9
+
10
+ def model
11
+ @model ||= Result.where(feature: params[:feature_id]).find(params[:result_id])
12
+ end
13
+
14
+ def iframe_url
15
+ @iframe_url ||= result_result_widget_url(model)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ # Exposes the result resource so users can view them
6
+ class ResultsController < Decidim::Accountability::ApplicationController
7
+ include FilterResource
8
+ helper Decidim::WidgetUrlsHelper
9
+
10
+ helper_method :results, :result, :stats_calculator, :first_class_categories, :category, :progress_calculator, :count_calculator, :current_scope, :template_texts
11
+
12
+ def home
13
+ end
14
+
15
+ def csv
16
+ send_data CSVExporter.new(current_feature).export, filename: "results.csv", disposition: "attachment"
17
+ end
18
+
19
+ private
20
+
21
+ def results
22
+ parent_id = params[:parent_id].presence
23
+ @results ||= search.results.where(parent_id: parent_id).page(params[:page]).per(12)
24
+ end
25
+
26
+ def result
27
+ @result ||= Result.includes(:timeline_entries).where(feature: current_feature).find(params[:id])
28
+ end
29
+
30
+ def stats_calculator
31
+ @stats_calculator ||= ResultStatsCalculator.new(result)
32
+ end
33
+
34
+ def search_klass
35
+ ResultSearch
36
+ end
37
+
38
+ def default_filter_params
39
+ {
40
+ search_text: "",
41
+ scope_id: "",
42
+ category_id: ""
43
+ }
44
+ end
45
+
46
+ def context_params
47
+ { feature: current_feature, organization: current_organization }
48
+ end
49
+
50
+ def first_class_categories
51
+ @first_class_categories ||= current_participatory_process.categories.first_class
52
+ end
53
+
54
+ def category
55
+ if params[:filter] && params[:filter][:category_id].present?
56
+ current_participatory_process.categories.find(params[:filter][:category_id])
57
+ end
58
+ end
59
+
60
+ def progress_calculator(scope_id, category_id)
61
+ Decidim::Accountability::ResultsCalculator.new(current_feature, scope_id, category_id).progress
62
+ end
63
+
64
+ def count_calculator(scope_id, category_id)
65
+ Decidim::Accountability::ResultsCalculator.new(current_feature, scope_id, category_id).count
66
+ end
67
+
68
+ def current_scope
69
+ params[:filter][:scope_id] if params[:filter]
70
+ end
71
+
72
+ def template_texts
73
+ Decidim::Accountability::TemplateTexts.for(current_feature)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This class holds a Form to create/update results from Decidim's admin panel.
7
+ class ResultForm < Decidim::Form
8
+ include TranslatableAttributes
9
+ include TranslationsHelper
10
+
11
+ translatable_attribute :title, String
12
+ translatable_attribute :description, String
13
+
14
+ attribute :decidim_scope_id, Integer
15
+ attribute :decidim_category_id, Integer
16
+ attribute :proposal_ids, Array[Integer]
17
+ attribute :start_date, Date
18
+ attribute :end_date, Date
19
+ attribute :progress, Float
20
+ attribute :external_id, String
21
+ attribute :decidim_accountability_status_id, Integer
22
+ attribute :parent_id, Integer
23
+
24
+ validates :title, translatable_presence: true
25
+ validates :description, translatable_presence: true
26
+
27
+ validates :scope, presence: true, if: ->(form) { form.decidim_scope_id.present? }
28
+ validates :category, presence: true, if: ->(form) { form.decidim_category_id.present? }
29
+
30
+ validates :parent, presence: true, if: ->(form) { form.parent_id.present? }
31
+ validates :status, presence: true, if: ->(form) { form.decidim_accountability_status_id.present? }
32
+
33
+ validate :external_id_uniqueness
34
+
35
+ def map_model(model)
36
+ self.proposal_ids = model.linked_resources(:proposals, "included_proposals").pluck(:id)
37
+ self.decidim_category_id = model.category.try(:id)
38
+ end
39
+
40
+ def proposals
41
+ @proposals ||= Decidim.find_resource_manifest(:proposals).try(:resource_scope, context.current_feature)&.order(title: :asc)&.pluck(:title, :id)
42
+ end
43
+
44
+ def organization_scopes
45
+ current_organization.scopes
46
+ end
47
+
48
+ def scope
49
+ @scope ||= organization_scopes.where(id: decidim_scope_id).first
50
+ end
51
+
52
+ def category
53
+ @category ||= context.current_feature.categories.where(id: decidim_category_id).first
54
+ end
55
+
56
+ def parent
57
+ @parent ||= Decidim::Accountability::Result.where(feature: current_feature, id: parent_id).first
58
+ end
59
+
60
+ def status
61
+ @status ||= Decidim::Accountability::Status.where(feature: current_feature, id: decidim_accountability_status_id).first
62
+ end
63
+
64
+ private
65
+
66
+ def external_id_uniqueness
67
+ return if external_id.blank?
68
+ existing_with_external_id = Decidim::Accountability::Result.find_by(feature: current_feature, external_id: external_id)
69
+ errors.add(:external_id, :taken) if existing_with_external_id && existing_with_external_id.id != id
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This class holds a Form to create/update statuses from Decidim's admin panel.
7
+ class StatusForm < Decidim::Form
8
+ include TranslatableAttributes
9
+ include TranslationsHelper
10
+
11
+ attribute :key, String
12
+ translatable_attribute :name, String
13
+ translatable_attribute :description, String
14
+ attribute :progress, Integer
15
+
16
+ validates :key, presence: true
17
+ validates :name, translatable_presence: true
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This class holds a Form to create/update template texts from Decidim's admin panel.
7
+ class TemplateTextsForm < Decidim::Form
8
+ include TranslatableAttributes
9
+ include TranslationsHelper
10
+
11
+ translatable_attribute :intro, String
12
+ translatable_attribute :categories_label, String
13
+ translatable_attribute :subcategories_label, String
14
+ translatable_attribute :heading_parent_level_results, String
15
+ translatable_attribute :heading_leaf_level_results, String
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This class holds a Form to create/update timeline_entries from Decidim's admin panel.
7
+ class TimelineEntryForm < Decidim::Form
8
+ include TranslatableAttributes
9
+ include TranslationsHelper
10
+
11
+ attribute :decidim_accountability_result_id, Integer
12
+ attribute :entry_date, Date
13
+ translatable_attribute :description, String
14
+
15
+ validates :entry_date, presence: true
16
+ validates :description, translatable_presence: true
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ # Custom helpers, scoped to the meetings engine.
6
+ #
7
+ module ApplicationHelper
8
+ include PaginateHelper
9
+ include Decidim::Comments::CommentsHelper
10
+
11
+ def display_percentage(number)
12
+ return unless number.present?
13
+
14
+ number_to_percentage(number, precision: 1, strip_insignificant_zeros: true, locale: I18n.locale)
15
+ end
16
+
17
+ def display_count(count)
18
+ heading_leaf_level_results(count)
19
+ end
20
+
21
+ def active_class_if_current(scope)
22
+ %Q{class=active} if scope.to_s == current_scope.to_s
23
+ end
24
+
25
+ def categories_label
26
+ translated_attribute(template_texts.categories_label).presence || t("results.home.categories_label", scope: "decidim.accountability")
27
+ end
28
+
29
+ def subcategories_label
30
+ translated_attribute(template_texts.subcategories_label).presence || t("results.home.subcategories_label", scope: "decidim.accountability")
31
+ end
32
+
33
+ def heading_parent_level_results(count)
34
+ if text = translated_attribute(template_texts.heading_parent_level_results).presence
35
+ "#{count} #{text}"
36
+ else
37
+ t("results.count.results_count", scope: "decidim.accountability", count: count)
38
+ end
39
+ end
40
+
41
+ def heading_leaf_level_results(count)
42
+ if text = translated_attribute(template_texts.heading_leaf_level_results).presence
43
+ "#{count} #{text}"
44
+ else
45
+ t("results.count.results_count", scope: "decidim.accountability", count: count)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ # Abstract class from which all models in this engine inherit.
6
+ class ApplicationRecord < ActiveRecord::Base
7
+ self.abstract_class = true
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ # The data store for a Result in the Decidim::Accountability component. It stores a
6
+ # title, description and any other useful information to render a custom result.
7
+ class Result < Accountability::ApplicationRecord
8
+ include Decidim::Resourceable
9
+ include Decidim::HasFeature
10
+ include Decidim::HasScope
11
+ include Decidim::HasCategory
12
+ include Decidim::HasReference
13
+ include Decidim::Comments::Commentable
14
+
15
+ feature_manifest_name "accountability"
16
+
17
+ has_many :children, foreign_key: "parent_id", class_name: "Decidim::Accountability::Result", inverse_of: :parent, dependent: :destroy
18
+ belongs_to :parent, foreign_key: "parent_id", class_name: "Decidim::Accountability::Result", inverse_of: :children, optional: true, counter_cache: :children_count
19
+
20
+ belongs_to :status, foreign_key: "decidim_accountability_status_id", class_name: "Decidim::Accountability::Status", inverse_of: :results, optional: true
21
+
22
+ has_many :timeline_entries, -> { order(:entry_date) }, foreign_key: "decidim_accountability_result_id", class_name: "Decidim::Accountability::TimelineEntry", inverse_of: :result, dependent: :destroy
23
+
24
+ before_validation :remove_blank_values
25
+
26
+ after_save :update_parent_progress, if: -> { parent_id.present? }
27
+
28
+ def update_parent_progress
29
+ return unless parent.present?
30
+
31
+ parent.update_progress!
32
+ end
33
+
34
+ def update_progress!
35
+ self.progress = children.average(:progress)
36
+ save!
37
+ end
38
+
39
+ # Public: Overrides the `commentable?` Commentable concern method.
40
+ def commentable?
41
+ feature.settings.comments_enabled?
42
+ end
43
+
44
+ # Public: Overrides the `accepts_new_comments?` Commentable concern method.
45
+ def accepts_new_comments?
46
+ commentable? && !feature.current_settings.comments_blocked
47
+ end
48
+
49
+ # Public: Overrides the `comments_have_alignment?` Commentable concern method.
50
+ def comments_have_alignment?
51
+ true
52
+ end
53
+
54
+ # Public: Overrides the `comments_have_votes?` Commentable concern method.
55
+ def comments_have_votes?
56
+ true
57
+ end
58
+
59
+ private
60
+
61
+ def remove_blank_values
62
+ self.external_id = nil if external_id.blank?
63
+ end
64
+ end
65
+ end
66
+ end