decidim-debates 0.22.0 → 0.23.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/app/cells/decidim/debates/debate_activity_cell.rb +4 -0
  3. data/app/cells/decidim/debates/debate_m/data.erb +10 -8
  4. data/app/cells/decidim/debates/debate_m/footer.erb +5 -0
  5. data/app/cells/decidim/debates/debate_m_cell.rb +24 -2
  6. data/app/commands/decidim/debates/admin/close_debate.rb +54 -0
  7. data/app/commands/decidim/debates/admin/create_debate.rb +4 -2
  8. data/app/commands/decidim/debates/admin/update_debate.rb +5 -2
  9. data/app/commands/decidim/debates/close_debate.rb +58 -0
  10. data/app/commands/decidim/debates/create_debate.rb +17 -12
  11. data/app/commands/decidim/debates/update_debate.rb +56 -0
  12. data/app/controllers/decidim/debates/admin/debate_closes_controller.rb +43 -0
  13. data/app/controllers/decidim/debates/admin/debates_controller.rb +4 -2
  14. data/app/controllers/decidim/debates/debates_controller.rb +70 -9
  15. data/app/controllers/decidim/debates/orderable.rb +41 -0
  16. data/app/controllers/decidim/debates/versions_controller.rb +16 -0
  17. data/app/controllers/decidim/debates/widgets_controller.rb +19 -0
  18. data/app/events/decidim/debates/close_debate_event.rb +15 -0
  19. data/app/forms/decidim/debates/admin/close_debate_form.rb +34 -0
  20. data/app/forms/decidim/debates/admin/debate_form.rb +5 -1
  21. data/app/forms/decidim/debates/close_debate_form.rb +39 -0
  22. data/app/forms/decidim/debates/debate_form.rb +27 -1
  23. data/app/helpers/decidim/debates/admin/application_helper.rb +13 -0
  24. data/app/helpers/decidim/debates/application_helper.rb +37 -1
  25. data/app/models/decidim/debates/debate.rb +60 -8
  26. data/app/permissions/decidim/debates/admin/permissions.rb +3 -1
  27. data/app/permissions/decidim/debates/permissions.rb +28 -0
  28. data/app/presenters/decidim/debates/admin_log/debate_presenter.rb +6 -2
  29. data/app/presenters/decidim/debates/admin_log/value_types/debate_title_description_presenter.rb +20 -0
  30. data/app/presenters/decidim/debates/debate_presenter.rb +62 -7
  31. data/app/presenters/decidim/debates/log/resource_presenter.rb +18 -0
  32. data/app/presenters/decidim/debates/official_author_presenter.rb +1 -29
  33. data/app/services/decidim/debates/debate_search.rb +13 -23
  34. data/app/services/decidim/debates/diff_renderer.rb +27 -0
  35. data/app/views/decidim/debates/admin/debate_closes/edit.html.erb +17 -0
  36. data/app/views/decidim/debates/admin/debates/_form.html.erb +4 -4
  37. data/app/views/decidim/debates/admin/debates/index.html.erb +7 -1
  38. data/app/views/decidim/debates/debates/_close_debate_modal.html.erb +13 -0
  39. data/app/views/decidim/debates/debates/_debates.html.erb +7 -0
  40. data/app/views/decidim/debates/debates/_filters.html.erb +15 -13
  41. data/app/views/decidim/debates/debates/_form.html.erb +19 -0
  42. data/app/views/decidim/debates/debates/edit.html.erb +22 -0
  43. data/app/views/decidim/debates/debates/new.html.erb +1 -22
  44. data/app/views/decidim/debates/debates/show.html.erb +81 -11
  45. data/app/views/decidim/debates/versions/index.html.erb +8 -0
  46. data/app/views/decidim/debates/versions/show.html.erb +10 -0
  47. data/config/locales/am-ET.yml +1 -0
  48. data/config/locales/ar.yml +2 -1
  49. data/config/locales/bg.yml +15 -0
  50. data/config/locales/ca.yml +70 -1
  51. data/config/locales/cs.yml +70 -1
  52. data/config/locales/da.yml +1 -0
  53. data/config/locales/de.yml +48 -1
  54. data/config/locales/el.yml +2 -1
  55. data/config/locales/en.yml +70 -1
  56. data/config/locales/eo.yml +1 -0
  57. data/config/locales/es-MX.yml +70 -1
  58. data/config/locales/es-PY.yml +70 -1
  59. data/config/locales/es.yml +70 -1
  60. data/config/locales/et.yml +1 -0
  61. data/config/locales/eu.yml +2 -1
  62. data/config/locales/fi-plain.yml +70 -1
  63. data/config/locales/fi.yml +70 -1
  64. data/config/locales/fr-CA.yml +70 -1
  65. data/config/locales/fr.yml +70 -1
  66. data/config/locales/gl.yml +2 -1
  67. data/config/locales/hr.yml +1 -0
  68. data/config/locales/hu.yml +24 -1
  69. data/config/locales/id-ID.yml +2 -1
  70. data/config/locales/is-IS.yml +2 -1
  71. data/config/locales/is.yml +102 -0
  72. data/config/locales/it.yml +33 -1
  73. data/config/locales/ja-JP.yml +63 -20
  74. data/config/locales/ja.yml +221 -0
  75. data/config/locales/ko-KR.yml +1 -0
  76. data/config/locales/ko.yml +1 -0
  77. data/config/locales/lt.yml +1 -0
  78. data/config/locales/{lv-LV.yml → lv.yml} +0 -0
  79. data/config/locales/mt.yml +1 -0
  80. data/config/locales/nl.yml +46 -1
  81. data/config/locales/no.yml +42 -1
  82. data/config/locales/om-ET.yml +1 -0
  83. data/config/locales/pl.yml +58 -6
  84. data/config/locales/pt-BR.yml +2 -1
  85. data/config/locales/pt.yml +18 -1
  86. data/config/locales/ro-RO.yml +18 -1
  87. data/config/locales/ru.yml +2 -1
  88. data/config/locales/sk.yml +2 -1
  89. data/config/locales/sl.yml +13 -1
  90. data/config/locales/so-SO.yml +1 -0
  91. data/config/locales/sv.yml +51 -1
  92. data/config/locales/ti-ER.yml +1 -0
  93. data/config/locales/tr-TR.yml +2 -1
  94. data/config/locales/uk.yml +2 -1
  95. data/config/locales/vi-VN.yml +1 -0
  96. data/config/locales/vi.yml +1 -0
  97. data/config/locales/zh-CN.yml +218 -0
  98. data/config/locales/zh-TW.yml +1 -0
  99. data/db/migrate/20200703134657_close_debates.rb +9 -0
  100. data/db/migrate/20200708072042_fix_debates_i18n_fields.rb +48 -0
  101. data/db/migrate/20200716143929_add_endorsable_to_debates.rb +8 -0
  102. data/db/migrate/20200827154116_add_commentable_counter_cache_to_debates.rb +14 -0
  103. data/db/migrate/20200902133452_add_cached_comment_metadata_to_debates.rb +23 -0
  104. data/lib/decidim/debates/admin_engine.rb +3 -1
  105. data/lib/decidim/debates/component.rb +44 -1
  106. data/lib/decidim/debates/engine.rb +7 -1
  107. data/lib/decidim/debates/test/factories.rb +62 -6
  108. data/lib/decidim/debates/version.rb +1 -1
  109. metadata +57 -13
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Decidim
6
+ module Debates
7
+ # Common logic to sorting resources
8
+ module Orderable
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ include Decidim::Orderable
13
+
14
+ private
15
+
16
+ def available_orders
17
+ @available_orders ||= %w(random recent commented updated)
18
+ end
19
+
20
+ def default_order
21
+ "updated"
22
+ end
23
+
24
+ def reorder(debates)
25
+ case order
26
+ when "recent"
27
+ debates.order("created_at DESC")
28
+ when "commented"
29
+ debates.order("comments_count DESC")
30
+ when "updated"
31
+ debates.order("updated_at DESC")
32
+ when "random"
33
+ debates.order_randomly(random_seed)
34
+ else
35
+ debates
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Debates
5
+ # Exposes Debates versions so users can see how a Debate has been updated
6
+ # through time.
7
+ class VersionsController < Decidim::Proposals::ApplicationController
8
+ include Decidim::ApplicationHelper
9
+ include Decidim::ResourceVersionsConcern
10
+
11
+ def versioned_resource
12
+ @versioned_resource ||= present(Debate.where(component: current_component).find(params[:debate_id]))
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Debates
5
+ class WidgetsController < Decidim::WidgetsController
6
+ helper Debates::ApplicationHelper
7
+
8
+ private
9
+
10
+ def model
11
+ @model ||= Debate.where(component: params[:component_id]).find(params[:debate_id])
12
+ end
13
+
14
+ def iframe_url
15
+ @iframe_url ||= debate_widget_url(model)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ # frozen-string_literal: true
2
+
3
+ module Decidim
4
+ module Debates
5
+ class CloseDebateEvent < Decidim::Events::SimpleEvent
6
+ def resource_text
7
+ translated_attribute(resource.conclusions)
8
+ end
9
+
10
+ def event_has_roles?
11
+ true
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Debates
5
+ module Admin
6
+ # This class holds a Form to close debates from Decidim's admin views.
7
+ class CloseDebateForm < Decidim::Form
8
+ include TranslatableAttributes
9
+
10
+ mimic :debate
11
+
12
+ translatable_attribute :conclusions, String do |translated_attribute, locale|
13
+ validates translated_attribute, presence: true, if: ->(record) { record.default_locale?(locale) }
14
+ validates translated_attribute, length: { minimum: 10, maximum: 10_000 }, if: ->(record) { record.default_locale?(locale) }
15
+ end
16
+
17
+ attribute :debate, Debate
18
+
19
+ validates :debate, presence: true
20
+ validate :user_can_close_debate
21
+
22
+ def closed_at
23
+ debate&.closed_at || Time.current
24
+ end
25
+
26
+ private
27
+
28
+ def user_can_close_debate
29
+ errors.add(:debate, :invalid) unless debate.official?
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -24,7 +24,11 @@ module Decidim
24
24
  validates :category, presence: true, if: ->(form) { form.decidim_category_id.present? }
