Class: CoPlan::Plans::Create

Inherits:
Object
  • Object
show all
Defined in:
app/services/coplan/plans/create.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, content:, user:) ⇒ Create

Returns a new instance of Create.



8
9
10
11
12
# File 'app/services/coplan/plans/create.rb', line 8

def initialize(title:, content:, user:)
  @title = title
  @content = content
  @user = user
end

Class Method Details

.call(title:, content:, user:) ⇒ Object



4
5
6
# File 'app/services/coplan/plans/create.rb', line 4

def self.call(title:, content:, user:)
  new(title:, content:, user:).call
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/coplan/plans/create.rb', line 14

def call
  ActiveRecord::Base.transaction do
    plan = Plan.create!(title: @title, created_by_user: @user)
    version = PlanVersion.create!(
      plan: plan,
      revision: 1,
      content_markdown: @content,
      actor_type: "human",
      actor_id: @user.id
    )
    plan.update!(current_plan_version: version, current_revision: 1)
    plan
  end
end