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,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This command is executed when the user creates a Status from the admin
7
+ # panel.
8
+ class CreateStatus < Rectify::Command
9
+ def initialize(form)
10
+ @form = form
11
+ end
12
+
13
+ # Creates the status if valid.
14
+ #
15
+ # Broadcasts :ok if successful, :invalid otherwise.
16
+ def call
17
+ return broadcast(:invalid) if @form.invalid?
18
+
19
+ transaction do
20
+ create_status
21
+ end
22
+
23
+ broadcast(:ok)
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :status
29
+
30
+ def create_status
31
+ @status = Status.create!(
32
+ feature: @form.current_feature,
33
+ key: @form.key,
34
+ name: @form.name,
35
+ description: @form.description,
36
+ progress: @form.progress
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This command is executed when the user creates a TimelineEntry
7
+ # for a Result from the admin panel.
8
+ class CreateTimelineEntry < Rectify::Command
9
+ def initialize(form)
10
+ @form = form
11
+ end
12
+
13
+ # Creates the timeline_entry if valid.
14
+ #
15
+ # Broadcasts :ok if successful, :invalid otherwise.
16
+ def call
17
+ return broadcast(:invalid) if @form.invalid?
18
+
19
+ transaction do
20
+ create_timeline_entry
21
+ end
22
+
23
+ broadcast(:ok)
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :timeline_entry
29
+
30
+ def create_timeline_entry
31
+ @timeline_entry = TimelineEntry.create!(
32
+ decidim_accountability_result_id: @form.decidim_accountability_result_id,
33
+ entry_date: @form.entry_date,
34
+ description: @form.description
35
+ )
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This command is executed when the user changes a Result from the admin
7
+ # panel.
8
+ class UpdateResult < Rectify::Command
9
+ # Initializes an UpdateResult Command.
10
+ #
11
+ # form - The form from which to get the data.
12
+ # result - The current instance of the result to be updated.
13
+ def initialize(form, result)
14
+ @form = form
15
+ @result = result
16
+ end
17
+
18
+ # Updates the result if valid.
19
+ #
20
+ # Broadcasts :ok if successful, :invalid otherwise.
21
+ def call
22
+ return broadcast(:invalid) if form.invalid?
23
+
24
+ current_proposal_ids = result.linked_resources(:proposals, "included_proposals").pluck(:id).sort
25
+ form_proposal_ids = form.proposal_ids.reject(&:blank?).sort
26
+
27
+ transaction do
28
+ update_result
29
+ result.touch if form_proposal_ids != current_proposal_ids
30
+ link_proposals
31
+ link_meetings
32
+ end
33
+
34
+ broadcast(:ok)
35
+ end
36
+
37
+ private
38
+
39
+ attr_reader :result, :form
40
+
41
+ def update_result
42
+ result.update_attributes!(
43
+ scope: @form.scope,
44
+ category: @form.category,
45
+ parent_id: @form.parent_id,
46
+ title: @form.title,
47
+ description: @form.description,
48
+ external_id: @form.external_id,
49
+ start_date: @form.start_date,
50
+ end_date: @form.end_date,
51
+ progress: @form.progress,
52
+ decidim_accountability_status_id: @form.decidim_accountability_status_id
53
+ )
54
+ end
55
+
56
+ def proposals
57
+ @proposals ||= result.sibling_scope(:proposals).where(id: form.proposal_ids)
58
+ end
59
+
60
+ def meeting_ids
61
+ @meeting_ids ||= proposals.flat_map do |proposal|
62
+ proposal.linked_resources(:meetings, "proposals_from_meeting").pluck(:id)
63
+ end.uniq
64
+ end
65
+
66
+ def meetings
67
+ @meetings ||= result.sibling_scope(:meetings).where(id: meeting_ids)
68
+ end
69
+
70
+ def link_proposals
71
+ result.link_resources(proposals, "included_proposals")
72
+ end
73
+
74
+ def link_meetings
75
+ result.link_resources(meetings, "meetings_through_proposals")
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This command is executed when the user changes a Result from the admin
7
+ # panel.
8
+ class UpdateStatus < Rectify::Command
9
+ # Initializes an UpdateStatus Command.
10
+ #
11
+ # form - The form from which to get the data.
12
+ # status - The current instance of the status to be updated.
13
+ def initialize(form, status)
14
+ @form = form
15
+ @status = status
16
+ end
17
+
18
+ # Updates the status if valid.
19
+ #
20
+ # Broadcasts :ok if successful, :invalid otherwise.
21
+ def call
22
+ return broadcast(:invalid) if form.invalid?
23
+
24
+ transaction do
25
+ update_status
26
+ end
27
+
28
+ broadcast(:ok)
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :status, :form
34
+
35
+ def update_status
36
+ status.update_attributes!(
37
+ key: @form.key,
38
+ name: @form.name,
39
+ description: @form.description,
40
+ progress: @form.progress
41
+ )
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This command is executed when the user changes TemplateTexts from the admin
7
+ # panel.
8
+ class UpdateTemplateTexts < Rectify::Command
9
+ # Initializes an UpdateResult Command.
10
+ #
11
+ # form - The form from which to get the data.
12
+ # template_texts - The current instance of the template_texts to be updated.
13
+ def initialize(form, template_texts)
14
+ @form = form
15
+ @template_texts = template_texts
16
+ end
17
+
18
+ # Updates the result if valid.
19
+ #
20
+ # Broadcasts :ok if successful, :invalid otherwise.
21
+ def call
22
+ return broadcast(:invalid) if form.invalid?
23
+
24
+ transaction do
25
+ update_template_texts
26
+ end
27
+
28
+ broadcast(:ok)
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :template_texts, :form
34
+
35
+ def update_template_texts
36
+ template_texts.update_attributes!(
37
+ intro: @form.intro,
38
+ categories_label: @form.categories_label,
39
+ subcategories_label: @form.subcategories_label,
40
+ heading_parent_level_results: @form.heading_parent_level_results,
41
+ heading_leaf_level_results: @form.heading_leaf_level_results,
42
+ )
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This command is executed when the user changes a Result from the admin
7
+ # panel.
8
+ class UpdateTimelineEntry < Rectify::Command
9
+ # Initializes an UpdateTimelineEntry Command.
10
+ #
11
+ # form - The form from which to get the data.
12
+ # timeline_entry - The current instance of the timeline_entry to be updated.
13
+ def initialize(form, timeline_entry)
14
+ @form = form
15
+ @timeline_entry = timeline_entry
16
+ end
17
+
18
+ # Updates the timeline_entry if valid.
19
+ #
20
+ # Broadcasts :ok if successful, :invalid otherwise.
21
+ def call
22
+ return broadcast(:invalid) if form.invalid?
23
+
24
+ transaction do
25
+ update_timeline_entry
26
+ end
27
+
28
+ broadcast(:ok)
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :timeline_entry, :form
34
+
35
+ def update_timeline_entry
36
+ timeline_entry.update_attributes!(
37
+ entry_date: @form.entry_date,
38
+ description: @form.description
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This controller is the abstract class from which all other controllers of
7
+ # this engine inherit.
8
+ #
9
+ # Note that it inherits from `Decidim::Features::BaseController`, which
10
+ # override its layout and provide all kinds of useful methods.
11
+ class ApplicationController < Decidim::Admin::Features::BaseController
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This controller allows an admin to import results from a csv file for the Accountability feature
7
+ class ImportsController < Admin::ApplicationController
8
+
9
+ def new
10
+ @errors = []
11
+ end
12
+
13
+ def create
14
+ @csv_file = params[:csv_file]
15
+ redirect_to new_import_path and return unless @csv_file.present?
16
+
17
+ i = CSVImporter.new(current_feature, @csv_file.path)
18
+ @errors = i.import!
19
+ if @errors.empty?
20
+ flash[:notice] = I18n.t("imports.create.success", scope: "decidim.accountability.admin")
21
+ redirect_to new_import_path
22
+ else
23
+ flash.now[:error] = I18n.t("imports.create.invalid", scope: "decidim.accountability.admin")
24
+ render :new
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This controller allows an admin to manage results from a Participatory Process
7
+ class ResultsController < Admin::ApplicationController
8
+ helper_method :results, :parent_result, :parent_results, :statuses
9
+
10
+ def new
11
+ @form = form(ResultForm).instance
12
+ @form.parent_id = params[:parent_id]
13
+ end
14
+
15
+ def create
16
+ @form = form(ResultForm).from_params(params)
17
+
18
+ CreateResult.call(@form) do
19
+ on(:ok) do
20
+ flash[:notice] = I18n.t("results.create.success", scope: "decidim.accountability.admin")
21
+ redirect_to results_path(parent_id: result.parent_id)
22
+ end
23
+
24
+ on(:invalid) do
25
+ flash.now[:alert] = I18n.t("results.create.invalid", scope: "decidim.accountability.admin")
26
+ render action: "new"
27
+ end
28
+ end
29
+ end
30
+
31
+ def edit
32
+ @form = form(ResultForm).from_model(result)
33
+ end
34
+
35
+ def update
36
+ @form = form(ResultForm).from_params(params)
37
+
38
+ UpdateResult.call(@form, result) do
39
+ on(:ok) do
40
+ flash[:notice] = I18n.t("results.update.success", scope: "decidim.accountability.admin")
41
+ redirect_to results_path(parent_id: result.parent_id)
42
+ end
43
+
44
+ on(:invalid) do
45
+ flash.now[:alert] = I18n.t("results.update.invalid", scope: "decidim.accountability.admin")
46
+ render action: "edit"
47
+ end
48
+ end
49
+ end
50
+
51
+ def destroy
52
+ result.destroy!
53
+
54
+ flash[:notice] = I18n.t("results.destroy.success", scope: "decidim.accountability.admin")
55
+
56
+ redirect_to results_path(parent_id: result.parent_id)
57
+ end
58
+
59
+ private
60
+
61
+ def results
62
+ parent_id = params[:parent_id].presence
63
+ @results ||= Result.where(feature: current_feature, parent_id: parent_id).page(params[:page]).per(15)
64
+ end
65
+
66
+ def result
67
+ @result ||= Result.where(feature: current_feature).find(params[:id])
68
+ end
69
+
70
+ def parent_result
71
+ @parent_result ||= Result.where(feature: current_feature, id: params[:parent_id]).first
72
+ end
73
+
74
+ def parent_results
75
+ @parent_results ||= Result.where(feature: current_feature, parent_id: nil)
76
+ end
77
+
78
+ def statuses
79
+ @statuses ||= Status.where(feature: current_feature)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Accountability
5
+ module Admin
6
+ # This controller allows an admin to manage results from a Participatory Process
7
+ class StatusesController < Admin::ApplicationController
8
+ helper_method :statuses
9
+
10
+ def new
11
+ @form = form(StatusForm).instance
12
+ end
13
+
14
+ def create
15
+ @form = form(StatusForm).from_params(params)
16
+
17
+ CreateStatus.call(@form) do
18
+ on(:ok) do
19
+ flash[:notice] = I18n.t("statuses.create.success", scope: "decidim.accountability.admin")
20
+ redirect_to statuses_path
21
+ end
22
+
23
+ on(:invalid) do
24
+ flash.now[:alert] = I18n.t("statuses.create.invalid", scope: "decidim.accountability.admin")
25
+ render action: "new"
26
+ end
27
+ end
28
+ end
29
+
30
+ def edit
31
+ @form = form(StatusForm).from_model(status)
32
+ end
33
+
34
+ def update
35
+ @form = form(StatusForm).from_params(params)
36
+
37
+ UpdateStatus.call(@form, status) do
38
+ on(:ok) do
39
+ flash[:notice] = I18n.t("statuses.update.success", scope: "decidim.accountability.admin")
40
+ redirect_to statuses_path
41
+ end
42
+
43
+ on(:invalid) do
44
+ flash.now[:alert] = I18n.t("statuses.update.invalid", scope: "decidim.accountability.admin")
45
+ render action: "edit"
46
+ end
47
+ end
48
+ end
49
+
50
+ def destroy
51
+ status.destroy!
52
+
53
+ flash[:notice] = I18n.t("statuses.destroy.success", scope: "decidim.accountability.admin")
54
+
55
+ redirect_to statuses_path
56
+ end
57
+
58
+ private
59
+
60
+ def statuses
61
+ @statuses ||= Status.where(feature: current_feature).page(params[:page]).per(15)
62
+ end
63
+
64
+ def status
65
+ @status ||= statuses.find(params[:id])
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end