25
25
 
26
26
  def map_model(model)
27
- self.decidim_category_id = model.category.try(:id)
27
+ self.decidim_category_id = model.categorization.decidim_category_id if model.categorization
28
+ presenter = DebatePresenter.new(model)
29
+
30
+ self.title = presenter.title(all_locales: title.is_a?(Hash))
31
+ self.description = presenter.description(all_locales: description.is_a?(Hash))
28
32
  end
29
33
 
30
34
  def category
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Debates
5
+ # This class holds a Form to close debates from Decidim's public views.
6
+ class CloseDebateForm < Decidim::Form
7
+ mimic :debate
8
+
9
+ attribute :conclusions, String
10
+ attribute :debate, Debate
11
+
12
+ validates :debate, presence: true
13
+ validates :conclusions, presence: true, length: { minimum: 10, maximum: 10_000 }
14
+ validate :user_can_close_debate
15
+
16
+ def closed_at
17
+ debate&.closed_at || Time.current
18
+ end
19
+
20
+ def map_model(debate)
21
+ super
22
+ self.debate = debate
23
+
24
+ # Debates can be translated in different languages from the admin but
25
+ # the public form doesn't allow it. When a user closes a debate the
26
+ # user locale is taken as the text locale.
27
+ self.conclusions = debate.conclusions&.values&.first
28
+ end
29
+
30
+ private
31
+
32
+ def user_can_close_debate
33
+ return if !debate || !debate.respond_to?(:closeable_by?)
34
+
35
+ errors.add(:debate, :invalid) unless debate.closeable_by?(current_user)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -10,15 +10,41 @@ module Decidim
10
10
  attribute :description, String
