Module: Decidim::Proposals::ProposalBuilder

Defined in:
app/services/decidim/proposals/proposal_builder.rb

Overview

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

Class Method Summary collapse

Class Method Details

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

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

original_proposal - The Proposal to be used as base to create the new one. author - An Authorable the will be the first coauthor of the Proposal. 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 proposal in the traceability logs. extra_attributes - A Hash of attributes to create the new proposal, will overwrite the original ones. skip_link - Whether to skip linking the two proposals or not (default false).

Returns a Proposal

rubocop:disable Metrics/ParameterLists



58
59
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
# File 'app/services/decidim/proposals/proposal_builder.rb', line 58

def copy(original_proposal, author:, action_user:, user_group_author: nil, extra_attributes: {}, skip_link: false)
  origin_attributes = original_proposal.attributes.except(
    "id",
    "created_at",
    "updated_at",
    "state",
    "answer",
    "answered_at",
    "decidim_component_id",
    "reference",
    "proposal_votes_count",
    "proposal_endorsements_count",
    "proposal_notes_count"
  ).merge(
    "category" => original_proposal.category
  ).merge(
    extra_attributes
  )

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

  proposal.link_resources(original_proposal, "copied_from_component") unless skip_link
  proposal
end

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

Public: Creates a new Proposal.

attributes - The Hash of attributes to create the Proposal with. author - An Authorable the will be the first coauthor of the Proposal. 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 proposal in the traceability logs.

Returns a Proposal.



15
16
17
18
19
20
21
22
# File 'app/services/decidim/proposals/proposal_builder.rb', line 15

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

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

Public: Creates a new Proposal with the authors of the ‘original_proposal`.

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

Returns a Proposal.



33
34
35
36
37
38
39
40
41
42
# File 'app/services/decidim/proposals/proposal_builder.rb', line 33

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