Module: Decidim::EnhancedTextwork::ParagraphBuilder

Defined in:
app/services/decidim/enhanced_textwork/paragraph_builder.rb

Overview

A factory class to ensure we always create Paragraphs the same way since it involves some logic.

Class Method Summary collapse

Class Method Details

.copy(original_paragraph, author:, action_user:, user_group_author: nil, extra_attributes: {}, skip_link: false) ⇒ Object

Public: Creates a new Paragraph by copying the attributes from another one.

original_paragraph - The Paragraph to be used as base to create the new one. author - An Authorable the will be the first coauthor of the Paragraph. user_group_author - A User Group to, optionally, set it as the author too. action_user - The User to be used as the user who is creating the paragraph in the traceability logs. extra_attributes - A Hash of attributes to create the new paragraph, will overwrite the original ones. skip_link - Whether to skip linking the two paragraphs or not (default false).

Returns a Paragraph

rubocop:disable Metrics/ParameterLists



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/services/decidim/enhanced_textwork/paragraph_builder.rb', line 60

def copy(original_paragraph, author:, action_user:, user_group_author: nil, extra_attributes: {}, skip_link: false)
  origin_attributes = original_paragraph.attributes.except(
    "id",
    "created_at",
    "updated_at",
    "state",
    "state_published_at",
    "answer",
    "answered_at",
    "decidim_component_id",
    "reference",
    "comments_count",
    "endorsements_count",
    "follows_count",
    "paragraph_notes_count",
    "paragraph_votes_count"
  ).merge(
    "category" => original_paragraph.category
  ).merge(
    extra_attributes
  )

  paragraph = if author.nil?
               create_with_authors(
                 attributes: origin_attributes,
                 original_paragraph: original_paragraph,
                 action_user: action_user
               )
             else
               create(
                 attributes: origin_attributes,
                 author: author,
                 user_group_author: user_group_author,
                 action_user: action_user
               )
             end

  paragraph.link_resources(original_paragraph, "copied_from_component") unless skip_link
  copy_attachments(original_paragraph, paragraph)

  paragraph
end

.copy_attachments(original_paragraph, paragraph) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/services/decidim/enhanced_textwork/paragraph_builder.rb', line 106

def copy_attachments(original_paragraph, paragraph)
  original_paragraph.attachments.each do |attachment|
    new_attachment = Decidim::Attachment.new(
      {
        # Attached to needs to be always defined before the file is set
        attached_to: paragraph
      }.merge(
        attachment.attributes.slice("content_type", "description", "file_size", "title", "weight")
      )
    )

    if attachment.file.attached?
      new_attachment.file = attachment.file.blob
    else
      new_attachment.attached_uploader(:file).remote_url = attachment.attached_uploader(:file).url(host: original_paragraph.organization.host)
    end

    new_attachment.save!
  rescue Errno::ENOENT, OpenURI::HTTPError => e
    Rails.logger.warn("[ERROR] Couldn't copy attachment from paragraph #{original_paragraph.id} when copying to component due to #{e.message}")
  end
end

.create(attributes:, author:, action_user:, user_group_author: nil) ⇒ Object

Public: Creates a new Paragraph.

attributes - The Hash of attributes to create the Paragraph with. author - An Authorable the will be the first coauthor of the Paragraph. user_group_author - A User Group to, optionally, set it as the author too. action_user - The User to be used as the user who is creating the paragraph in the traceability logs.

Returns a Paragraph.



17
18
19
20
21
22
23
24
# File 'app/services/decidim/enhanced_textwork/paragraph_builder.rb', line 17

def create(attributes:, author:, action_user:, user_group_author: nil)
  Decidim.traceability.perform_action!(:create, Paragraph, action_user, visibility: "all") do
    paragraph = Paragraph.new(attributes)
    paragraph.add_coauthor(author, user_group: user_group_author)
    paragraph.save!
    paragraph
  end
end

.create_with_authors(attributes:, action_user:, original_paragraph:) ⇒ Object

Public: Creates a new Paragraph with the authors of the `original_paragraph`.

attributes - The Hash of attributes to create the Paragraph with. action_user - The User to be used as the user who is creating the paragraph in the traceability logs. original_paragraph - The paragraph from which authors will be copied.

Returns a Paragraph.



35
36
37
38
39
40
41
42
43
44
# File 'app/services/decidim/enhanced_textwork/paragraph_builder.rb', line 35

def create_with_authors(attributes:, action_user:, original_paragraph:)
  Decidim.traceability.perform_action!(:create, Paragraph, action_user, visibility: "all") do
    paragraph = Paragraph.new(attributes)
    original_paragraph.coauthorships.each do |coauthorship|
      paragraph.add_coauthor(coauthorship.author, user_group: coauthorship.user_group)
    end
    paragraph.save!
    paragraph
  end
end