11
11
  attribute :category_id, Integer
12
12
  attribute :user_group_id, Integer
13
+ attribute :debate, Debate
13
14
 
14
15
  validates :title, presence: true
15
16
  validates :description, presence: true
16
-
17
17
  validates :category, presence: true, if: ->(form) { form.category_id.present? }
18
+ validate :editable_by_user
19
+
20
+ def map_model(debate)
21
+ super
22
+ self.debate = debate
23
+
24
+ # Debates can be translated in different languages from the admin but
25
+ # the public form doesn't allow it. When a user creates a debate the
26
+ # user locale is taken as the text locale.
27
+ self.title = debate.title.values.first
28
+ self.description = debate.description.values.first
29
+ self.user_group_id = debate.decidim_user_group_id
30
+
31
+ if debate.category.present?
32
+ self.category_id = debate.category.id
33
+ @category = debate.category
34
+ end
35
+ end
18
36
 
19
37
  def category
20
38
  @category ||= current_component.categories.find_by(id: category_id)
21
39
  end
40
+
41
+ private
42
+
43
+ def editable_by_user
44
+ return unless debate.respond_to?(:editable_by?)
45
+
46
+ errors.add(:debate, :invalid) unless debate.editable_by?(current_user)
47
+ end
22
48
  end
23
49
  end
