Module: Decidim::Proposals::ProposalBuilder
- Defined in:
- decidim-proposals/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:, 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:) ⇒ 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:, 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. 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
57 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 95 96 97 98 |
# File 'decidim-proposals/app/services/decidim/proposals/proposal_builder.rb', line 57 def copy(original_proposal, author:, action_user:, extra_attributes: {}, skip_link: false) origin_attributes = original_proposal.attributes.except( "id", "created_at", "updated_at", "state", "decidim_proposals_proposal_state_id", "state_published_at", "answer", "answered_at", "decidim_component_id", "reference", "comments_count", "endorsements_count", "follows_count", "proposal_notes_count", "proposal_votes_count" ).merge( "taxonomies" => original_proposal.taxonomies ).merge( extra_attributes ) proposal = if .nil? ( attributes: origin_attributes, original_proposal:, action_user: ) else create( attributes: origin_attributes, 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'decidim-proposals/app/services/decidim/proposals/proposal_builder.rb', line 102 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 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:) ⇒ 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. action_user - The User to be used as the user who is creating the proposal in the traceability logs.
Returns a Proposal.
16 17 18 19 20 21 22 23 |
# File 'decidim-proposals/app/services/decidim/proposals/proposal_builder.rb', line 16 def create(attributes:, author:, action_user:) Decidim.traceability.perform_action!(:create, Proposal, action_user, visibility: "all") do proposal = Proposal.new(attributes) proposal.() 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.
34 35 36 37 38 39 40 41 42 43 |
# File 'decidim-proposals/app/services/decidim/proposals/proposal_builder.rb', line 34 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.(.) end proposal.save! proposal end end |