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
-
.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.
- .copy_attachments(original_proposal, proposal) ⇒ Object
-
.create(attributes:, author:, action_user:, user_group_author: nil) ⇒ Object
Public: Creates a new Proposal.
-
.create_with_authors(attributes:, action_user:, original_proposal:) ⇒ Object
Public: Creates a new Proposal with the authors of the ‘original_proposal`.
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
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/proposals/proposal_builder.rb', line 60 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", "state_published_at", "answer", "answered_at", "decidim_component_id", "reference", "comments_count", "endorsements_count", "follows_count", "proposal_notes_count", "proposal_votes_count" ).merge( "category" => original_proposal.category ).merge( extra_attributes ) proposal = if .nil? ( attributes: origin_attributes, original_proposal:, action_user: ) else create( attributes: origin_attributes, author:, user_group_author:, action_user: ) end proposal.link_resources(original_proposal, "copied_from_component") unless skip_link (original_proposal, proposal) proposal end |
.copy_attachments(original_proposal, proposal) ⇒ 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/proposals/proposal_builder.rb', line 106 def (original_proposal, proposal) original_proposal..each do || = Decidim::Attachment.new( { # Attached to needs to be always defined before the file is set attached_to: proposal }.merge( .attributes.slice("content_type", "description", "file_size", "title", "weight") ) ) if .file.attached? .file = .file.blob else .attached_uploader(:file).remote_url = .attached_uploader(:file).url(host: original_proposal.organization.host) end .save! rescue Errno::ENOENT, OpenURI::HTTPError => e Rails.logger.warn("[ERROR] Could not copy attachment from proposal #{original_proposal.id} when copying to component due to #{e.}") end 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.
17 18 19 20 21 22 23 24 |
# File 'app/services/decidim/proposals/proposal_builder.rb', line 17 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.(, user_group: ) 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.
35 36 37 38 39 40 41 42 43 44 |
# File 'app/services/decidim/proposals/proposal_builder.rb', line 35 def (attributes:, action_user:, original_proposal:) Decidim.traceability.perform_action!(:create, Proposal, action_user, visibility: "all") do proposal = Proposal.new(attributes) original_proposal..each do || proposal.(., user_group: .user_group) end proposal.save! proposal end end |