24
50
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Debates
5
+ module Admin
6
+ # Custom helpers, scoped to the debates admin engine.
7
+ #
8
+ module ApplicationHelper
9
+ include Decidim::Admin::ResourceScopeHelper
10
+ end
11
+ end
12
+ end
13
+ end
@@ -8,6 +8,9 @@ module Decidim
8
8
  include PaginateHelper
9
9
  include Decidim::Comments::CommentsHelper
10
10
  include Decidim::RichTextEditorHelper
11
+ include Decidim::EndorsableHelper
12
+ include Decidim::FollowableHelper
13
+ include Decidim::CheckBoxesTreeHelper
11
14
 
12
15
  # If the debate is official or the rich text editor is enabled on the
13
16
  # frontend, the debate description is considered as safe content.
@@ -17,7 +20,7 @@ module Decidim
17
20
 
18
21
  # If the content is safe, HTML tags are sanitized, otherwise, they are stripped.
19
22
  def render_debate_description(debate)
20
- description = present(debate).description(strip_tags: !safe_content?)
23
+ description = present(debate).description(strip_tags: !safe_content?, links: true)
21
24
 
22
25
  safe_content? ? decidim_sanitize(description) : simple_format(description)
23
26
  end
@@ -26,6 +29,39 @@ module Decidim
26
29
  def text_editor_for_debate_description(form)
27
30
  text_editor_for(form, :description)
28
31
  end
32
+
33
+ # Returns a TreeNode to be used in the list filters to filter debates by
34
+ # its origin.
35
+ def filter_origin_values
36
+ origin_values = []
37
+ origin_values << TreePoint.new("official", t("decidim.debates.debates.filters.official"))
38
+ origin_values << TreePoint.new("citizens", t("decidim.debates.debates.filters.citizens"))
39
+ origin_values << TreePoint.new("user_group", t("decidim.debates.debates.filters.user_groups")) if current_organization.user_groups_enabled?
40
+
41
+ TreeNode.new(TreePoint.new("", t("decidim.debates.debates.filters.all")), origin_values)
42
+ end
43
+
44
+ # Options to filter Debates by activity.
45
+ def activity_filter_values
46
+ base = [
47
+ ["all", t("decidim.debates.debates.filters.all")],
48
+ ["my_debates", t("decidim.debates.debates.filters.my_debates")]
49
+ ]
50
+ base += [["commented", t("decidim.debates.debates.filters.commented")]]
51
+ base
52
+ end
53
+
54
+ # Returns a TreeNode to be used in the list filters to filter debates by
55
+ # its state.
56
+ def filter_debates_state_values
57
+ Decidim::CheckBoxesTreeHelper::TreeNode.new(
58
+ Decidim::CheckBoxesTreeHelper::TreePoint.new("", t("decidim.debates.debates.filters.all")),
59
+ [
60
+ Decidim::CheckBoxesTreeHelper::TreePoint.new("open", t("decidim.debates.debates.filters.state_values.open")),
61
+ Decidim::CheckBoxesTreeHelper::TreePoint.new("closed", t("decidim.debates.debates.filters.state_values.closed"))
62
+ ]
63
+ )
64
+ end
29
65
  end
30
66
  end
31
67
  end
@@ -11,7 +11,7 @@ module Decidim
11
11
  include Decidim::Resourceable
12
12
  include Decidim::Followable
13
13
  include Decidim::Comments::Commentable
14
- include Decidim::ScopableComponent
14
+ include Decidim::ScopableResource
15
15
  include Decidim::Authorable
16
16
  include Decidim::Reportable
17
17
  include Decidim::HasReference
@@ -20,11 +20,17 @@ module Decidim
20
20
  include Decidim::DataPortability
21
21
  include Decidim::NewsletterParticipant
22
22
  include Decidim::Searchable
23
+ include Decidim::TranslatableResource
24
+ include Decidim::TranslatableAttributes
25
+ include Decidim::Endorsable
26
+ include Decidim::Randomable
23
27
 
28
+ belongs_to :last_comment_by, polymorphic: true, foreign_key: "last_comment_by_id", foreign_type: "last_comment_by_type", optional: true
24
29
  component_manifest_name "debates"
25
30
 
26
31
  validates :title, presence: true
27
32
 
