Module: Decidim::Plans::PlanBuilder

Defined in:
app/services/decidim/plans/plan_builder.rb

Overview

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

Class Method Summary collapse

Class Method Details

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

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

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

Returns a Plan

rubocop:disable Metrics/ParameterLists



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/decidim/plans/plan_builder.rb', line 38

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

  plan = create(
    attributes: origin_attributes,
    author: author,
    user_group_author: user_group_author,
    action_user: action_user
  )

  plan.link_resources(original_plan, "copied_from_component") unless skip_link
  plan
end

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

Public: Creates a new Plan.

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

Returns a Plan.



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

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