33
+ translatable_fields :title, :description, :instructions, :information_updates
28
34
  searchable_fields({
29
35
  participatory_space: { component: :participatory_space },
30
36
  A: :title,
@@ -34,17 +40,23 @@ module Decidim
34
40
  index_on_create: ->(debate) { debate.visible? },
35
41
  index_on_update: ->(debate) { debate.visible? })
36
42
 
43
+ scope :open, -> { where(closed_at: nil) }
44
+ scope :closed, -> { where.not(closed_at: nil) }
45
+ scope :authored_by, ->(author) { where(author: author) }
46
+ scope :commented_by, lambda { |author|
47
+ joins(:comments).where(
48
+ decidim_comments_comments:
49
+ {
50
+ decidim_author_id: author.id,
51
+ decidim_author_type: author.class.base_class.name
52
+ }
53
+ )
54
+ }
55
+
37
56
  def self.log_presenter_class_for(_log)
38
57
  Decidim::Debates::AdminLog::DebatePresenter
39
58
  end
40
59
 
41
- # Public: Checks whether the debate is official or not.
42
- #
43
- # Returns a boolean.
44
- def official?
45
- author.is_a?(Decidim::Organization)
46
- end
47
-
48
60
  # Public: Overrides the `reported_content_url` Reportable concern method.
49
61
  def reported_content_url
50
62
  ResourceLocatorPresenter.new(self).url
@@ -82,6 +94,7 @@ module Decidim
82
94
  # Public: Overrides the `accepts_new_comments?` Commentable concern method.
83
95
  def accepts_new_comments?
84
96
  return false unless open?
97
+ return false if closed?
85
98
 
86
99
  commentable? && !comments_blocked?
87
100
  end
@@ -124,6 +137,45 @@ module Decidim
124
137
  .pluck(:decidim_author_id).flatten.compact.uniq
125
138
  end
126
139
 
140
+ # Checks whether the user can edit the debate.
141
+ #
142
+ # user - the user to check for authorship
143
+ def editable_by?(user)
144
+ !closed? && authored_by?(user)
145
+ end
146
+
147
+ # Checks whether the debate is closed or not.
148
+ #
149
+ def closed?
150
+ closed_at.present? && conclusions.present?
151
+ end
152
+
153
+ # Checks whether the user can edit the debate.
154
+ #
155
+ # user - the user to check for authorship
156
+ def closeable_by?(user)
157
+ authored_by?(user)
158
+ end
159
+
160
+ # Public: Updates the comments counter cache. We have to do it these
161
+ # way in order to properly calculate the counter with hidden
162
+ # comments.
163
+ #
164
+ # rubocop:disable Rails/SkipsModelValidations
165
+ def update_comments_count
166
+ comments_count = comments.not_hidden.count
167
+ last_comment = comments.not_hidden.order("created_at DESC").first
168
+
169
+ update_columns(
170
+ last_comment_at: last_comment&.created_at,
171
+ last_comment_by_id: last_comment&.decidim_author_id,
172
+ last_comment_by_type: last_comment&.decidim_author_type,
173
+ comments_count: comments_count,
174
+ updated_at: Time.current
175
+ )
176
+ end
177
+ # rubocop:enable Rails/SkipsModelValidations
178
+
127
179
  private
128
180
 
129
181
  def comments_blocked?
@@ -12,7 +12,9 @@ module Decidim
12
12
  case permission_action.action
13
13
  when :create, :read
14
14
  allow!
15
- when :update, :delete
15
+ when :update
16
+ toggle_allow(debate && !debate.closed? && debate.official?)
17
+ when :delete, :close
16
18
  toggle_allow(debate && debate.official?)
17
19
  end
18
20
 
@@ -15,6 +15,12 @@ module Decidim
15
15
  toggle_allow(can_create_debate?)
16
16
  when :report
17
17
  allow!
18
+ when :edit
19
+ can_edit_debate?
20
+ when :endorse
21
+ can_endorse_debate?
22
+ when :close
23
+ can_close_debate?
18
24
  end
19
25
 
20
26
  permission_action
@@ -26,6 +32,28 @@ module Decidim
26
32
  authorized?(:create) &&
27
33
  current_settings&.creation_enabled? && component.participatory_space.can_participate?(user)
28
34
  end
35
+
36
+ def can_edit_debate?
37
+ return allow! if debate&.editable_by?(user)
38
+
39
+ disallow!
40
+ end
41
+
42
+ def can_close_debate?
43
+ return allow! if debate&.closeable_by?(user)
44
+
45
+ disallow!
46
+ end
47
+
48
+ def can_endorse_debate?
49
+ return disallow! if debate.closed?
50
+
51
+ allow!
52
+ end
53
+
54
+ def debate
55
+ @debate ||= context.fetch(:debate, nil) || context.fetch(:resource, nil)
56
+ end
29
57
  end
30
58
  end
31
59
  end